Skip to content

Commit

Permalink
Update to strucutred outputs beta
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Aug 11, 2024
1 parent fbb384c commit 0085478
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 87 deletions.
29 changes: 14 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 50 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
{
"name": "@jakub.knejzlik/openai-toolkit",
"version": "0.0.4",
"description": "A lightweight wrapper library for simplifying OpenAI API interactions.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "vitest",
"test:watch": "vitest --watch",
"prepublishOnly": "tsc",
"build": "tsc",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/jakubknejzlik/openai-toolkit.git"
},
"keywords": [
"sql",
"query",
"builder",
"serialization"
],
"author": "Jakub Knejzlik",
"license": "MIT",
"bugs": {
"url": "https://github.com/jakubknejzlik/openai-toolkit/issues"
},
"homepage": "https://github.com/jakubknejzlik/openai-toolkit#readme",
"peerDependencies": {
"openai": "^4.50.0"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"jest": "^29.7.0",
"openai": "^4.50.0",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-live": "^4.1.5",
"storybook": "^8.1.6",
"ts-jest": "^29.1.1",
"vite": "^4.5.1",
"vitest": "^1.6.0"
},
"dependencies": {
"p-retry": "^6.2.0",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.0"
}
"name": "@jakub.knejzlik/openai-toolkit",
"version": "0.0.4",
"description": "A lightweight wrapper library for simplifying OpenAI API interactions.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "vitest",
"test:watch": "vitest --watch",
"prepublishOnly": "tsc",
"build": "tsc",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/jakubknejzlik/openai-toolkit.git"
},
"keywords": [
"sql",
"query",
"builder",
"serialization"
],
"author": "Jakub Knejzlik",
"license": "MIT",
"bugs": {
"url": "https://github.com/jakubknejzlik/openai-toolkit/issues"
},
"homepage": "https://github.com/jakubknejzlik/openai-toolkit#readme",
"peerDependencies": {
"openai": "^4.55.4"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"jest": "^29.7.0",
"openai": "^4.55.4",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-live": "^4.1.5",
"storybook": "^8.1.6",
"ts-jest": "^29.1.1",
"vite": "^4.5.1",
"vitest": "^1.6.0"
},
"dependencies": {
"p-retry": "^6.2.0",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.0"
}
}
22 changes: 10 additions & 12 deletions src/completions/completion-with-functions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import OpenAI from 'openai'
import type { z } from 'zod'
import { z } from 'zod'
import type { ChatCompletionFunction } from '../function'

import zodToJsonSchema from 'zod-to-json-schema'
import { zodFunction } from 'openai/helpers/zod'

type CompletionOpts = Partial<
Omit<OpenAI.ChatCompletionCreateParams, 'functions' | 'tools'>
Expand All @@ -22,15 +23,11 @@ export type CompletionOptsWithFunctionOpts = CompletionOpts & {
export const functionToOpenAIChatCompletionTool = <T extends z.ZodRawShape>(
fn: ChatCompletionFunction<T>
): OpenAI.ChatCompletionTool => {
const params = fn.parameters ? zodToJsonSchema(fn.parameters) : undefined
return {
type: 'function',
function: {
name: fn.name,
description: fn.description,
parameters: params
}
}
return zodFunction({
name: fn.name,
description: fn.description,
parameters: fn.parameters
})
}

export const completionWithFunctions = async (
Expand All @@ -55,13 +52,14 @@ export const completionWithFunctions = async (
_messages.push({ role: 'user', content: prompt })
}

const response = await client.chat.completions.create({
const response = await client.beta.chat.completions.parse({
model: model ?? 'gpt-4o-mini',
messages: _messages,
tools: functions?.map(functionToOpenAIChatCompletionTool),
...rest,
stream: false
})
console.log('???', JSON.stringify(response, null, 2))

let message = response?.choices?.[0]?.message

Expand All @@ -88,7 +86,7 @@ export const completionWithFunctions = async (
}
}

if (message?.tool_calls) {
if (message?.tool_calls && message?.tool_calls.length > 0) {
let toolCallResults: {
tool_call_id: string
output: string
Expand Down
17 changes: 7 additions & 10 deletions src/completions/completion-with-json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { z } from 'zod'
import type { ChatCompletionFunction } from '../function'
import { zodFunction, zodResponseFormat } from 'openai/helpers/zod'

import zodToJsonSchema from 'zod-to-json-schema'
import {
Expand All @@ -17,15 +18,11 @@ export type CompletionOptsWithJsonResponse<T extends z.ZodRawShape> =
export const functionToOpenAIChatCompletionTool = <T extends z.ZodRawShape>(
fn: ChatCompletionFunction<T>
): OpenAI.ChatCompletionTool => {
const params = fn.parameters ? zodToJsonSchema(fn.parameters) : undefined
return {
type: 'function',
function: {
name: fn.name,
description: fn.description,
parameters: params
}
}
return zodFunction({
name: fn.name,
description: fn.description,
parameters: fn.parameters
})
}

type Response<T extends z.ZodRawShape> = {
Expand All @@ -46,7 +43,7 @@ export const completionWithJsonResponse = async <T extends z.ZodRawShape>({
const _prompt = `JSON schema:\n${responseObjectSchema}\n\n${prompt ? `${prompt}\n\n` : ''}\nYou MUST answer with a JSON object that matches the JSON schema above.`
const res = await completionWithFunctions({
...rest,
response_format: { type: 'json_object' },
response_format: zodResponseFormat(responseObject, 'event'),
prompt: _prompt
})

Expand Down

0 comments on commit 0085478

Please sign in to comment.