Commit 26e2170b by CalciumIon

feat: Add FunctionResponse type and enhance GeminiPart structure

- Introduced a new `FunctionResponse` type to encapsulate function call responses, improving the clarity of data handling.
- Updated the `GeminiPart` struct to include the new `FunctionResponse` field, allowing for better representation of function call results in Gemini requests.
- Modified the `CovertGemini2OpenAI` function to handle tool calls more effectively by setting the message role and appending function responses to the Gemini parts, enhancing the integration with OpenAI and Gemini systems.
parent 69c590d4
......@@ -18,10 +18,16 @@ type FunctionCall struct {
Arguments any `json:"args"`
}
type FunctionResponse struct {
Name string `json:"name"`
Response any `json:"response"`
}
type GeminiPart struct {
Text string `json:"text,omitempty"`
InlineData *GeminiInlineData `json:"inlineData,omitempty"`
FunctionCall *FunctionCall `json:"functionCall,omitempty"`
FunctionResponse *FunctionResponse `json:"functionResponse,omitempty"`
}
type GeminiChatContent struct {
......
......@@ -2,6 +2,7 @@ package gemini
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
......@@ -108,16 +109,14 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
},
}
continue
} else if message.Role == "tool" {
message.Role = "model"
}
var parts []GeminiPart
content := GeminiChatContent{
Role: message.Role,
}
isToolCall := false
if message.ToolCalls != nil {
message.Role = "model"
isToolCall = true
for _, call := range message.ParseToolCalls() {
toolCall := GeminiPart{
......@@ -130,6 +129,20 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
}
if !isToolCall {
if message.Role == "tool" {
content.Role = "user"
name := ""
if message.Name != nil {
name = *message.Name
}
functionResp := &FunctionResponse{
Name: name,
Response: common.StrToMap(message.StringContent()),
}
parts = append(parts, GeminiPart{
FunctionResponse: functionResp,
})
} else {
openaiContent := message.ParseContent()
imageNum := 0
for _, part := range openaiContent {
......@@ -168,6 +181,7 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
}
}
}
content.Parts = parts
// there's no assistant role in gemini and API shall vomit if Role is not user or model
......@@ -176,6 +190,7 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
geminiRequest.Contents = append(geminiRequest.Contents, content)
}
common.LogJson(context.Background(), "gemini_request", geminiRequest)
return &geminiRequest, nil
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment