0.2.12 • Published 4 months ago
safegen v0.2.12
Safegen
Generate well-formed data from Large Language Models (LLMs) without encountering TypeError
s, leveraging the power of Zod and zod-to-json-schema
.
Features
- Type Safety: Ensure data from LLMs conforms to your defined schemas.
- Budget Control: Set a real USD budget before running queries using
OpenAiSafeGenerator
. - Caching: Automatically cache AI generations during test-time to avoid unnecessary API costs and flaky CI outcomes.
Installation
npm install safegen
Quick Start
import { z } from "zod";
import { OpenAiSafeGenerator } from "safegen/openai";
// Initialize the generator with your OpenAI API key and budget
const gptGenerator = new OpenAiSafeGenerator({
usdBudget: 0.01,
usdMinimum: 0.0001,
model: "gpt-4",
apiKey: process.env.OPENAI_API_KEY,
});
// Define the schema for the expected response
const countSpec = {
schema: z.object({ count: z.number() }),
fallback: { count: 0 },
};
// Create a generator function based on the schema
const counter = gptGenerator.from(countSpec);
// Use the generator to get data from the LLM
const { count: numberOfPlanets } = await counter(
"How many planets are in the solar system?"
);
console.log(`There are ${numberOfPlanets} planets in the solar system.`);
// Output: There are 8 planets in the solar system.
In this example:
- Schema Definition: We use Zod to define the expected shape of the data.
- Safe Generation: Safegen ensures the response matches the schema or provides a fallback.
- Budget Control: The generator respects the set budget for API calls.
License
MIT License
0.2.12
4 months ago
0.2.11
5 months ago
0.2.10
5 months ago
0.1.0
8 months ago
0.2.1
7 months ago
0.1.2
8 months ago
0.2.0
7 months ago
0.1.1
8 months ago
0.2.7
5 months ago
0.2.6
5 months ago
0.2.9
5 months ago
0.2.8
5 months ago
0.2.3
6 months ago
0.1.4
7 months ago
0.0.5
8 months ago
0.2.2
7 months ago
0.1.3
7 months ago
0.2.5
5 months ago
0.2.4
6 months ago
0.1.5
7 months ago
0.0.4
9 months ago
0.0.3
9 months ago
0.0.2
9 months ago
0.0.1
9 months ago
0.0.0
9 months ago