Skip to content

Commit

Permalink
Merge pull request #87 from parea-ai/PAI-1444-openai-structured-outpu…
Browse files Browse the repository at this point in the history
…ts-ts

feat(struc): add openai struc outputs
  • Loading branch information
jalexanderII authored Aug 16, 2024
2 parents 5b0f692 + 2788fcb commit 51d5d79
Show file tree
Hide file tree
Showing 5 changed files with 834 additions and 719 deletions.
92 changes: 92 additions & 0 deletions cookbook/tracing_with_openai_structured_outputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as dotenv from 'dotenv';
import { Parea, patchOpenAI } from '../src';
import OpenAI from 'openai';
import { z } from 'zod';
import { zodResponseFormat } from 'openai/helpers/zod';

dotenv.config();

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

// needed for tracing
new Parea(process.env.PAREA_API_KEY);

// Patch OpenAI to add trace logs
patchOpenAI(openai);

const CalendarEvent = z.object({
name: z.string(),
date: z.string(),
participants: z.array(z.string()),
});

const main = async () => {
const completion = await openai.beta.chat.completions.parse({
model: 'gpt-4o-2024-08-06',
messages: [
{ role: 'system', content: 'Extract the event information.' },
{ role: 'user', content: 'Alice and Bob are going to a science fair on Friday.' },
],
response_format: zodResponseFormat(CalendarEvent, 'event'),
});

const event = completion.choices[0].message.parsed;

console.log(event);
};

type Step = {
explanation: string;
output: string;
};

type Solution = {
steps: Step[];
final_answer: string;
};

const main2 = async () => {
const response = await openai.chat.completions.create({
model: 'gpt-4o-2024-08-06',
messages: [
{ role: 'system', content: 'You are a helpful math tutor. Guide the user through the solution step by step.' },
{ role: 'user', content: 'how can I solve 8x + 7 = -23' },
],
response_format: {
type: 'json_schema',
json_schema: {
name: 'math_response',
schema: {
type: 'object',
properties: {
steps: {
type: 'array',
items: {
type: 'object',
properties: {
explanation: { type: 'string' },
output: { type: 'string' },
},
required: ['explanation', 'output'],
additionalProperties: false,
},
},
final_answer: { type: 'string' },
},
required: ['steps', 'final_answer'],
additionalProperties: false,
},
strict: true,
},
},
});

// console.log(response.choices[0].message.content);
const solution = JSON.parse(response.choices[0].message.content || '') as Solution;
console.log(solution);
};

main();
main2();
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@
"dependencies": {
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"axios": "^1.7.2",
"axios": "^1.7.4",
"axios-retry": "^3.9.1",
"dotenv": "^16.4.5",
"moment-timezone": "^0.5.45",
"openai": ">=4.53.0 <4.55.0",
"openai": "^4.56.0",
"semantic-release": "^21.1.2",
"ts-node": "^10.9.2",
"uuid": "^9.0.1"
},
"devDependencies": {
"@babel/core": "^7.24.9",
"@babel/preset-env": "^7.24.8",
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.3",
"@babel/preset-typescript": "^7.24.7",
"@commitlint/cli": "^17.8.1",
"@commitlint/config-conventional": "^17.8.1",
"@langchain/core": "^0.1.63",
"@langchain/openai": "^0.0.14",
"@tsconfig/recommended": "^1.0.7",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.12",
"@types/node": "^20.14.15",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"babel-jest": "^29.7.0",
Expand All @@ -56,8 +56,9 @@
"lint-staged": "^14.0.1",
"prettier": "3.0.3",
"semantic-release-pnpm": "^1.0.2",
"ts-jest": "^29.2.3",
"typescript": "^5.5.4"
"ts-jest": "^29.2.4",
"typescript": "^5.5.4",
"zod": "^3.23.8"
},
"scripts": {
"build": "tsc",
Expand Down
Loading

0 comments on commit 51d5d79

Please sign in to comment.