-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(chat): support custom tool calls in parser and ChatCompletionStream #2023
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { | |
| ChatCompletionFunctionTool, | ||
| ChatCompletionMessage, | ||
| ChatCompletionMessageFunctionToolCall, | ||
| ChatCompletionMessageToolCall, | ||
| ChatCompletionStreamingToolRunnerParams, | ||
| ChatCompletionStreamingToolRunnerParamsWithContext, | ||
| ChatCompletionStreamParams, | ||
|
|
@@ -158,16 +159,18 @@ export function maybeParseChatCompletion< | |
| return { | ||
| ...completion, | ||
| choices: completion.choices.map((choice) => { | ||
| assertToolCallsAreChatCompletionFunctionToolCalls(choice.message.tool_calls); | ||
|
|
||
| return { | ||
| ...choice, | ||
| message: { | ||
| ...choice.message, | ||
| parsed: null, | ||
| ...(choice.message.tool_calls ? | ||
| { | ||
| tool_calls: choice.message.tool_calls, | ||
| tool_calls: choice.message.tool_calls.map((toolCall) => | ||
| toolCall.type === 'function' ? | ||
| { ...toolCall, function: { ...toolCall.function, parsed_arguments: null } } | ||
| : toolCall, | ||
| ), | ||
| } | ||
| : undefined), | ||
| }, | ||
|
|
@@ -192,8 +195,6 @@ export function parseChatCompletion< | |
| throw new ContentFilterFinishReasonError(); | ||
| } | ||
|
|
||
| assertToolCallsAreChatCompletionFunctionToolCalls(choice.message.tool_calls); | ||
|
|
||
| return { | ||
| ...choice, | ||
| message: { | ||
|
|
@@ -238,8 +239,12 @@ function parseResponseFormat< | |
|
|
||
| function parseToolCall<Params extends ChatCompletionCreateParams>( | ||
| params: Params, | ||
| toolCall: ChatCompletionMessageFunctionToolCall, | ||
| ): ParsedFunctionToolCall { | ||
| toolCall: ChatCompletionMessageToolCall, | ||
| ): ParsedFunctionToolCall | ChatCompletionMessageToolCall { | ||
| if (toolCall.type !== 'function') { | ||
| return toolCall; | ||
|
Comment on lines
+240
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| const inputTool = params.tools?.find( | ||
| (inputTool) => | ||
| isChatCompletionFunctionTool(inputTool) && inputTool.function?.name === toolCall.function.name, | ||
|
|
@@ -258,9 +263,9 @@ function parseToolCall<Params extends ChatCompletionCreateParams>( | |
|
|
||
| export function shouldParseToolCall( | ||
| params: ChatCompletionCreateParams | null | undefined, | ||
| toolCall: ChatCompletionMessageFunctionToolCall, | ||
| toolCall: ChatCompletionMessageToolCall, | ||
| ): boolean { | ||
| if (!params || !('tools' in params) || !params.tools) { | ||
| if (!params || !('tools' in params) || !params.tools || toolCall.type !== 'function') { | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
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.
This return type now admits a custom tool call, but the public
ParsedChatCompletionMessage.tool_callsproperty remainsArray<ParsedFunctionToolCall>insrc/resources/chat/completions/completions.ts:286-288. Thus TypeScript consumers of.parse()orstream.finalChatCompletion()cannot narrow to or access.custom, while the declared type incorrectly guarantees that.functionexists; the parsed tool-call type needs to be a union that preserves custom calls.Useful? React with 👍 / 👎.
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.
(Git)