Skip to content

Commit

Permalink
Merge pull request #249 from Portkey-AI/fix/tool-call-json-to-stream
Browse files Browse the repository at this point in the history
fix: handle multiple tool calls response in json to stream handler
  • Loading branch information
VisargD authored Mar 9, 2024
2 parents 40d2365 + 3439bcb commit 399b36f
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions src/providers/openai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,51 +120,52 @@ export const OpenAIChatCompleteJSONToStreamResponseTransform: (response: OpenAIC
}

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") {
Expand Down

0 comments on commit 399b36f

Please sign in to comment.