Skip to content

Commit

Permalink
Fixed bug with tool calls being split across multiple chunks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevenic committed Nov 21, 2024
1 parent 4e39b9e commit e71b392
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions js/packages/teams-ai/src/models/OpenAIModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,32 @@ export class OpenAIModel implements PromptCompletionModel {

// Add tool calls to action calls
for (const toolCall of delta.tool_calls) {
message.action_calls.push({
id: toolCall.id,
function: {
name: toolCall.function!.name,
arguments: toolCall.function!.arguments
},
type: toolCall.type
} as ActionCall);
// Add empty tool call to message if new index
// - Note that a single tool call can span multiple chunks.
const index = toolCall.index;
if (index >= message.action_calls.length) {
message.action_calls.push({ id: '', function: { name: '', arguments: '' }, type: '' } as any);
}

// Set ID if provided
if (toolCall.id) {
message.action_calls[index].id = toolCall.id;
}

// Set type if provided
if (toolCall.type) {
message.action_calls[index].type = toolCall.type;
}

// Append function name if provided
if (toolCall.function?.name) {
message.action_calls[index].function.name += toolCall.function.name;
}

// Append function arguments if provided
if (toolCall.function?.arguments) {
message.action_calls[index].function.arguments += toolCall.function.arguments;
}
}
}

Expand Down

0 comments on commit e71b392

Please sign in to comment.