Skip to content

Commit

Permalink
fix(streaming): correctly accumulate tool calls and roles (openai#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored Sep 18, 2024
1 parent 80e9a72 commit 385e9d9
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions streamaccumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (cc *ChatCompletion) accumulateDelta(chunk ChatCompletionChunk) bool {
choice.Index = delta.Index
choice.FinishReason = ChatCompletionChoicesFinishReason(delta.FinishReason)

if delta.Delta.Role != "" {
choice.Message.Role = ChatCompletionMessageRole(delta.Delta.Role)
}

choice.Message.Content += delta.Delta.Content
choice.Message.Refusal += delta.Delta.Refusal

Expand All @@ -119,8 +123,12 @@ func (cc *ChatCompletion) accumulateDelta(chunk ChatCompletionChunk) bool {
choice.Message.ToolCalls = expandToFit(choice.Message.ToolCalls, int(deltaTool.Index))
tool := &choice.Message.ToolCalls[deltaTool.Index]

tool.ID = deltaTool.ID
tool.Type = ChatCompletionMessageToolCallType(deltaTool.Type)
if deltaTool.ID != "" {
tool.ID = deltaTool.ID
}
if deltaTool.Type != "" {
tool.Type = ChatCompletionMessageToolCallType(deltaTool.Type)
}
tool.Function.Name += deltaTool.Function.Name
tool.Function.Arguments += deltaTool.Function.Arguments
}
Expand Down

0 comments on commit 385e9d9

Please sign in to comment.