Skip to content

Commit 321ff9e

Browse files
fix(streaming): correctly accumulate tool calls and roles (openai#55)
1 parent 6398950 commit 321ff9e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

streamaccumulator.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func (cc *ChatCompletion) accumulateDelta(chunk ChatCompletionChunk) bool {
110110
choice.Index = delta.Index
111111
choice.FinishReason = ChatCompletionChoicesFinishReason(delta.FinishReason)
112112

113+
if delta.Delta.Role != "" {
114+
choice.Message.Role = ChatCompletionMessageRole(delta.Delta.Role)
115+
}
116+
113117
choice.Message.Content += delta.Delta.Content
114118
choice.Message.Refusal += delta.Delta.Refusal
115119

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

122-
tool.ID = deltaTool.ID
123-
tool.Type = ChatCompletionMessageToolCallType(deltaTool.Type)
126+
if deltaTool.ID != "" {
127+
tool.ID = deltaTool.ID
128+
}
129+
if deltaTool.Type != "" {
130+
tool.Type = ChatCompletionMessageToolCallType(deltaTool.Type)
131+
}
124132
tool.Function.Name += deltaTool.Function.Name
125133
tool.Function.Arguments += deltaTool.Function.Arguments
126134
}

0 commit comments

Comments
 (0)