CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
zod.mjs102 linesDownload Raw Back to helpers
1import { makeParseableResponseFormat, makeParseableTextFormat, makeParseableTool, } from "../lib/parser.mjs";2import { zodToJsonSchema as _zodToJsonSchema } from "../_vendor/zod-to-json-schema/index.mjs";3import { makeParseableResponseTool } from "../lib/ResponsesParser.mjs";4function zodToJsonSchema(schema, options) {5    return _zodToJsonSchema(schema, {6        openaiStrictMode: true,7        name: options.name,8        nameStrategy: 'duplicate-ref',9        $refStrategy: 'extract-to-root',10        nullableStrategy: 'property',11    });12}13/**14 * Creates a chat completion `JSONSchema` response format object from15 * the given Zod schema.16 *17 * If this is passed to the `.parse()`, `.stream()` or `.runTools()`18 * chat completion methods then the response message will contain a19 * `.parsed` property that is the result of parsing the content with20 * the given Zod object.21 *22 * ```ts23 * const completion = await client.chat.completions.parse({24 *    model: 'gpt-4o-2024-08-06',25 *    messages: [26 *      { role: 'system', content: 'You are a helpful math tutor.' },27 *      { role: 'user', content: 'solve 8x + 31 = 2' },28 *    ],29 *    response_format: zodResponseFormat(30 *      z.object({31 *        steps: z.array(z.object({32 *          explanation: z.string(),33 *          answer: z.string(),34 *        })),35 *        final_answer: z.string(),36 *      }),37 *      'math_answer',38 *    ),39 *  });40 *  const message = completion.choices[0]?.message;41 *  if (message?.parsed) {42 *    console.log(message.parsed);43 *    console.log(message.parsed.final_answer);44 * }45 * ```46 *47 * This can be passed directly to the `.create()` method but will not48 * result in any automatic parsing, you'll have to parse the response yourself.49 */50export function zodResponseFormat(zodObject, name, props) {51    return makeParseableResponseFormat({52        type: 'json_schema',53        json_schema: {54            ...props,55            name,56            strict: true,57            schema: zodToJsonSchema(zodObject, { name }),58        },59    }, (content) => zodObject.parse(JSON.parse(content)));60}61export function zodTextFormat(zodObject, name, props) {62    return makeParseableTextFormat({63        type: 'json_schema',64        ...props,65        name,66        strict: true,67        schema: zodToJsonSchema(zodObject, { name }),68    }, (content) => zodObject.parse(JSON.parse(content)));69}70/**71 * Creates a chat completion `function` tool that can be invoked72 * automatically by the chat completion `.runTools()` method or automatically73 * parsed by `.parse()` / `.stream()`.74 */75export function zodFunction(options) {76    // @ts-expect-error TODO77    return makeParseableTool({78        type: 'function',79        function: {80            name: options.name,81            parameters: zodToJsonSchema(options.parameters, { name: options.name }),82            strict: true,83            ...(options.description ? { description: options.description } : undefined),84        },85    }, {86        callback: options.function,87        parser: (args) => options.parameters.parse(JSON.parse(args)),88    });89}90export function zodResponsesFunction(options) {91    return makeParseableResponseTool({92        type: 'function',93        name: options.name,94        parameters: zodToJsonSchema(options.parameters, { name: options.name }),95        strict: true,96        ...(options.description ? { description: options.description } : undefined),97    }, {98        callback: options.function,99        parser: (args) => options.parameters.parse(JSON.parse(args)),100    });101}102//# sourceMappingURL=zod.mjs.map
basant307/AI_Governance_Project · CoolFace