File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff 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 {
603606type 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
You can’t perform that action at this time.
0 commit comments