Skip to content

Commit

Permalink
fix(functions): handle correctly when there are no results (#1758)
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler authored Feb 26, 2024
1 parent 7f72a61 commit 05818e0
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions api/openai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,20 @@ func parseFunctionCall(llmresult string, multipleResults bool) []funcCallResults
log.Debug().Msgf("Function return: %s %+v", s, ss)

for _, s := range ss {
func_name := s["function"]
args := s["arguments"]
func_name, ok := s["function"]
if !ok {
continue
}
args, ok := s["arguments"]
if !ok {
continue
}
d, _ := json.Marshal(args)
results = append(results, funcCallResults{name: func_name.(string), arguments: string(d)})
funcName, ok := func_name.(string)
if !ok {
continue
}
results = append(results, funcCallResults{name: funcName, arguments: string(d)})
}
} else {
// As we have to change the result before processing, we can't stream the answer token-by-token (yet?)
Expand All @@ -579,12 +589,21 @@ func parseFunctionCall(llmresult string, multipleResults bool) []funcCallResults
log.Debug().Msgf("Function return: %s %+v", s, ss)

// The grammar defines the function name as "function", while OpenAI returns "name"
func_name := ss["function"]
func_name, ok := ss["function"]
if !ok {
return results
}
// Similarly, while here arguments is a map[string]interface{}, OpenAI actually want a stringified object
args := ss["arguments"] // arguments needs to be a string, but we return an object from the grammar result (TODO: fix)
args, ok := ss["arguments"] // arguments needs to be a string, but we return an object from the grammar result (TODO: fix)
if !ok {
return results
}
d, _ := json.Marshal(args)

results = append(results, funcCallResults{name: func_name.(string), arguments: string(d)})
funcName, ok := func_name.(string)
if !ok {
return results
}
results = append(results, funcCallResults{name: funcName, arguments: string(d)})
}

return results
Expand Down

0 comments on commit 05818e0

Please sign in to comment.