fix: tool call handling in OpenAIProvider (#2953)
This commit is contained in:
parent
52de270d04
commit
6b1c27ab2c
@ -334,8 +334,7 @@ export default class OpenAIProvider extends BaseProvider {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
const final_tool_calls = {} as Record<number, ChatCompletionMessageToolCall>
|
||||||
const toolCalls: ChatCompletionMessageToolCall[] = []
|
|
||||||
|
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
if (window.keyv.get(EVENT_NAMES.CHAT_COMPLETION_PAUSED)) {
|
if (window.keyv.get(EVENT_NAMES.CHAT_COMPLETION_PAUSED)) {
|
||||||
@ -365,18 +364,22 @@ export default class OpenAIProvider extends BaseProvider {
|
|||||||
const finishReason = chunk.choices[0]?.finish_reason
|
const finishReason = chunk.choices[0]?.finish_reason
|
||||||
|
|
||||||
if (delta?.tool_calls) {
|
if (delta?.tool_calls) {
|
||||||
const chunkToolCalls: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta.ToolCall[] = delta.tool_calls
|
const chunkToolCalls = delta.tool_calls
|
||||||
if (finishReason !== 'tool_calls') {
|
if (finishReason !== 'tool_calls') {
|
||||||
if (toolCalls.length === 0) {
|
for (const t of chunkToolCalls) {
|
||||||
for (const toolCall of chunkToolCalls) {
|
const { index, id, function: fn, type } = t
|
||||||
toolCalls.push(toolCall as ChatCompletionMessageToolCall)
|
const args = fn && typeof fn.arguments === 'string' ? fn.arguments : ''
|
||||||
}
|
if (!(index in final_tool_calls)) {
|
||||||
} else {
|
final_tool_calls[index] = {
|
||||||
for (let i = 0; i < chunkToolCalls.length; i++) {
|
id,
|
||||||
if (typeof toolCalls[i].function.arguments !== 'string') {
|
function: {
|
||||||
toolCalls[i].function.arguments = ''
|
name: fn?.name,
|
||||||
}
|
arguments: args
|
||||||
toolCalls[i].function.arguments += chunkToolCalls[i].function?.arguments || ''
|
},
|
||||||
|
type
|
||||||
|
} as ChatCompletionMessageToolCall
|
||||||
|
} else {
|
||||||
|
final_tool_calls[index].function.arguments += args
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@ -384,6 +387,7 @@ export default class OpenAIProvider extends BaseProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (finishReason === 'tool_calls') {
|
if (finishReason === 'tool_calls') {
|
||||||
|
const toolCalls = Object.values(final_tool_calls)
|
||||||
console.log('start invoke tools', toolCalls)
|
console.log('start invoke tools', toolCalls)
|
||||||
reqMessages.push({
|
reqMessages.push({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user