fix: gemini request -> openai tool call

This commit is contained in:
Seefs
2025-12-31 18:09:21 +08:00
parent b808b96cce
commit ddb40b1a6e
2 changed files with 15 additions and 14 deletions

View File

@@ -126,7 +126,7 @@ func (r *GeminiChatRequest) SetModelName(modelName string) {
func (r *GeminiChatRequest) GetTools() []GeminiChatTool { func (r *GeminiChatRequest) GetTools() []GeminiChatTool {
var tools []GeminiChatTool var tools []GeminiChatTool
if strings.HasSuffix(string(r.Tools), "[") { if strings.HasPrefix(string(r.Tools), "[") {
// is array // is array
if err := common.Unmarshal(r.Tools, &tools); err != nil { if err := common.Unmarshal(r.Tools, &tools); err != nil {
logger.LogError(nil, "error_unmarshalling_tools: "+err.Error()) logger.LogError(nil, "error_unmarshalling_tools: "+err.Error())

View File

@@ -674,9 +674,11 @@ func GeminiToOpenAIRequest(geminiRequest *dto.GeminiChatRequest, info *relaycomm
var tools []dto.ToolCallRequest var tools []dto.ToolCallRequest
for _, tool := range geminiRequest.GetTools() { for _, tool := range geminiRequest.GetTools() {
if tool.FunctionDeclarations != nil { if tool.FunctionDeclarations != nil {
// 将 Gemini 的 FunctionDeclarations 转换为 OpenAI 的 ToolCallRequest functionDeclarations, err := common.Any2Type[[]dto.FunctionRequest](tool.FunctionDeclarations)
functionDeclarations, ok := tool.FunctionDeclarations.([]dto.FunctionRequest) if err != nil {
if ok { common.SysError(fmt.Sprintf("failed to parse gemini function declarations: %v (type=%T)", err, tool.FunctionDeclarations))
continue
}
for _, function := range functionDeclarations { for _, function := range functionDeclarations {
openAITool := dto.ToolCallRequest{ openAITool := dto.ToolCallRequest{
Type: "function", Type: "function",
@@ -690,7 +692,6 @@ func GeminiToOpenAIRequest(geminiRequest *dto.GeminiChatRequest, info *relaycomm
} }
} }
} }
}
if len(tools) > 0 { if len(tools) > 0 {
openaiRequest.Tools = tools openaiRequest.Tools = tools
} }