zod.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.zodResponsesFunction = exports.zodFunction = exports.zodTextFormat = exports.zodResponseFormat = void 0;
  4. const parser_1 = require("../lib/parser.js");
  5. const zod_to_json_schema_1 = require("../_vendor/zod-to-json-schema/index.js");
  6. const ResponsesParser_1 = require("../lib/ResponsesParser.js");
  7. function zodToJsonSchema(schema, options) {
  8. return (0, zod_to_json_schema_1.zodToJsonSchema)(schema, {
  9. openaiStrictMode: true,
  10. name: options.name,
  11. nameStrategy: 'duplicate-ref',
  12. $refStrategy: 'extract-to-root',
  13. nullableStrategy: 'property',
  14. });
  15. }
  16. /**
  17. * Creates a chat completion `JSONSchema` response format object from
  18. * the given Zod schema.
  19. *
  20. * If this is passed to the `.parse()`, `.stream()` or `.runTools()`
  21. * chat completion methods then the response message will contain a
  22. * `.parsed` property that is the result of parsing the content with
  23. * the given Zod object.
  24. *
  25. * ```ts
  26. * const completion = await client.beta.chat.completions.parse({
  27. * model: 'gpt-4o-2024-08-06',
  28. * messages: [
  29. * { role: 'system', content: 'You are a helpful math tutor.' },
  30. * { role: 'user', content: 'solve 8x + 31 = 2' },
  31. * ],
  32. * response_format: zodResponseFormat(
  33. * z.object({
  34. * steps: z.array(z.object({
  35. * explanation: z.string(),
  36. * answer: z.string(),
  37. * })),
  38. * final_answer: z.string(),
  39. * }),
  40. * 'math_answer',
  41. * ),
  42. * });
  43. * const message = completion.choices[0]?.message;
  44. * if (message?.parsed) {
  45. * console.log(message.parsed);
  46. * console.log(message.parsed.final_answer);
  47. * }
  48. * ```
  49. *
  50. * This can be passed directly to the `.create()` method but will not
  51. * result in any automatic parsing, you'll have to parse the response yourself.
  52. */
  53. function zodResponseFormat(zodObject, name, props) {
  54. return (0, parser_1.makeParseableResponseFormat)({
  55. type: 'json_schema',
  56. json_schema: {
  57. ...props,
  58. name,
  59. strict: true,
  60. schema: zodToJsonSchema(zodObject, { name }),
  61. },
  62. }, (content) => zodObject.parse(JSON.parse(content)));
  63. }
  64. exports.zodResponseFormat = zodResponseFormat;
  65. function zodTextFormat(zodObject, name, props) {
  66. return (0, parser_1.makeParseableTextFormat)({
  67. type: 'json_schema',
  68. ...props,
  69. name,
  70. strict: true,
  71. schema: zodToJsonSchema(zodObject, { name }),
  72. }, (content) => zodObject.parse(JSON.parse(content)));
  73. }
  74. exports.zodTextFormat = zodTextFormat;
  75. /**
  76. * Creates a chat completion `function` tool that can be invoked
  77. * automatically by the chat completion `.runTools()` method or automatically
  78. * parsed by `.parse()` / `.stream()`.
  79. */
  80. function zodFunction(options) {
  81. // @ts-expect-error TODO
  82. return (0, parser_1.makeParseableTool)({
  83. type: 'function',
  84. function: {
  85. name: options.name,
  86. parameters: zodToJsonSchema(options.parameters, { name: options.name }),
  87. strict: true,
  88. ...(options.description ? { description: options.description } : undefined),
  89. },
  90. }, {
  91. callback: options.function,
  92. parser: (args) => options.parameters.parse(JSON.parse(args)),
  93. });
  94. }
  95. exports.zodFunction = zodFunction;
  96. function zodResponsesFunction(options) {
  97. return (0, ResponsesParser_1.makeParseableResponseTool)({
  98. type: 'function',
  99. name: options.name,
  100. parameters: zodToJsonSchema(options.parameters, { name: options.name }),
  101. strict: true,
  102. ...(options.description ? { description: options.description } : undefined),
  103. }, {
  104. callback: options.function,
  105. parser: (args) => options.parameters.parse(JSON.parse(args)),
  106. });
  107. }
  108. exports.zodResponsesFunction = zodResponsesFunction;
  109. //# sourceMappingURL=zod.js.map