-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tracing, factor out some helper functions, fix a few bugs with Nodejs mode #68
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e4d0c2c
Factor out app url
ankrgyl 21c9075
zod
ankrgyl 283cb86
Export some stuff
ankrgyl ed397b5
Allow specifying whether fetch decompresses
ankrgyl 1f1e021
Bump vercel AI sdk
ankrgyl caaebb5
Revert version
ankrgyl dd19b89
Snapshot
ankrgyl ec941a5
Add proxy tracing
ankrgyl f65e548
Include usage
ankrgyl 53c86d6
Merge branch 'main' into sandbox-p2
ankrgyl 87e7011
Fix
ankrgyl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,249 @@ | ||
// This is copied from the Vercel AI SDK commit bfa1182c7f5379d7a3d81878ea00ec84682cb046 | ||
// We just need the OpenAI parser, but not the streaming code. | ||
|
||
import { CompletionUsage, FunctionCall, trimStartOfStreamHelper } from "ai"; | ||
|
||
// 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; | ||
} | ||
|
||
/** | ||
* 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} A parser function that takes a JSON string as input and returns the extracted text content or nothing. | ||
*/ | ||
export function parseOpenAIStream(): (data: string) => string | void { | ||
const extract = chunkToText(); | ||
return (data) => extract(JSON.parse(data) as OpenAIStreamReturnTypes); | ||
} | ||
|
||
function chunkToText(): (chunk: OpenAIStreamReturnTypes) => 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 `{"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 `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${toolCall.function?.name}", "arguments": "`; | ||
} else { | ||
return `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${toolCall.function?.name}", "arguments": "`; | ||
} | ||
} else if (delta.function_call?.arguments) { | ||
return cleanupArguments(delta.function_call?.arguments); | ||
} else if (delta.tool_calls?.[0]?.function?.arguments) { | ||
return 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 '"}}'; | ||
} else if ( | ||
isFunctionStreamingIn && | ||
json.choices[0]?.finish_reason === "tool_calls" | ||
) { | ||
isFunctionStreamingIn = false; // Reset the flag | ||
return '"}}]}'; | ||
} | ||
} | ||
|
||
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 AzureChatCompletions = any; | ||
|
||
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] | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment superseded by the next line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. It's just copy/pasted from Vercel's code. Would rather not change it.