llm-fns 1.0.9 → 1.0.11
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.
|
@@ -15,6 +15,13 @@ export type JsonSchemaLlmClientOptions = Omit<LlmPromptOptions, 'messages' | 're
|
|
|
15
15
|
* @returns The processed data to be validated.
|
|
16
16
|
*/
|
|
17
17
|
beforeValidation?: (data: any) => any;
|
|
18
|
+
/**
|
|
19
|
+
* A custom validator function.
|
|
20
|
+
* If provided, this function will be used to validate the parsed JSON data.
|
|
21
|
+
* It should throw an error if the data is invalid, or return the validated data (potentially transformed).
|
|
22
|
+
* If not provided, an AJV-based validator will be used.
|
|
23
|
+
*/
|
|
24
|
+
validator?: (data: any) => any;
|
|
18
25
|
};
|
|
19
26
|
export interface CreateJsonSchemaLlmClientParams {
|
|
20
27
|
prompt: PromptFunction;
|
|
@@ -27,3 +34,4 @@ export declare function createJsonSchemaLlmClient(params: CreateJsonSchemaLlmCli
|
|
|
27
34
|
isPromptJsonCached: (messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[], schema: Record<string, any>, options?: JsonSchemaLlmClientOptions) => Promise<boolean>;
|
|
28
35
|
};
|
|
29
36
|
export type JsonSchemaClient = ReturnType<typeof createJsonSchemaLlmClient>;
|
|
37
|
+
export type PromptJsonFunction = JsonSchemaClient['promptJson'];
|
|
@@ -146,16 +146,22 @@ ${schemaJsonString}`;
|
|
|
146
146
|
return { finalMessages, schemaJsonString, response_format };
|
|
147
147
|
}
|
|
148
148
|
async function promptJson(messages, schema, options) {
|
|
149
|
-
//
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
149
|
+
// Default validator using AJV
|
|
150
|
+
const defaultValidator = (data) => {
|
|
151
|
+
try {
|
|
152
|
+
const validate = ajv.compile(schema);
|
|
153
|
+
const valid = validate(data);
|
|
154
|
+
if (!valid) {
|
|
155
|
+
const errors = validate.errors?.map(e => `${e.instancePath} ${e.message}`).join(', ');
|
|
156
|
+
throw new Error(`AJV Validation Error: ${errors}`);
|
|
157
|
+
}
|
|
158
|
+
return data;
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
throw error;
|
|
156
162
|
}
|
|
157
|
-
return data;
|
|
158
163
|
};
|
|
164
|
+
const validator = options?.validator ?? defaultValidator;
|
|
159
165
|
const { finalMessages, schemaJsonString, response_format } = _getJsonPromptConfig(messages, schema, options);
|
|
160
166
|
const processResponse = async (llmResponseString) => {
|
|
161
167
|
let jsonData;
|
|
@@ -170,7 +176,7 @@ The response provided was not valid JSON. Please correct it.`;
|
|
|
170
176
|
throw new createLlmRetryClient_js_1.LlmRetryError(errorMessage, 'JSON_PARSE_ERROR', undefined, llmResponseString);
|
|
171
177
|
}
|
|
172
178
|
try {
|
|
173
|
-
const validatedData = await _validateOrFix(jsonData,
|
|
179
|
+
const validatedData = await _validateOrFix(jsonData, validator, schemaJsonString, options);
|
|
174
180
|
return validatedData;
|
|
175
181
|
}
|
|
176
182
|
catch (validationError) {
|
|
@@ -194,7 +200,7 @@ The response was valid JSON but did not conform to the required schema. Please r
|
|
|
194
200
|
}
|
|
195
201
|
async function isPromptJsonCached(messages, schema, options) {
|
|
196
202
|
const { finalMessages, response_format } = _getJsonPromptConfig(messages, schema, options);
|
|
197
|
-
const { maxRetries, useResponseFormat: _u, beforeValidation, ...restOptions } = options || {};
|
|
203
|
+
const { maxRetries, useResponseFormat: _u, beforeValidation, validator, ...restOptions } = options || {};
|
|
198
204
|
return isPromptCached({
|
|
199
205
|
messages: finalMessages,
|
|
200
206
|
response_format,
|
|
@@ -26,3 +26,5 @@ export declare function createZodLlmClient(params: CreateZodLlmClientParams): {
|
|
|
26
26
|
<T extends ZodTypeAny>(mainInstruction: string, userMessagePayload: string | OpenAI.Chat.Completions.ChatCompletionContentPart[], dataExtractionSchema: T, options?: ZodLlmClientOptions): Promise<boolean>;
|
|
27
27
|
};
|
|
28
28
|
};
|
|
29
|
+
export type ZodLlmClient = ReturnType<typeof createZodLlmClient>;
|
|
30
|
+
export type PromptZodFunction = ZodLlmClient['promptZod'];
|
|
@@ -93,11 +93,14 @@ function createZodLlmClient(params) {
|
|
|
93
93
|
const schema = z.toJSONSchema(dataExtractionSchema, {
|
|
94
94
|
unrepresentable: 'any'
|
|
95
95
|
});
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
96
|
+
const zodValidator = (data) => {
|
|
97
|
+
return dataExtractionSchema.parse(data);
|
|
98
|
+
};
|
|
99
|
+
const result = await jsonSchemaClient.promptJson(messages, schema, {
|
|
100
|
+
...options,
|
|
101
|
+
validator: zodValidator
|
|
102
|
+
});
|
|
103
|
+
return result;
|
|
101
104
|
}
|
|
102
105
|
async function isPromptZodCached(arg1, arg2, arg3, arg4) {
|
|
103
106
|
const { messages, dataExtractionSchema, options } = normalizeZodArgs(arg1, arg2, arg3, arg4);
|
package/dist/llmFactory.d.ts
CHANGED