-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(helpers/zod): avoid import issue in certain environments (#1039)
- Loading branch information
1 parent
195fff8
commit 1e9808a
Showing
8 changed files
with
368 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.