From fc984597845354b878db4aeff392207db7f7c25f Mon Sep 17 00:00:00 2001 From: visargD Date: Sat, 9 Mar 2024 13:37:16 +0530 Subject: [PATCH 1/2] fix: handle multiple tool calls response in json to stream handler --- src/providers/openai/chatComplete.ts | 101 +++++++++++++-------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/src/providers/openai/chatComplete.ts b/src/providers/openai/chatComplete.ts index eb6d13ecd..6d8a39309 100644 --- a/src/providers/openai/chatComplete.ts +++ b/src/providers/openai/chatComplete.ts @@ -99,72 +99,69 @@ export const OpenAIChatCompleteResponseTransform: (response: OpenAIChatCompleteR export const OpenAIChatCompleteJSONToStreamResponseTransform: (response: OpenAIChatCompleteResponse, provider: string) => Array = (response, provider) => { const streamChunkArray: Array = []; const { id, model, system_fingerprint, choices } = response; - - const { prompt_tokens, completion_tokens } = response.usage || {}; - - let total_tokens; - if (prompt_tokens && completion_tokens) total_tokens = prompt_tokens + completion_tokens; - - const streamChunkTemplate: Record = { + const streamChunkTemplate = { id, object: "chat.completion.chunk", created: Date.now(), model: model || "", system_fingerprint: system_fingerprint || null, provider, - usage: { - ...(completion_tokens && {completion_tokens}), - ...(prompt_tokens && {prompt_tokens}), - ...(total_tokens && {total_tokens}) + usage: {} + } + + if (response.usage?.completion_tokens) { + streamChunkTemplate.usage = { + completion_tokens: response.usage?.completion_tokens } } for (const [index, choice] of choices.entries()) { - if (choice.message && choice.message.tool_calls) { - const currentToolCall = choice.message.tool_calls[0]; - const toolCallNameChunk = { - index: 0, - id: currentToolCall.id, - type: "function", - function: { - name: currentToolCall.function.name, - arguments: "" + if (choice.message && choice.message.tool_calls && choice.message.tool_calls.length) { + for (const [toolCallIndex, toolCall] of choice.message.tool_calls.entries()) { + const toolCallNameChunk = { + index: toolCallIndex, + id: toolCall.id, + type: "function", + function: { + name: toolCall.function.name, + arguments: "" + } } - } - - const toolCallArgumentChunk = { - index: 0, - function: { - arguments: currentToolCall.function.arguments + + const toolCallArgumentChunk = { + index: toolCallIndex, + function: { + arguments: toolCall.function.arguments + } } - } - - streamChunkArray.push(`data: ${JSON.stringify({ - ...streamChunkTemplate, - choices: [ - { - index: index, - delta: { - role: "assistant", - content: null, - tool_calls: [toolCallNameChunk] + + streamChunkArray.push(`data: ${JSON.stringify({ + ...streamChunkTemplate, + choices: [ + { + index: index, + delta: { + role: "assistant", + content: null, + tool_calls: [toolCallNameChunk] + } } - } - ] - })}\n\n`) - - streamChunkArray.push(`data: ${JSON.stringify({ - ...streamChunkTemplate, - choices: [ - { - index: index, - delta: { - role: "assistant", - tool_calls: [toolCallArgumentChunk] + ] + })}\n\n`) + + streamChunkArray.push(`data: ${JSON.stringify({ + ...streamChunkTemplate, + choices: [ + { + index: index, + delta: { + role: "assistant", + tool_calls: [toolCallArgumentChunk] + } } - } - ] - })}\n\n`) + ] + })}\n\n`) + } } if (choice.message && choice.message.content && typeof choice.message.content === "string") { From 3439bcba44caf259d88929eae36976c78e51a11e Mon Sep 17 00:00:00 2001 From: visargD Date: Sat, 9 Mar 2024 13:41:53 +0530 Subject: [PATCH 2/2] chore: update branch with main --- src/providers/openai/chatComplete.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/providers/openai/chatComplete.ts b/src/providers/openai/chatComplete.ts index 6d8a39309..3c3affbad 100644 --- a/src/providers/openai/chatComplete.ts +++ b/src/providers/openai/chatComplete.ts @@ -99,19 +99,23 @@ export const OpenAIChatCompleteResponseTransform: (response: OpenAIChatCompleteR export const OpenAIChatCompleteJSONToStreamResponseTransform: (response: OpenAIChatCompleteResponse, provider: string) => Array = (response, provider) => { const streamChunkArray: Array = []; const { id, model, system_fingerprint, choices } = response; - const streamChunkTemplate = { + + const { prompt_tokens, completion_tokens } = response.usage || {}; + + let total_tokens; + if (prompt_tokens && completion_tokens) total_tokens = prompt_tokens + completion_tokens; + + const streamChunkTemplate: Record = { id, object: "chat.completion.chunk", created: Date.now(), model: model || "", system_fingerprint: system_fingerprint || null, provider, - usage: {} - } - - if (response.usage?.completion_tokens) { - streamChunkTemplate.usage = { - completion_tokens: response.usage?.completion_tokens + usage: { + ...(completion_tokens && {completion_tokens}), + ...(prompt_tokens && {prompt_tokens}), + ...(total_tokens && {total_tokens}) } }