Skip to content

Commit

Permalink
fix(openai): consistently return stop reason (#4771)
Browse files Browse the repository at this point in the history
We were not returning a stop reason when no tool was actually called
(even if specified).

Fixes: #4716

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
  • Loading branch information
mudler authored Feb 6, 2025
1 parent e4b8ddb commit 8d45670
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions core/http/endpoints/openai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,26 +401,30 @@ 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)
if err != nil {
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 {
Expand All @@ -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{
Expand Down

0 comments on commit 8d45670

Please sign in to comment.