Parcourir la source

fix: gemini request -> openai tool call

Seefs il y a 2 mois
Parent
commit
ddb40b1a6e
2 fichiers modifiés avec 15 ajouts et 14 suppressions
  1. 1 1
      dto/gemini.go
  2. 14 13
      service/convert.go

+ 1 - 1
dto/gemini.go

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

+ 14 - 13
service/convert.go

@@ -674,20 +674,21 @@ func GeminiToOpenAIRequest(geminiRequest *dto.GeminiChatRequest, info *relaycomm
 		var tools []dto.ToolCallRequest
 		for _, tool := range geminiRequest.GetTools() {
 			if tool.FunctionDeclarations != nil {
-				// 将 Gemini 的 FunctionDeclarations 转换为 OpenAI 的 ToolCallRequest
-				functionDeclarations, ok := tool.FunctionDeclarations.([]dto.FunctionRequest)
-				if ok {
-					for _, function := range functionDeclarations {
-						openAITool := dto.ToolCallRequest{
-							Type: "function",
-							Function: dto.FunctionRequest{
-								Name:        function.Name,
-								Description: function.Description,
-								Parameters:  function.Parameters,
-							},
-						}
-						tools = append(tools, openAITool)
+				functionDeclarations, err := common.Any2Type[[]dto.FunctionRequest](tool.FunctionDeclarations)
+				if err != nil {
+					common.SysError(fmt.Sprintf("failed to parse gemini function declarations: %v (type=%T)", err, tool.FunctionDeclarations))
+					continue
+				}
+				for _, function := range functionDeclarations {
+					openAITool := dto.ToolCallRequest{
+						Type: "function",
+						Function: dto.FunctionRequest{
+							Name:        function.Name,
+							Description: function.Description,
+							Parameters:  function.Parameters,
+						},
 					}
+					tools = append(tools, openAITool)
 				}
 			}
 		}