zod.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { makeParseableResponseFormat, makeParseableTextFormat, makeParseableTool, } from "../lib/parser.mjs";
  2. import { zodToJsonSchema as _zodToJsonSchema } from "../_vendor/zod-to-json-schema/index.mjs";
  3. import { makeParseableResponseTool } from "../lib/ResponsesParser.mjs";
  4. function 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 from
  15. * 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 a
  19. * `.parsed` property that is the result of parsing the content with
  20. * the given Zod object.
  21. *
  22. * ```ts
  23. * const completion = await client.beta.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 not
  48. * result in any automatic parsing, you'll have to parse the response yourself.
  49. */
  50. export 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. }
  61. export 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 invoked
  72. * automatically by the chat completion `.runTools()` method or automatically
  73. * parsed by `.parse()` / `.stream()`.
  74. */
  75. export function zodFunction(options) {
  76. // @ts-expect-error TODO
  77. 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. }
  90. export 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