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