From 8d45670e4109db8968ffa5ae426f6656e9e0784c Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Thu, 6 Feb 2025 12:41:08 +0100 Subject: [PATCH] fix(openai): consistently return stop reason (#4771) We were not returning a stop reason when no tool was actually called (even if specified). Fixes: https://github.com/mudler/LocalAI/issues/4716 Signed-off-by: Ettore Di Giacinto --- core/http/endpoints/openai/chat.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/core/http/endpoints/openai/chat.go b/core/http/endpoints/openai/chat.go index 3b8d3056..a94a729a 100644 --- a/core/http/endpoints/openai/chat.go +++ b/core/http/endpoints/openai/chat.go @@ -401,6 +401,11 @@ func ChatEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, evaluat log.Debug().Msgf("Text content to return: %s", textContentToReturn) noActionsToRun := len(results) > 0 && results[0].Name == noActionName || len(results) == 0 + finishReason := "stop" + if len(input.Tools) > 0 { + finishReason = "tool_calls" + } + switch { case noActionsToRun: result, err := handleQuestion(config, input, ml, startupOptions, results, s, predInput) @@ -408,19 +413,18 @@ func ChatEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, evaluat log.Error().Err(err).Msg("error handling question") return } + *c = append(*c, schema.Choice{ - Message: &schema.Message{Role: "assistant", Content: &result}}) + FinishReason: finishReason, + Message: &schema.Message{Role: "assistant", Content: &result}}) default: toolChoice := schema.Choice{ + FinishReason: finishReason, Message: &schema.Message{ Role: "assistant", }, } - if len(input.Tools) > 0 { - toolChoice.FinishReason = "tool_calls" - } - for _, ss := range results { name, args := ss.Name, ss.Arguments if len(input.Tools) > 0 { @@ -438,7 +442,7 @@ func ChatEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, evaluat }, ) } else { - // otherwise we return more choices directly + // otherwise we return more choices directly (deprecated) *c = append(*c, schema.Choice{ FinishReason: "function_call", Message: &schema.Message{