llm-fns 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +34 -32
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -115,47 +115,50 @@ const buffer2 = await llm.promptImage({
|
|
|
115
115
|
|
|
116
116
|
---
|
|
117
117
|
|
|
118
|
-
# Use Case 3: Structured Data (`llm.promptZod`)
|
|
118
|
+
# Use Case 3: Structured Data (`llm.promptJson` & `llm.promptZod`)
|
|
119
119
|
|
|
120
|
-
This is a high-level wrapper that employs a **Re-asking Loop**. If the LLM outputs invalid JSON or data that fails the
|
|
120
|
+
This is a high-level wrapper that employs a **Re-asking Loop**. If the LLM outputs invalid JSON or data that fails the schema validation, the client automatically feeds the error back to the LLM and asks it to fix it (up to `maxRetries`).
|
|
121
121
|
|
|
122
|
-
**Return Type:** `Promise<
|
|
122
|
+
**Return Type:** `Promise<T>`
|
|
123
|
+
|
|
124
|
+
### Level 1: Raw JSON Schema (`promptJson`)
|
|
125
|
+
Use this if you have a standard JSON Schema object (e.g. from another library or API) and don't want to use Zod.
|
|
123
126
|
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
```typescript
|
|
128
|
+
const MySchema = {
|
|
129
|
+
type: "object",
|
|
130
|
+
properties: {
|
|
131
|
+
sentiment: { type: "string", enum: ["positive", "negative"] },
|
|
132
|
+
score: { type: "number" }
|
|
133
|
+
},
|
|
134
|
+
required: ["sentiment", "score"],
|
|
135
|
+
additionalProperties: false
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const result = await llm.promptJson(
|
|
139
|
+
[{ role: "user", content: "I love this!" }],
|
|
140
|
+
MySchema,
|
|
141
|
+
(data) => data // Optional validator function
|
|
142
|
+
);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Level 2: Zod Wrapper (`promptZod`)
|
|
146
|
+
This is syntactic sugar over `promptJson`. It converts your Zod schema to JSON Schema and automatically sets up the validator to throw formatted Zod errors for the retry loop.
|
|
147
|
+
|
|
148
|
+
**Return Type:** `Promise<z.infer<typeof Schema>>`
|
|
126
149
|
|
|
127
150
|
```typescript
|
|
128
151
|
import { z } from 'zod';
|
|
129
152
|
const UserSchema = z.object({ name: z.string(), age: z.number() });
|
|
130
153
|
|
|
131
|
-
//
|
|
154
|
+
// 1. Schema Only (Hallucinate data)
|
|
132
155
|
const user = await llm.promptZod(UserSchema);
|
|
133
|
-
// Output: { name: "Alice", age: 32 }
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
### Level 2: Extraction (Injection Shortcuts)
|
|
137
|
-
Pass context alongside the schema. This automates the "System Prompt JSON Injection".
|
|
138
156
|
|
|
139
|
-
|
|
140
|
-
// 1. Extract from String
|
|
157
|
+
// 2. Extraction (Context + Schema)
|
|
141
158
|
const email = "Meeting at 2 PM with Bob.";
|
|
142
159
|
const event = await llm.promptZod(email, z.object({ time: z.string(), who: z.string() }));
|
|
143
160
|
|
|
144
|
-
//
|
|
145
|
-
// Useful for auditing code or translations where instructions must not bleed into data.
|
|
146
|
-
const analysis = await llm.promptZod(
|
|
147
|
-
"You are a security auditor.", // Arg 1: System
|
|
148
|
-
"function dangerous() {}", // Arg 2: User Data
|
|
149
|
-
SecuritySchema // Arg 3: Schema
|
|
150
|
-
);
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
### Level 3: State & Options (History + Config)
|
|
154
|
-
Process full chat history into state, and use the **Options Object (4th Argument)** to control the internals (Models, Retries, Caching).
|
|
155
|
-
|
|
156
|
-
**Input Type:** `ZodLlmClientOptions`
|
|
157
|
-
|
|
158
|
-
```typescript
|
|
161
|
+
// 3. Full Control (History + Schema + Options)
|
|
159
162
|
const history = [
|
|
160
163
|
{ role: "user", content: "I cast Fireball." },
|
|
161
164
|
{ role: "assistant", content: "It misses." }
|
|
@@ -173,12 +176,12 @@ const gameState = await llm.promptZod(
|
|
|
173
176
|
);
|
|
174
177
|
```
|
|
175
178
|
|
|
176
|
-
### Level
|
|
177
|
-
Sometimes LLMs output data that is *almost* correct (e.g., strings for numbers). You can sanitize data before
|
|
179
|
+
### Level 3: Hooks & Pre-processing
|
|
180
|
+
Sometimes LLMs output data that is *almost* correct (e.g., strings for numbers). You can sanitize data before validation runs.
|
|
178
181
|
|
|
179
182
|
```typescript
|
|
180
183
|
const result = await llm.promptZod(MySchema, {
|
|
181
|
-
// Transform JSON before
|
|
184
|
+
// Transform JSON before validation runs
|
|
182
185
|
beforeValidation: (data) => {
|
|
183
186
|
if (data.price && typeof data.price === 'string') {
|
|
184
187
|
return { ...data, price: parseFloat(data.price) };
|
|
@@ -187,7 +190,6 @@ const result = await llm.promptZod(MySchema, {
|
|
|
187
190
|
},
|
|
188
191
|
|
|
189
192
|
// Toggle usage of 'response_format: { type: "json_object" }'
|
|
190
|
-
// Sometimes strict JSON mode is too restrictive for creative tasks
|
|
191
193
|
useResponseFormat: false
|
|
192
194
|
});
|
|
193
195
|
```
|