0.2.12 • Published 12 months ago
safegen v0.2.12
Safegen
Generate well-formed data from Large Language Models (LLMs) without encountering TypeErrors, 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 safegenQuick 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
12 months ago
0.2.11
1 year ago
0.2.10
1 year ago
0.1.0
1 year ago
0.2.1
1 year ago
0.1.2
1 year ago
0.2.0
1 year ago
0.1.1
1 year ago
0.2.7
1 year ago
0.2.6
1 year ago
0.2.9
1 year ago
0.2.8
1 year ago
0.2.3
1 year ago
0.1.4
1 year ago
0.0.5
1 year ago
0.2.2
1 year ago
0.1.3
1 year ago
0.2.5
1 year ago
0.2.4
1 year ago
0.1.5
1 year ago
0.0.4
1 year ago
0.0.3
1 year ago
0.0.2
1 year ago
0.0.1
1 year ago
0.0.0
1 year ago