Fixed the problem of arguments not being stringified.

Fix panic: candidate.Content.Parts out of range
This commit is contained in:
mxdlzg 2024-04-23 16:18:13 +08:00
parent 78cbbd9ac8
commit 1588716e05

View File

@ -172,11 +172,16 @@ func getToolCalls(candidate *ChatCandidate) []model.Tool {
if item.FunctionCall == nil {
return toolCalls
}
argsBytes, err := json.Marshal(item.FunctionCall.Arguments)
if err != nil {
logger.FatalLog("getToolCalls failed: " + err.Error())
return toolCalls
}
toolCall := model.Tool{
Id: fmt.Sprintf("call_%s", random.GetUUID()),
Type: "function",
Function: model.Function{
Arguments: item.FunctionCall.Arguments,
Arguments: string(argsBytes),
Name: item.FunctionCall.FunctionName,
},
}
@ -199,11 +204,15 @@ func responseGeminiChat2OpenAI(response *ChatResponse) *openai.TextResponse {
},
FinishReason: constant.StopFinishReason,
}
if candidate.Content.Parts[i].FunctionCall != nil {
choice.Message.ToolCalls = getToolCalls(&candidate)
} else if len(candidate.Content.Parts) > 0 {
choice.Message.Content = candidate.Content.Parts[i].Text
if len(candidate.Content.Parts) > 0 {
if candidate.Content.Parts[0].FunctionCall != nil {
choice.Message.ToolCalls = getToolCalls(&candidate)
} else {
choice.Message.Content = candidate.Content.Parts[0].Text
}
} else {
choice.Message.Content = ""
choice.FinishReason = candidate.FinishReason
}
fullTextResponse.Choices = append(fullTextResponse.Choices, choice)
}