Skip to content

Commit

Permalink
Update OpenAI completion chunk types to OpenAI SDK 4.16.1 (#715)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Leiter <max.leiter@vercel.com>
  • Loading branch information
lgrammel and MaxLeiter committed Nov 7, 2023
1 parent b59e0d8 commit 2c8d4bd
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-keys-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

Support openai@4.16.0 and later
2 changes: 1 addition & 1 deletion examples/next-fireworks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"ai": "2.2.20",
"next": "13.4.12",
"openai": "4.11.0",
"openai": "4.16.1",
"react": "18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/next-openai-rate-limits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@vercel/kv": "^0.2.2",
"ai": "2.2.20",
"next": "13.4.12",
"openai": "4.11.0",
"openai": "4.16.1",
"react": "18.2.0",
"react-dom": "^18.2.0",
"sonner": "^0.6.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/next-openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"ai": "2.2.20",
"next": "13.4.12",
"openai": "4.11.0",
"openai": "4.16.1",
"react": "18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/nuxt-openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@vue/shared": "^3.3.4",
"ai": "2.2.20",
"nuxt": "^3.6.5",
"openai": "4.11.0",
"openai": "4.16.1",
"tailwindcss": "^3.3.3",
"ufo": "^1.2.0",
"unctx": "^2.3.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/solidstart-openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@solidjs/meta": "^0.28.2",
"@solidjs/router": "^0.8.2",
"ai": "2.2.20",
"openai": "4.11.0",
"openai": "4.16.1",
"solid-js": "^1.7.2",
"solid-start": "^0.2.26",
"undici": "^5.15.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/sveltekit-openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"dependencies": {
"openai": "4.11.0",
"openai": "4.16.1",
"ai": "2.2.20"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"eslint-config-vercel-ai": "workspace:*",
"jest": "29.2.1",
"langchain": "0.0.172",
"openai": "4.16.1",
"ts-jest": "29.0.3",
"tsup": "^6.7.0",
"typescript": "5.1.3"
Expand Down
20 changes: 20 additions & 0 deletions packages/core/streams/openai-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
StreamingTextResponse,
experimental_StreamData,
} from '.';
import OpenAI from 'openai';
import { createClient } from '../tests/utils/mock-client';
import { setup } from '../tests/utils/mock-service';

Expand All @@ -13,6 +14,25 @@ describe('OpenAIStream', () => {
});
afterAll(async () => server.teardown());

// deactivated to only test types
xit('should not throw type errors', async () => {
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-16k',
stream: true,
temperature: 0.0,
messages: [
{ role: 'system', content: 'You are a helpful yada yada' },
{ role: 'user', content: '' },
],
});

const stream = OpenAIStream(response);
});

it('should be able to parse SSE and receive the streamed response', async () => {
const stream = OpenAIStream(
await fetch(server.api, {
Expand Down
49 changes: 47 additions & 2 deletions packages/core/streams/openai-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@ interface ChatCompletionChunk {

// https://github.com/openai/openai-node/blob/07b3504e1c40fd929f4aae1651b83afc19e3baf8/src/resources/chat/completions.ts#L43-L49
// Updated for https://github.com/openai/openai-node/commit/f10c757d831d90407ba47b4659d9cd34b1a35b1d
// Updated to https://github.com/openai/openai-node/commit/84b43280089eacdf18f171723591856811beddce
interface ChatCompletionChunkChoice {
delta: ChoiceDelta;
finish_reason: 'stop' | 'length' | 'function_call' | 'content_filter' | null;
finish_reason:
| 'stop'
| 'length'
| 'tool_calls'
| 'content_filter'
| 'function_call'
| null;
index: number;
}

// https://github.com/openai/openai-node/blob/07b3504e1c40fd929f4aae1651b83afc19e3baf8/src/resources/chat/completions.ts#L123-L139
// Updated to https://github.com/openai/openai-node/commit/84b43280089eacdf18f171723591856811beddce
interface ChoiceDelta {
/**
* The contents of the chunk message.
Expand All @@ -87,7 +95,44 @@ interface ChoiceDelta {
/**
* The role of the author of this message.
*/
role?: 'system' | 'user' | 'assistant' | 'function';
role?: 'system' | 'user' | 'assistant' | 'tool';

tool_calls?: Array<DeltaToolCall>;
}

// From https://github.com/openai/openai-node/blob/master/src/resources/chat/completions.ts
// Updated to https://github.com/openai/openai-node/commit/84b43280089eacdf18f171723591856811beddce
interface DeltaToolCall {
index: number;

/**
* The ID of the tool call.
*/
id?: string;

function?: ToolCallFunction;

/**
* The type of the tool. Currently, only `function` is supported.
*/
type?: 'function';
}

// From https://github.com/openai/openai-node/blob/master/src/resources/chat/completions.ts
// Updated to https://github.com/openai/openai-node/commit/84b43280089eacdf18f171723591856811beddce
interface ToolCallFunction {
/**
* The arguments to call the function with, as generated by the model in JSON
* format. Note that the model does not always generate valid JSON, and may
* hallucinate parameters not defined by your function schema. Validate the
* arguments in your code before calling your function.
*/
arguments?: string;

/**
* The name of the function to call.
*/
name?: string;
}

/**
Expand Down
33 changes: 18 additions & 15 deletions pnpm-lock.yaml

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

0 comments on commit 2c8d4bd

Please sign in to comment.