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 toolCalls: ChatCompletionMessageToolCall[] = []
|
||||
const final_tool_calls = {} as Record<number, ChatCompletionMessageToolCall>
|
||||
|
||||
for await (const chunk of stream) {
|
||||
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
|
||||
|
||||
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 (toolCalls.length === 0) {
|
||||
for (const toolCall of chunkToolCalls) {
|
||||
toolCalls.push(toolCall as ChatCompletionMessageToolCall)
|
||||
}
|
||||
for (const t of chunkToolCalls) {
|
||||
const { index, id, function: fn, type } = t
|
||||
const args = fn && typeof fn.arguments === 'string' ? fn.arguments : ''
|
||||
if (!(index in final_tool_calls)) {
|
||||
final_tool_calls[index] = {
|
||||
id,
|
||||
function: {
|
||||
name: fn?.name,
|
||||
arguments: args
|
||||
},
|
||||
type
|
||||
} as ChatCompletionMessageToolCall
|
||||
} else {
|
||||
for (let i = 0; i < chunkToolCalls.length; i++) {
|
||||
if (typeof toolCalls[i].function.arguments !== 'string') {
|
||||
toolCalls[i].function.arguments = ''
|
||||
}
|
||||
toolCalls[i].function.arguments += chunkToolCalls[i].function?.arguments || ''
|
||||
final_tool_calls[index].function.arguments += args
|
||||
}
|
||||
}
|
||||
continue
|
||||
@ -384,6 +387,7 @@ export default class OpenAIProvider extends BaseProvider {
|
||||
}
|
||||
|
||||
if (finishReason === 'tool_calls') {
|
||||
const toolCalls = Object.values(final_tool_calls)
|
||||
console.log('start invoke tools', toolCalls)
|
||||
reqMessages.push({
|
||||
role: 'assistant',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user