From bed6bba32a02ebb88bed13cce029e89fa61363bc Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Wed, 18 Sep 2024 14:04:13 +0100 Subject: [PATCH] fix(streaming): correctly accumulate tool calls and roles --- streamaccumulator.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/streamaccumulator.go b/streamaccumulator.go index 5fb45059..3b9f2077 100644 --- a/streamaccumulator.go +++ b/streamaccumulator.go @@ -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 @@ -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 }