-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathopenai-stream.ts
725 lines (662 loc) · 23.1 KB
/
openai-stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
import { formatStreamPart } from '../shared/stream-parts';
import {
CreateMessage,
FunctionCall,
JSONValue,
ToolCall,
} from '../shared/types';
import { createChunkDecoder } from '../shared/utils';
import {
AIStream,
trimStartOfStreamHelper,
type AIStreamCallbacksAndOptions,
FunctionCallPayload,
readableFromAsyncIterable,
createCallbacksTransformer,
ToolCallPayload,
} from './ai-stream';
import { AzureChatCompletions } from './azure-openai-types';
import { createStreamDataTransformer } from './stream-data';
export type OpenAIStreamCallbacks = AIStreamCallbacksAndOptions & {
/**
* @example
* ```js
* const response = await openai.chat.completions.create({
* model: 'gpt-3.5-turbo-0613',
* stream: true,
* messages,
* functions,
* })
*
* const stream = OpenAIStream(response, {
* experimental_onFunctionCall: async (functionCallPayload, createFunctionCallMessages) => {
* // ... run your custom logic here
* const result = await myFunction(functionCallPayload)
*
* // Ask for another completion, or return a string to send to the client as an assistant message.
* return await openai.chat.completions.create({
* model: 'gpt-3.5-turbo-0613',
* stream: true,
* // Append the relevant "assistant" and "function" call messages
* messages: [...messages, ...createFunctionCallMessages(result)],
* functions,
* })
* }
* })
* ```
*/
experimental_onFunctionCall?: (
functionCallPayload: FunctionCallPayload,
createFunctionCallMessages: (
functionCallResult: JSONValue,
) => CreateMessage[],
) => Promise<
Response | undefined | void | string | AsyncIterableOpenAIStreamReturnTypes
>;
/**
* @example
* ```js
* const response = await openai.chat.completions.create({
* model: 'gpt-3.5-turbo-1106', // or gpt-4-1106-preview
* stream: true,
* messages,
* tools,
* tool_choice: "auto", // auto is default, but we'll be explicit
* })
*
* const stream = OpenAIStream(response, {
* experimental_onToolCall: async (toolCallPayload, appendToolCallMessages) => {
* let messages: CreateMessage[] = []
* // There might be multiple tool calls, so we need to iterate through them
* for (const tool of toolCallPayload.tools) {
* // ... run your custom logic here
* const result = await myFunction(tool.function)
* // Append the relevant "assistant" and "tool" call messages
* appendToolCallMessage({tool_call_id:tool.id, function_name:tool.function.name, tool_call_result:result})
* }
* // Ask for another completion, or return a string to send to the client as an assistant message.
* return await openai.chat.completions.create({
* model: 'gpt-3.5-turbo-1106', // or gpt-4-1106-preview
* stream: true,
* // Append the results messages, calling appendToolCallMessage without
* // any arguments will jsut return the accumulated messages
* messages: [...messages, ...appendToolCallMessage()],
* tools,
* tool_choice: "auto", // auto is default, but we'll be explicit
* })
* }
* })
* ```
*/
experimental_onToolCall?: (
toolCallPayload: ToolCallPayload,
appendToolCallMessage: (result?: {
tool_call_id: string;
function_name: string;
tool_call_result: JSONValue;
}) => CreateMessage[],
) => Promise<
Response | undefined | void | string | AsyncIterableOpenAIStreamReturnTypes
>;
};
// https://github.com/openai/openai-node/blob/07b3504e1c40fd929f4aae1651b83afc19e3baf8/src/resources/chat/completions.ts#L28-L40
interface ChatCompletionChunk {
id: string;
choices: Array<ChatCompletionChunkChoice>;
created: number;
model: string;
object: string;
}
// 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'
| '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.
*/
content?: string | null;
/**
* The name and arguments of a function that should be called, as generated by the
* model.
*/
function_call?: FunctionCall;
/**
* The role of the author of this message.
*/
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;
/**
* The function that the model called.
*/
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;
}
/**
* https://github.com/openai/openai-node/blob/3ec43ee790a2eb6a0ccdd5f25faa23251b0f9b8e/src/resources/completions.ts#L28C1-L64C1
* Completions API. Streamed and non-streamed responses are the same.
*/
interface Completion {
/**
* A unique identifier for the completion.
*/
id: string;
/**
* The list of completion choices the model generated for the input prompt.
*/
choices: Array<CompletionChoice>;
/**
* The Unix timestamp of when the completion was created.
*/
created: number;
/**
* The model used for completion.
*/
model: string;
/**
* The object type, which is always "text_completion"
*/
object: string;
/**
* Usage statistics for the completion request.
*/
usage?: CompletionUsage;
}
interface CompletionChoice {
/**
* The reason the model stopped generating tokens. This will be `stop` if the model
* hit a natural stop point or a provided stop sequence, or `length` if the maximum
* number of tokens specified in the request was reached.
*/
finish_reason: 'stop' | 'length' | 'content_filter';
index: number;
// edited: Removed CompletionChoice.logProbs and replaced with any
logprobs: any | null;
text: string;
}
export interface CompletionUsage {
/**
* Usage statistics for the completion request.
*/
/**
* Number of tokens in the generated completion.
*/
completion_tokens: number;
/**
* Number of tokens in the prompt.
*/
prompt_tokens: number;
/**
* Total number of tokens used in the request (prompt + completion).
*/
total_tokens: number;
}
/**
* Creates a parser function for processing the OpenAI stream data.
* The parser extracts and trims text content from the JSON data. This parser
* can handle data for chat or completion models.
*
* @return {(data: string) => string | void| { isText: false; content: string }}
* A parser function that takes a JSON string as input and returns the extracted text content,
* a complex object with isText: false for function/tool calls, or nothing.
*/
function parseOpenAIStream(): (
data: string,
) => string | void | { isText: false; content: string } {
const extract = chunkToText();
return data => extract(JSON.parse(data) as OpenAIStreamReturnTypes);
}
/**
* Reads chunks from OpenAI's new Streamable interface, which is essentially
* the same as the old Response body interface with an included SSE parser
* doing the parsing for us.
*/
async function* streamable(stream: AsyncIterableOpenAIStreamReturnTypes) {
const extract = chunkToText();
for await (let chunk of stream) {
// convert chunk if it is an Azure chat completion. Azure does not expose all
// properties in the interfaces, and also uses camelCase instead of snake_case
if ('promptFilterResults' in chunk) {
chunk = {
id: chunk.id,
created: chunk.created.getDate(),
object: (chunk as any).object, // not exposed by Azure API
model: (chunk as any).model, // not exposed by Azure API
choices: chunk.choices.map(choice => ({
delta: {
content: choice.delta?.content,
function_call: choice.delta?.functionCall,
role: choice.delta?.role as any,
tool_calls: choice.delta?.toolCalls?.length
? choice.delta?.toolCalls?.map((toolCall, index) => ({
index,
id: toolCall.id,
function: toolCall.function,
type: toolCall.type,
}))
: undefined,
},
finish_reason: choice.finishReason as any,
index: choice.index,
})),
} satisfies ChatCompletionChunk;
}
const text = extract(chunk);
if (text) yield text;
}
}
function chunkToText(): (
chunk: OpenAIStreamReturnTypes,
) => string | { isText: false; content: string } | void {
const trimStartOfStream = trimStartOfStreamHelper();
let isFunctionStreamingIn: boolean;
return json => {
if (isChatCompletionChunk(json)) {
const delta = json.choices[0]?.delta;
if (delta.function_call?.name) {
isFunctionStreamingIn = true;
return {
isText: false,
content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`,
};
} else if (delta.tool_calls?.[0]?.function?.name) {
isFunctionStreamingIn = true;
const toolCall = delta.tool_calls[0];
if (toolCall.index === 0) {
return {
isText: false,
content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${toolCall.function?.name}", "arguments": "`,
};
} else {
return {
isText: false,
content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${toolCall.function?.name}", "arguments": "`,
};
}
} else if (delta.function_call?.arguments) {
return {
isText: false,
content: cleanupArguments(delta.function_call?.arguments),
};
} else if (delta.tool_calls?.[0]?.function?.arguments) {
return {
isText: false,
content: cleanupArguments(delta.tool_calls?.[0]?.function?.arguments),
};
} else if (
isFunctionStreamingIn &&
(json.choices[0]?.finish_reason === 'function_call' ||
json.choices[0]?.finish_reason === 'stop')
) {
isFunctionStreamingIn = false; // Reset the flag
return {
isText: false,
content: '"}}',
};
} else if (
isFunctionStreamingIn &&
json.choices[0]?.finish_reason === 'tool_calls'
) {
isFunctionStreamingIn = false; // Reset the flag
return {
isText: false,
content: '"}}]}',
};
}
}
const text = trimStartOfStream(
isChatCompletionChunk(json) && json.choices[0].delta.content
? json.choices[0].delta.content
: isCompletion(json)
? json.choices[0].text
: '',
);
return text;
};
function cleanupArguments(argumentChunk: string) {
let escapedPartialJson = argumentChunk
.replace(/\\/g, '\\\\') // Replace backslashes first to prevent double escaping
.replace(/\//g, '\\/') // Escape slashes
.replace(/"/g, '\\"') // Escape double quotes
.replace(/\n/g, '\\n') // Escape new lines
.replace(/\r/g, '\\r') // Escape carriage returns
.replace(/\t/g, '\\t') // Escape tabs
.replace(/\f/g, '\\f'); // Escape form feeds
return `${escapedPartialJson}`;
}
}
const __internal__OpenAIFnMessagesSymbol = Symbol(
'internal_openai_fn_messages',
);
type AsyncIterableOpenAIStreamReturnTypes =
| AsyncIterable<ChatCompletionChunk>
| AsyncIterable<Completion>
| AsyncIterable<AzureChatCompletions>;
type ExtractType<T> = T extends AsyncIterable<infer U> ? U : never;
type OpenAIStreamReturnTypes =
ExtractType<AsyncIterableOpenAIStreamReturnTypes>;
function isChatCompletionChunk(
data: OpenAIStreamReturnTypes,
): data is ChatCompletionChunk {
return (
'choices' in data &&
data.choices &&
data.choices[0] &&
'delta' in data.choices[0]
);
}
function isCompletion(data: OpenAIStreamReturnTypes): data is Completion {
return (
'choices' in data &&
data.choices &&
data.choices[0] &&
'text' in data.choices[0]
);
}
export function OpenAIStream(
res: Response | AsyncIterableOpenAIStreamReturnTypes,
callbacks?: OpenAIStreamCallbacks,
): ReadableStream {
// Annotate the internal `messages` property for recursive function calls
const cb:
| undefined
| (OpenAIStreamCallbacks & {
[__internal__OpenAIFnMessagesSymbol]?: CreateMessage[];
}) = callbacks;
let stream: ReadableStream<Uint8Array>;
if (Symbol.asyncIterator in res) {
stream = readableFromAsyncIterable(streamable(res)).pipeThrough(
createCallbacksTransformer(
cb?.experimental_onFunctionCall || cb?.experimental_onToolCall
? {
...cb,
onFinal: undefined,
}
: {
...cb,
},
),
);
} else {
stream = AIStream(
res,
parseOpenAIStream(),
cb?.experimental_onFunctionCall || cb?.experimental_onToolCall
? {
...cb,
onFinal: undefined,
}
: {
...cb,
},
);
}
if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
const functionCallTransformer = createFunctionCallTransformer(cb);
return stream.pipeThrough(functionCallTransformer);
} else {
return stream.pipeThrough(
createStreamDataTransformer(cb?.experimental_streamData),
);
}
}
function createFunctionCallTransformer(
callbacks: OpenAIStreamCallbacks & {
[__internal__OpenAIFnMessagesSymbol]?: CreateMessage[];
},
): TransformStream<Uint8Array, Uint8Array> {
const textEncoder = new TextEncoder();
let isFirstChunk = true;
let aggregatedResponse = '';
let aggregatedFinalCompletionResponse = '';
let isFunctionStreamingIn = false;
let functionCallMessages: CreateMessage[] =
callbacks[__internal__OpenAIFnMessagesSymbol] || [];
const isComplexMode = callbacks?.experimental_streamData;
const decode = createChunkDecoder();
return new TransformStream({
async transform(chunk, controller): Promise<void> {
const message = decode(chunk);
aggregatedFinalCompletionResponse += message;
const shouldHandleAsFunction =
isFirstChunk &&
(message.startsWith('{"function_call":') ||
message.startsWith('{"tool_calls":'));
if (shouldHandleAsFunction) {
isFunctionStreamingIn = true;
aggregatedResponse += message;
isFirstChunk = false;
return;
}
// Stream as normal
if (!isFunctionStreamingIn) {
controller.enqueue(
isComplexMode
? textEncoder.encode(formatStreamPart('text', message))
: chunk,
);
return;
} else {
aggregatedResponse += message;
}
},
async flush(controller): Promise<void> {
try {
if (
!isFirstChunk &&
isFunctionStreamingIn &&
(callbacks.experimental_onFunctionCall ||
callbacks.experimental_onToolCall)
) {
isFunctionStreamingIn = false;
const payload = JSON.parse(aggregatedResponse);
// Append the function call message to the list
let newFunctionCallMessages: CreateMessage[] = [
...functionCallMessages,
];
let functionResponse:
| Response
| undefined
| void
| string
| AsyncIterableOpenAIStreamReturnTypes
| undefined = undefined;
// This callbacks.experimental_onFunctionCall check should not be necessary but TS complains
if (callbacks.experimental_onFunctionCall) {
// If the user is using the experimental_onFunctionCall callback, they should not be using tools
// if payload.function_call is not defined by time we get here we must have gotten a tool response
// and the user had defined experimental_onToolCall
if (payload.function_call === undefined) {
console.warn(
'experimental_onFunctionCall should not be defined when using tools',
);
}
const argumentsPayload = JSON.parse(
payload.function_call.arguments,
);
functionResponse = await callbacks.experimental_onFunctionCall(
{
name: payload.function_call.name,
arguments: argumentsPayload,
},
result => {
// Append the function call request and result messages to the list
newFunctionCallMessages = [
...functionCallMessages,
{
role: 'assistant',
content: '',
function_call: payload.function_call,
},
{
role: 'function',
name: payload.function_call.name,
content: JSON.stringify(result),
},
];
// Return it to the user
return newFunctionCallMessages;
},
);
}
if (callbacks.experimental_onToolCall) {
const toolCalls: ToolCallPayload = {
tools: [],
};
for (const tool of payload.tool_calls) {
toolCalls.tools.push({
id: tool.id,
type: 'function',
func: {
name: tool.function.name,
arguments: tool.function.arguments,
},
});
}
let responseIndex = 0;
try {
functionResponse = await callbacks.experimental_onToolCall(
toolCalls,
result => {
if (result) {
const { tool_call_id, function_name, tool_call_result } =
result;
// Append the function call request and result messages to the list
newFunctionCallMessages = [
...newFunctionCallMessages,
// Only append the assistant message if it's the first response
...(responseIndex === 0
? [
{
role: 'assistant' as const,
content: '',
tool_calls: payload.tool_calls.map(
(tc: ToolCall) => ({
id: tc.id,
type: 'function',
function: {
name: tc.function.name,
// we send the arguments an object to the user, but as the API expects a string, we need to stringify it
arguments: JSON.stringify(
tc.function.arguments,
),
},
}),
),
},
]
: []),
// Append the function call result message
{
role: 'tool',
tool_call_id,
name: function_name,
content: JSON.stringify(tool_call_result),
},
];
responseIndex++;
}
// Return it to the user
return newFunctionCallMessages;
},
);
} catch (e) {
console.error('Error calling experimental_onToolCall:', e);
}
}
if (!functionResponse) {
// The user didn't do anything with the function call on the server and wants
// to either do nothing or run it on the client
// so we just return the function call as a message
controller.enqueue(
textEncoder.encode(
isComplexMode
? formatStreamPart(
payload.function_call ? 'function_call' : 'tool_calls',
// parse to prevent double-encoding:
JSON.parse(aggregatedResponse),
)
: aggregatedResponse,
),
);
return;
} else if (typeof functionResponse === 'string') {
// The user returned a string, so we just return it as a message
controller.enqueue(
isComplexMode
? textEncoder.encode(formatStreamPart('text', functionResponse))
: textEncoder.encode(functionResponse),
);
aggregatedFinalCompletionResponse = functionResponse;
return;
}
// Recursively:
// We don't want to trigger onStart or onComplete recursively
// so we remove them from the callbacks
// see https://github.com/vercel/ai/issues/351
const filteredCallbacks: OpenAIStreamCallbacks = {
...callbacks,
onStart: undefined,
};
// We only want onFinal to be called the _last_ time
callbacks.onFinal = undefined;
const openAIStream = OpenAIStream(functionResponse, {
...filteredCallbacks,
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages,
} as AIStreamCallbacksAndOptions);
const reader = openAIStream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
controller.enqueue(value);
}
}
} finally {
if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
await callbacks.onFinal(aggregatedFinalCompletionResponse);
}
}
},
});
}