Skip to content

Commit 4f5114e

Browse files
committed
Merge branch 'stream-tools-stop' into testing
2 parents b2c9288 + 38b3b15 commit 4f5114e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

openai/openai.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ func toChunk(id string, r api.ChatResponse) ChatCompletionChunk {
293293
Index: 0,
294294
Delta: Message{Role: "assistant", Content: r.Message.Content, ToolCalls: toolCalls},
295295
FinishReason: func(reason string) *string {
296+
if len(toolCalls) > 0 {
297+
reason = "tool_calls"
298+
}
296299
if len(reason) > 0 {
297300
return &reason
298301
}
@@ -603,6 +606,7 @@ type BaseWriter struct {
603606
type ChatWriter struct {
604607
stream bool
605608
streamUsage bool
609+
finished bool
606610
id string
607611
BaseWriter
608612
}
@@ -657,6 +661,13 @@ func (w *ChatWriter) writeResponse(data []byte) (int, error) {
657661
if w.streamUsage {
658662
c.Usage = &nullChunkUsage
659663
}
664+
if w.finished {
665+
// we've already finished the chat, usually for tool calls, so do
666+
// not continue to send empty choices.
667+
c.Choices = nil
668+
} else if len(c.Choices) > 0 && c.Choices[0].FinishReason != nil {
669+
w.finished = true
670+
}
660671
d, err := json.Marshal(c)
661672
if err != nil {
662673
return 0, err

0 commit comments

Comments
 (0)