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.
This commit is contained in:
@@ -18,10 +18,16 @@ type FunctionCall struct {
|
|||||||
Arguments any `json:"args"`
|
Arguments any `json:"args"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FunctionResponse struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Response any `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
type GeminiPart struct {
|
type GeminiPart struct {
|
||||||
Text string `json:"text,omitempty"`
|
Text string `json:"text,omitempty"`
|
||||||
InlineData *GeminiInlineData `json:"inlineData,omitempty"`
|
InlineData *GeminiInlineData `json:"inlineData,omitempty"`
|
||||||
FunctionCall *FunctionCall `json:"functionCall,omitempty"`
|
FunctionCall *FunctionCall `json:"functionCall,omitempty"`
|
||||||
|
FunctionResponse *FunctionResponse `json:"functionResponse,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GeminiChatContent struct {
|
type GeminiChatContent struct {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package gemini
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -108,16 +109,14 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
} else if message.Role == "tool" {
|
|
||||||
message.Role = "model"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var parts []GeminiPart
|
var parts []GeminiPart
|
||||||
content := GeminiChatContent{
|
content := GeminiChatContent{
|
||||||
Role: message.Role,
|
Role: message.Role,
|
||||||
}
|
}
|
||||||
isToolCall := false
|
isToolCall := false
|
||||||
if message.ToolCalls != nil {
|
if message.ToolCalls != nil {
|
||||||
|
message.Role = "model"
|
||||||
isToolCall = true
|
isToolCall = true
|
||||||
for _, call := range message.ParseToolCalls() {
|
for _, call := range message.ParseToolCalls() {
|
||||||
toolCall := GeminiPart{
|
toolCall := GeminiPart{
|
||||||
@@ -130,6 +129,20 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !isToolCall {
|
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()
|
openaiContent := message.ParseContent()
|
||||||
imageNum := 0
|
imageNum := 0
|
||||||
for _, part := range openaiContent {
|
for _, part := range openaiContent {
|
||||||
@@ -168,6 +181,7 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
content.Parts = parts
|
content.Parts = parts
|
||||||
|
|
||||||
// there's no assistant role in gemini and API shall vomit if Role is not user or model
|
// 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)
|
geminiRequest.Contents = append(geminiRequest.Contents, content)
|
||||||
}
|
}
|
||||||
|
common.LogJson(context.Background(), "gemini_request", geminiRequest)
|
||||||
return &geminiRequest, nil
|
return &geminiRequest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user