From e238daa7c12f3fb13369f58b9d405365f5efcc8f Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Wed, 4 Sep 2024 20:46:58 +0100 Subject: [PATCH] fix(helpers/zod): avoid import issue in certain environments (#1039) --- ecosystem-tests/cli.ts | 5 + ecosystem-tests/node-ts-es2020/index.ts | 36 ++++ .../node-ts-es2020/package-lock.json | 193 ++++++++++++++++++ ecosystem-tests/node-ts-es2020/package.json | 16 ++ .../node-ts-es2020/tsconfig.base.json | 37 ++++ ecosystem-tests/node-ts-es2020/tsconfig.json | 18 ++ .../node-ts-es2020/tsconfig.nodenext.json | 55 +++++ src/helpers/zod.ts | 16 +- 8 files changed, 368 insertions(+), 8 deletions(-) create mode 100644 ecosystem-tests/node-ts-es2020/index.ts create mode 100644 ecosystem-tests/node-ts-es2020/package-lock.json create mode 100644 ecosystem-tests/node-ts-es2020/package.json create mode 100644 ecosystem-tests/node-ts-es2020/tsconfig.base.json create mode 100644 ecosystem-tests/node-ts-es2020/tsconfig.json create mode 100644 ecosystem-tests/node-ts-es2020/tsconfig.nodenext.json diff --git a/ecosystem-tests/cli.ts b/ecosystem-tests/cli.ts index e315ccd6c..2d9702112 100644 --- a/ecosystem-tests/cli.ts +++ b/ecosystem-tests/cli.ts @@ -25,6 +25,11 @@ const projectRunners = { 'node-ts-esm': defaultNodeRunner, 'node-ts-esm-web': defaultNodeRunner, 'node-ts-esm-auto': defaultNodeRunner, + 'node-ts-es2020': async () => { + await installPackage(); + await run('npm', ['run', 'tsc']); + await run('npm', ['run', 'main']); + }, 'node-js': async () => { await installPackage(); await run('node', ['test.js']); diff --git a/ecosystem-tests/node-ts-es2020/index.ts b/ecosystem-tests/node-ts-es2020/index.ts new file mode 100644 index 000000000..d92cc2720 --- /dev/null +++ b/ecosystem-tests/node-ts-es2020/index.ts @@ -0,0 +1,36 @@ +import { zodResponseFormat } from 'openai/helpers/zod'; +import OpenAI from 'openai/index'; +import { z } from 'zod'; + +const Step = z.object({ + explanation: z.string(), + output: z.string(), +}); + +const MathResponse = z.object({ + steps: z.array(Step), + final_answer: z.string(), +}); + +async function main() { + const client = new OpenAI(); + + const completion = await client.beta.chat.completions.parse({ + model: 'gpt-4o-2024-08-06', + messages: [ + { role: 'system', content: 'You are a helpful math tutor.' }, + { role: 'user', content: 'solve 8x + 31 = 2' }, + ], + response_format: zodResponseFormat(MathResponse, 'math_response'), + }); + + console.dir(completion, { depth: 5 }); + + const message = completion.choices[0]?.message; + if (message?.parsed) { + console.log(message.parsed.steps); + console.log(`answer: ${message.parsed.final_answer}`); + } +} + +main(); diff --git a/ecosystem-tests/node-ts-es2020/package-lock.json b/ecosystem-tests/node-ts-es2020/package-lock.json new file mode 100644 index 000000000..5ae1d5aa0 --- /dev/null +++ b/ecosystem-tests/node-ts-es2020/package-lock.json @@ -0,0 +1,193 @@ +{ + "name": "node-ts-es2020", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-ts-es2020", + "version": "1.0.0", + "dependencies": { + "ts-node": "^10.9.2", + "zod": "^3.23.8" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz", + "integrity": "sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==" + }, + "node_modules/@tsconfig/node10": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", + "integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==" + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==" + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==" + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==" + }, + "node_modules/@types/node": { + "version": "20.4.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.4.2.tgz", + "integrity": "sha512-Dd0BYtWgnWJKwO1jkmTrzofjK2QXXcai0dmtzvIBhcA+RsG5h8R3xlyta0kGOZRNfL9GuRtb1knmPEhQrePCEw==", + "peer": true + }, + "node_modules/acorn": { + "version": "8.10.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.10.0.tgz", + "integrity": "sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw==", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/acorn-walk": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz", + "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==" + }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==" + }, + "node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==" + }, + "node_modules/ts-node": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", + "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/typescript": { + "version": "4.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz", + "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==", + "peer": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==" + }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "engines": { + "node": ">=6" + } + }, + "node_modules/zod": { + "version": "3.23.8", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", + "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + } + } +} diff --git a/ecosystem-tests/node-ts-es2020/package.json b/ecosystem-tests/node-ts-es2020/package.json new file mode 100644 index 000000000..ea7e488f5 --- /dev/null +++ b/ecosystem-tests/node-ts-es2020/package.json @@ -0,0 +1,16 @@ +{ + "name": "node-ts-es2020", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "tsc": "tsc && tsc -p tsconfig.nodenext.json && tsc -p node_modules/openai/src/tsconfig.json", + "main": "ts-node index.ts" + }, + "dependencies": { + "ts-node": "^10.9.2", + "zod": "^3.23.8" + }, + "overrides": { + "@types/node": "20.4.2" + } +} diff --git a/ecosystem-tests/node-ts-es2020/tsconfig.base.json b/ecosystem-tests/node-ts-es2020/tsconfig.base.json new file mode 100644 index 000000000..8edad9422 --- /dev/null +++ b/ecosystem-tests/node-ts-es2020/tsconfig.base.json @@ -0,0 +1,37 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "commonjs", + "lib": ["es2020", "dom"], + "allowJs": false, + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "removeComments": true, + "forceConsistentCasingInFileNames": true, + "downlevelIteration": true, + "strict": true, + "moduleResolution": "node", + "paths": {}, + "typeRoots": ["node_modules/@types"], + "types": ["node"], + "allowSyntheticDefaultImports": false, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "resolveJsonModule": true, + "incremental": true, + "strictBindCallApply": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "strictPropertyInitialization": true, + "noImplicitAny": true, + "noImplicitThis": true, + "noImplicitReturns": true, + "noUnusedParameters": true, + "noUnusedLocals": true, + "noFallthroughCasesInSwitch": true, + "preserveSymlinks": true, + "suppressImplicitAnyIndexErrors": true + }, + "exclude": ["node_modules"] +} diff --git a/ecosystem-tests/node-ts-es2020/tsconfig.json b/ecosystem-tests/node-ts-es2020/tsconfig.json new file mode 100644 index 000000000..aa3b68f53 --- /dev/null +++ b/ecosystem-tests/node-ts-es2020/tsconfig.json @@ -0,0 +1,18 @@ +{ + "extends": "./tsconfig.base.json", + "ts-node": { + "swc": true, + "transpileOnly": true + }, + "compilerOptions": { + "declaration": false, + "declarationMap": false, + "allowJs": true, + "checkJs": false, + "outDir": "./dist", + "baseUrl": "./", + "types": ["node", "jest"], + "paths": {} + }, + "include": ["index.ts", "tsconfig.json", "jest.config.ts", ".eslintrc.js"] +} diff --git a/ecosystem-tests/node-ts-es2020/tsconfig.nodenext.json b/ecosystem-tests/node-ts-es2020/tsconfig.nodenext.json new file mode 100644 index 000000000..97df071fb --- /dev/null +++ b/ecosystem-tests/node-ts-es2020/tsconfig.nodenext.json @@ -0,0 +1,55 @@ +{ + "include": ["tests/*.ts", "index.ts"], + "exclude": ["tests/*-shim-errors.ts"], + + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + /* Projects */ + "incremental": true, + + /* Language and Environment */ + "target": "ES2015", + "lib": ["ES2015"], + "jsx": "react", + + /* Modules */ + "module": "commonjs", + "rootDir": "./", + "moduleResolution": "NodeNext", + "baseUrl": "./", + "paths": { + "~/*": ["*"] + }, + "resolveJsonModule": true, + "composite": true, + + /* Emit */ + "outDir": "node_modules", + "noEmit": true, + + /* Interop Constraints */ + "isolatedModules": true, + "allowSyntheticDefaultImports": true, + /* "esModuleInterop": true, */ + "forceConsistentCasingInFileNames": true, + "allowJs": true, + "checkJs": true, + + /* Experimental Features */ + "experimentalDecorators": true, + + /* Type Checking */ + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + "noUncheckedIndexedAccess": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "skipLibCheck": false + } +} diff --git a/src/helpers/zod.ts b/src/helpers/zod.ts index 463ef588c..99b9eb4b0 100644 --- a/src/helpers/zod.ts +++ b/src/helpers/zod.ts @@ -1,5 +1,5 @@ -import { ResponseFormatJSONSchema } from 'openai/resources'; -import type z from 'zod'; +import { ResponseFormatJSONSchema } from '../resources/index'; +import type { infer as zodInfer, ZodType } from 'zod'; import { AutoParseableResponseFormat, AutoParseableTool, @@ -8,7 +8,7 @@ import { } from '../lib/parser'; import { zodToJsonSchema as _zodToJsonSchema } from '../_vendor/zod-to-json-schema'; -function zodToJsonSchema(schema: z.ZodType, options: { name: string }): Record { +function zodToJsonSchema(schema: ZodType, options: { name: string }): Record { return _zodToJsonSchema(schema, { openaiStrictMode: true, name: options.name, @@ -55,11 +55,11 @@ function zodToJsonSchema(schema: z.ZodType, options: { name: string }): Record( +export function zodResponseFormat( zodObject: ZodInput, name: string, props?: Omit, -): AutoParseableResponseFormat> { +): AutoParseableResponseFormat> { return makeParseableResponseFormat( { type: 'json_schema', @@ -79,15 +79,15 @@ export function zodResponseFormat( * automatically by the chat completion `.runTools()` method or automatically * parsed by `.parse()` / `.stream()`. */ -export function zodFunction(options: { +export function zodFunction(options: { name: string; parameters: Parameters; - function?: ((args: z.infer) => unknown | Promise) | undefined; + function?: ((args: zodInfer) => unknown | Promise) | undefined; description?: string | undefined; }): AutoParseableTool<{ arguments: Parameters; name: string; - function: (args: z.infer) => unknown; + function: (args: zodInfer) => unknown; }> { // @ts-expect-error TODO return makeParseableTool(