mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-11 10:53:42 +08:00
Merge branch 'feature/gemini-2.0-flash'
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
@@ -33,12 +32,6 @@ var mimeTypeMap = map[string]string{
|
||||
"text": "text/plain",
|
||||
}
|
||||
|
||||
var toolChoiceTypeMap = map[string]string{
|
||||
"none": "NONE",
|
||||
"auto": "AUTO",
|
||||
"required": "ANY",
|
||||
}
|
||||
|
||||
// Setting safety to the lowest possible values since Gemini is already powerless enough
|
||||
func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
||||
geminiRequest := ChatRequest{
|
||||
@@ -97,24 +90,7 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
||||
},
|
||||
}
|
||||
}
|
||||
if textRequest.ToolChoice != nil {
|
||||
geminiRequest.ToolConfig = &ToolConfig{
|
||||
FunctionCallingConfig: FunctionCallingConfig{
|
||||
Mode: "auto",
|
||||
},
|
||||
}
|
||||
switch mode := textRequest.ToolChoice.(type) {
|
||||
case string:
|
||||
geminiRequest.ToolConfig.FunctionCallingConfig.Mode = toolChoiceTypeMap[mode]
|
||||
case map[string]interface{}:
|
||||
geminiRequest.ToolConfig.FunctionCallingConfig.Mode = "ANY"
|
||||
if fn, ok := mode["function"].(map[string]interface{}); ok {
|
||||
if name, ok := fn["name"].(string); ok {
|
||||
geminiRequest.ToolConfig.FunctionCallingConfig.AllowedFunctionNames = []string{name}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
shouldAddDummyModelMessage := false
|
||||
for _, message := range textRequest.Messages {
|
||||
content := ChatContent{
|
||||
Role: message.Role,
|
||||
@@ -152,21 +128,32 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
||||
if content.Role == "assistant" {
|
||||
content.Role = "model"
|
||||
}
|
||||
// Converting system prompt to SystemInstructions
|
||||
// Converting system prompt to prompt from user for the same reason
|
||||
if content.Role == "system" {
|
||||
geminiRequest.SystemInstruction = &content
|
||||
continue
|
||||
shouldAddDummyModelMessage = true
|
||||
if IsModelSupportSystemInstruction(textRequest.Model) {
|
||||
geminiRequest.SystemInstruction = &content
|
||||
geminiRequest.SystemInstruction.Role = ""
|
||||
continue
|
||||
} else {
|
||||
content.Role = "user"
|
||||
}
|
||||
}
|
||||
geminiRequest.Contents = append(geminiRequest.Contents, content)
|
||||
}
|
||||
|
||||
// As of 2025-02-06, the newly released gemini 2.0 models do not support system_instruction,
|
||||
// which can reasonably be considered a bug. Google may fix this issue in the future.
|
||||
if geminiRequest.SystemInstruction != nil &&
|
||||
strings.Contains(textRequest.Model, "-2.0") &&
|
||||
textRequest.Model != "gemini-2.0-flash-exp" &&
|
||||
textRequest.Model != "gemini-2.0-flash-thinking-exp-01-21" {
|
||||
geminiRequest.SystemInstruction = nil
|
||||
geminiRequest.Contents = append(geminiRequest.Contents, content)
|
||||
|
||||
// If a system message is the last message, we need to add a dummy model message to make gemini happy
|
||||
if shouldAddDummyModelMessage {
|
||||
geminiRequest.Contents = append(geminiRequest.Contents, ChatContent{
|
||||
Role: "model",
|
||||
Parts: []Part{
|
||||
{
|
||||
Text: "Okay",
|
||||
},
|
||||
},
|
||||
})
|
||||
shouldAddDummyModelMessage = false
|
||||
}
|
||||
}
|
||||
|
||||
return &geminiRequest
|
||||
@@ -204,16 +191,10 @@ func (g *ChatResponse) GetResponseText() string {
|
||||
if g == nil {
|
||||
return ""
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, candidate := range g.Candidates {
|
||||
for idx, part := range candidate.Content.Parts {
|
||||
if idx > 0 {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
builder.WriteString(part.Text)
|
||||
}
|
||||
if len(g.Candidates) > 0 && len(g.Candidates[0].Content.Parts) > 0 {
|
||||
return g.Candidates[0].Content.Parts[0].Text
|
||||
}
|
||||
return builder.String()
|
||||
return ""
|
||||
}
|
||||
|
||||
type ChatCandidate struct {
|
||||
@@ -276,8 +257,8 @@ func responseGeminiChat2OpenAI(response *ChatResponse) *openai.TextResponse {
|
||||
choice.Message.ToolCalls = getToolCalls(&candidate)
|
||||
} else {
|
||||
var builder strings.Builder
|
||||
for idx, part := range candidate.Content.Parts {
|
||||
if idx > 0 {
|
||||
for _, part := range candidate.Content.Parts {
|
||||
if i > 0 {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
builder.WriteString(part.Text)
|
||||
|
||||
Reference in New Issue
Block a user