2024-02-29 01:08:18 +08:00
|
|
|
|
package gemini
|
2023-12-18 23:45:08 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bufio"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"one-api/common"
|
2024-12-06 14:31:27 +08:00
|
|
|
|
"one-api/constant"
|
2024-02-29 01:08:18 +08:00
|
|
|
|
"one-api/dto"
|
|
|
|
|
|
relaycommon "one-api/relay/common"
|
|
|
|
|
|
"one-api/service"
|
2023-12-18 23:45:08 +08:00
|
|
|
|
"strings"
|
2024-12-20 13:20:07 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-12-18 23:45:08 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Setting safety to the lowest possible values since Gemini is already powerless enough
|
2024-12-20 21:50:58 +08:00
|
|
|
|
func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatRequest, error) {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
|
2023-12-18 23:45:08 +08:00
|
|
|
|
geminiRequest := GeminiChatRequest{
|
|
|
|
|
|
Contents: make([]GeminiChatContent, 0, len(textRequest.Messages)),
|
2024-01-10 13:56:10 +08:00
|
|
|
|
SafetySettings: []GeminiChatSafetySettings{
|
|
|
|
|
|
{
|
|
|
|
|
|
Category: "HARM_CATEGORY_HARASSMENT",
|
|
|
|
|
|
Threshold: common.GeminiSafetySetting,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Category: "HARM_CATEGORY_HATE_SPEECH",
|
|
|
|
|
|
Threshold: common.GeminiSafetySetting,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
|
|
|
|
|
Threshold: common.GeminiSafetySetting,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Category: "HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
|
|
|
|
Threshold: common.GeminiSafetySetting,
|
|
|
|
|
|
},
|
2024-12-20 13:20:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
Category: "HARM_CATEGORY_CIVIC_INTEGRITY",
|
|
|
|
|
|
Threshold: common.GeminiSafetySetting,
|
|
|
|
|
|
},
|
2024-01-10 13:56:10 +08:00
|
|
|
|
},
|
2023-12-18 23:45:08 +08:00
|
|
|
|
GenerationConfig: GeminiChatGenerationConfig{
|
|
|
|
|
|
Temperature: textRequest.Temperature,
|
|
|
|
|
|
TopP: textRequest.TopP,
|
|
|
|
|
|
MaxOutputTokens: textRequest.MaxTokens,
|
2024-12-24 20:46:02 +08:00
|
|
|
|
Seed: int64(textRequest.Seed),
|
2023-12-18 23:45:08 +08:00
|
|
|
|
},
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
|
|
|
|
|
|
// openaiContent.FuncToToolCalls()
|
2024-07-18 20:28:47 +08:00
|
|
|
|
if textRequest.Tools != nil {
|
|
|
|
|
|
functions := make([]dto.FunctionCall, 0, len(textRequest.Tools))
|
2024-12-12 17:58:25 +08:00
|
|
|
|
googleSearch := false
|
2024-12-24 20:46:02 +08:00
|
|
|
|
codeExecution := false
|
2024-07-18 20:28:47 +08:00
|
|
|
|
for _, tool := range textRequest.Tools {
|
2024-12-12 17:58:25 +08:00
|
|
|
|
if tool.Function.Name == "googleSearch" {
|
|
|
|
|
|
googleSearch = true
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if tool.Function.Name == "codeExecution" {
|
|
|
|
|
|
codeExecution = true
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2024-12-22 14:29:14 +08:00
|
|
|
|
if tool.Function.Parameters != nil {
|
|
|
|
|
|
params, ok := tool.Function.Parameters.(map[string]interface{})
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
if props, hasProps := params["properties"].(map[string]interface{}); hasProps {
|
|
|
|
|
|
if len(props) == 0 {
|
2024-12-22 14:35:21 +08:00
|
|
|
|
tool.Function.Parameters = nil
|
2024-12-22 14:29:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-07-18 20:28:47 +08:00
|
|
|
|
functions = append(functions, tool.Function)
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if codeExecution {
|
|
|
|
|
|
geminiRequest.Tools = append(geminiRequest.Tools, GeminiChatTool{
|
|
|
|
|
|
CodeExecution: make(map[string]string),
|
|
|
|
|
|
})
|
2024-12-12 17:58:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
if googleSearch {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
geminiRequest.Tools = append(geminiRequest.Tools, GeminiChatTool{
|
2024-12-12 17:58:25 +08:00
|
|
|
|
GoogleSearch: make(map[string]string),
|
|
|
|
|
|
})
|
2024-07-18 20:28:47 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if len(functions) > 0 {
|
|
|
|
|
|
geminiRequest.Tools = append(geminiRequest.Tools, GeminiChatTool{
|
|
|
|
|
|
FunctionDeclarations: functions,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
// common.SysLog("tools: " + fmt.Sprintf("%+v", geminiRequest.Tools))
|
|
|
|
|
|
// json_data, _ := json.Marshal(geminiRequest.Tools)
|
|
|
|
|
|
// common.SysLog("tools_json: " + string(json_data))
|
2024-07-18 20:28:47 +08:00
|
|
|
|
} else if textRequest.Functions != nil {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
geminiRequest.Tools = []GeminiChatTool{
|
2023-12-18 23:45:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
FunctionDeclarations: textRequest.Functions,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
|
2024-12-21 16:01:17 +08:00
|
|
|
|
if textRequest.ResponseFormat != nil && (textRequest.ResponseFormat.Type == "json_schema" || textRequest.ResponseFormat.Type == "json_object") {
|
|
|
|
|
|
geminiRequest.GenerationConfig.ResponseMimeType = "application/json"
|
|
|
|
|
|
|
|
|
|
|
|
if textRequest.ResponseFormat.JsonSchema != nil && textRequest.ResponseFormat.JsonSchema.Schema != nil {
|
|
|
|
|
|
cleanedSchema := removeAdditionalPropertiesWithDepth(textRequest.ResponseFormat.JsonSchema.Schema, 0)
|
|
|
|
|
|
geminiRequest.GenerationConfig.ResponseSchema = cleanedSchema
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-23 01:26:14 +08:00
|
|
|
|
tool_call_ids := make(map[string]string)
|
2024-12-24 20:46:02 +08:00
|
|
|
|
var system_content []string
|
2024-12-16 20:19:29 +08:00
|
|
|
|
//shouldAddDummyModelMessage := false
|
2023-12-18 23:45:08 +08:00
|
|
|
|
for _, message := range textRequest.Messages {
|
2024-12-16 20:19:29 +08:00
|
|
|
|
if message.Role == "system" {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
system_content = append(system_content, message.StringContent())
|
2024-12-16 20:19:29 +08:00
|
|
|
|
continue
|
2024-12-24 20:46:02 +08:00
|
|
|
|
} else if message.Role == "tool" || message.Role == "function" {
|
|
|
|
|
|
if len(geminiRequest.Contents) == 0 || geminiRequest.Contents[len(geminiRequest.Contents)-1].Role == "model" {
|
2024-12-23 01:26:14 +08:00
|
|
|
|
geminiRequest.Contents = append(geminiRequest.Contents, GeminiChatContent{
|
|
|
|
|
|
Role: "user",
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
var parts = &geminiRequest.Contents[len(geminiRequest.Contents)-1].Parts
|
|
|
|
|
|
name := ""
|
|
|
|
|
|
if message.Name != nil {
|
|
|
|
|
|
name = *message.Name
|
|
|
|
|
|
} else if val, exists := tool_call_ids[message.ToolCallId]; exists {
|
|
|
|
|
|
name = val
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
content := common.StrToMap(message.StringContent())
|
2024-12-23 01:26:14 +08:00
|
|
|
|
functionResp := &FunctionResponse{
|
2024-12-24 20:46:02 +08:00
|
|
|
|
Name: name,
|
|
|
|
|
|
Response: GeminiFunctionResponseContent{
|
|
|
|
|
|
Name: name,
|
|
|
|
|
|
Content: content,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
if content == nil {
|
|
|
|
|
|
functionResp.Response.Content = message.StringContent()
|
2024-12-23 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
*parts = append(*parts, GeminiPart{
|
|
|
|
|
|
FunctionResponse: functionResp,
|
|
|
|
|
|
})
|
|
|
|
|
|
continue
|
2024-12-16 20:19:29 +08:00
|
|
|
|
}
|
2024-12-22 16:20:30 +08:00
|
|
|
|
var parts []GeminiPart
|
2023-12-18 23:45:08 +08:00
|
|
|
|
content := GeminiChatContent{
|
|
|
|
|
|
Role: message.Role,
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
// isToolCall := false
|
2024-12-22 16:20:30 +08:00
|
|
|
|
if message.ToolCalls != nil {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
// message.Role = "model"
|
|
|
|
|
|
// isToolCall = true
|
2024-12-22 16:20:30 +08:00
|
|
|
|
for _, call := range message.ParseToolCalls() {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
args := map[string]interface{}{}
|
|
|
|
|
|
if call.Function.Arguments != "" {
|
|
|
|
|
|
if json.Unmarshal([]byte(call.Function.Arguments), &args) != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("invalid arguments for function %s, args: %s", call.Function.Name, call.Function.Arguments)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-22 16:20:30 +08:00
|
|
|
|
toolCall := GeminiPart{
|
|
|
|
|
|
FunctionCall: &FunctionCall{
|
|
|
|
|
|
FunctionName: call.Function.Name,
|
2024-12-24 20:46:02 +08:00
|
|
|
|
Arguments: args,
|
2024-12-22 16:20:30 +08:00
|
|
|
|
},
|
2024-12-20 21:36:23 +08:00
|
|
|
|
}
|
2024-12-22 16:20:30 +08:00
|
|
|
|
parts = append(parts, toolCall)
|
2024-12-23 01:26:14 +08:00
|
|
|
|
tool_call_ids[call.ID] = call.Function.Name
|
2024-12-22 16:20:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
|
|
|
|
|
|
openaiContent := message.ParseContent()
|
|
|
|
|
|
imageNum := 0
|
|
|
|
|
|
for _, part := range openaiContent {
|
|
|
|
|
|
if part.Type == dto.ContentTypeText {
|
|
|
|
|
|
if part.Text == "" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
parts = append(parts, GeminiPart{
|
|
|
|
|
|
Text: part.Text,
|
|
|
|
|
|
})
|
|
|
|
|
|
} else if part.Type == dto.ContentTypeImageURL {
|
|
|
|
|
|
imageNum += 1
|
|
|
|
|
|
|
|
|
|
|
|
if constant.GeminiVisionMaxImageNum != -1 && imageNum > constant.GeminiVisionMaxImageNum {
|
|
|
|
|
|
return nil, fmt.Errorf("too many images in the message, max allowed is %d", constant.GeminiVisionMaxImageNum)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 判断是否是url
|
|
|
|
|
|
if strings.HasPrefix(part.ImageUrl.(dto.MessageImageUrl).Url, "http") {
|
|
|
|
|
|
// 是url,获取图片的类型和base64编码的数据
|
|
|
|
|
|
mimeType, data, _ := service.GetImageFromUrl(part.ImageUrl.(dto.MessageImageUrl).Url)
|
2024-12-23 01:26:14 +08:00
|
|
|
|
parts = append(parts, GeminiPart{
|
2024-12-24 20:46:02 +08:00
|
|
|
|
InlineData: &GeminiInlineData{
|
|
|
|
|
|
MimeType: mimeType,
|
|
|
|
|
|
Data: data,
|
|
|
|
|
|
},
|
2024-12-23 01:26:14 +08:00
|
|
|
|
})
|
2024-12-24 20:46:02 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
_, format, base64String, err := service.DecodeBase64ImageData(part.ImageUrl.(dto.MessageImageUrl).Url)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("decode base64 image data failed: %s", err.Error())
|
2024-07-22 21:20:23 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
parts = append(parts, GeminiPart{
|
|
|
|
|
|
InlineData: &GeminiInlineData{
|
|
|
|
|
|
MimeType: "image/" + format,
|
|
|
|
|
|
Data: base64String,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
2024-07-22 21:20:23 +08:00
|
|
|
|
}
|
2023-12-27 16:32:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-23 01:26:14 +08:00
|
|
|
|
|
2023-12-27 16:32:54 +08:00
|
|
|
|
content.Parts = parts
|
|
|
|
|
|
|
2023-12-18 23:45:08 +08:00
|
|
|
|
// there's no assistant role in gemini and API shall vomit if Role is not user or model
|
|
|
|
|
|
if content.Role == "assistant" {
|
|
|
|
|
|
content.Role = "model"
|
|
|
|
|
|
}
|
|
|
|
|
|
geminiRequest.Contents = append(geminiRequest.Contents, content)
|
|
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
|
|
|
|
|
|
if len(system_content) > 0 {
|
|
|
|
|
|
geminiRequest.SystemInstructions = &GeminiChatContent{
|
|
|
|
|
|
Parts: []GeminiPart{
|
|
|
|
|
|
{
|
|
|
|
|
|
Text: strings.Join(system_content, "\n"),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-20 21:50:58 +08:00
|
|
|
|
return &geminiRequest, nil
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-21 16:01:17 +08:00
|
|
|
|
func removeAdditionalPropertiesWithDepth(schema interface{}, depth int) interface{} {
|
|
|
|
|
|
if depth >= 5 {
|
|
|
|
|
|
return schema
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v, ok := schema.(map[string]interface{})
|
|
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
|
|
return schema
|
|
|
|
|
|
}
|
2024-12-26 00:24:45 +08:00
|
|
|
|
// 删除所有的title字段
|
|
|
|
|
|
delete(v, "title")
|
2024-12-21 16:01:17 +08:00
|
|
|
|
// 如果type不为object和array,则直接返回
|
|
|
|
|
|
if typeVal, exists := v["type"]; !exists || (typeVal != "object" && typeVal != "array") {
|
|
|
|
|
|
return schema
|
|
|
|
|
|
}
|
|
|
|
|
|
switch v["type"] {
|
|
|
|
|
|
case "object":
|
|
|
|
|
|
delete(v, "additionalProperties")
|
|
|
|
|
|
// 处理 properties
|
|
|
|
|
|
if properties, ok := v["properties"].(map[string]interface{}); ok {
|
|
|
|
|
|
for key, value := range properties {
|
|
|
|
|
|
properties[key] = removeAdditionalPropertiesWithDepth(value, depth+1)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, field := range []string{"allOf", "anyOf", "oneOf"} {
|
|
|
|
|
|
if nested, ok := v[field].([]interface{}); ok {
|
|
|
|
|
|
for i, item := range nested {
|
|
|
|
|
|
nested[i] = removeAdditionalPropertiesWithDepth(item, depth+1)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
case "array":
|
|
|
|
|
|
if items, ok := v["items"].(map[string]interface{}); ok {
|
|
|
|
|
|
v["items"] = removeAdditionalPropertiesWithDepth(items, depth+1)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-24 20:46:02 +08:00
|
|
|
|
// func (g *GeminiChatResponse) GetResponseText() string {
|
|
|
|
|
|
// if g == nil {
|
|
|
|
|
|
// return ""
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if len(g.Candidates) > 0 && len(g.Candidates[0].Content.Parts) > 0 {
|
|
|
|
|
|
// return g.Candidates[0].Content.Parts[0].Text
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return ""
|
|
|
|
|
|
// }
|
2023-12-18 23:45:08 +08:00
|
|
|
|
|
2024-12-23 01:26:14 +08:00
|
|
|
|
func getToolCall(item *GeminiPart) *dto.ToolCall {
|
2024-07-18 20:28:47 +08:00
|
|
|
|
argsBytes, err := json.Marshal(item.FunctionCall.Arguments)
|
|
|
|
|
|
if err != nil {
|
2024-12-23 01:26:14 +08:00
|
|
|
|
//common.SysError("getToolCall failed: " + err.Error())
|
|
|
|
|
|
return nil
|
2024-07-18 20:28:47 +08:00
|
|
|
|
}
|
2024-12-23 01:26:14 +08:00
|
|
|
|
return &dto.ToolCall{
|
2024-07-18 20:28:47 +08:00
|
|
|
|
ID: fmt.Sprintf("call_%s", common.GetUUID()),
|
|
|
|
|
|
Type: "function",
|
|
|
|
|
|
Function: dto.FunctionCall{
|
2024-12-28 18:29:48 +08:00
|
|
|
|
// 不好评价,得去转义一下反斜杠,Gemini 的特性好像是,Google 返回的时候本身就会转义“\”
|
|
|
|
|
|
Arguments: strings.ReplaceAll(string(argsBytes), "\\\\", "\\"),
|
2024-07-18 20:28:47 +08:00
|
|
|
|
Name: item.FunctionCall.FunctionName,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-23 01:26:14 +08:00
|
|
|
|
// func getToolCalls(candidate *GeminiChatCandidate, index int) []dto.ToolCall {
|
|
|
|
|
|
// var toolCalls []dto.ToolCall
|
|
|
|
|
|
|
|
|
|
|
|
// item := candidate.Content.Parts[index]
|
|
|
|
|
|
// if item.FunctionCall == nil {
|
|
|
|
|
|
// return toolCalls
|
|
|
|
|
|
// }
|
|
|
|
|
|
// argsBytes, err := json.Marshal(item.FunctionCall.Arguments)
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
// //common.SysError("getToolCalls failed: " + err.Error())
|
|
|
|
|
|
// return toolCalls
|
|
|
|
|
|
// }
|
|
|
|
|
|
// toolCall := dto.ToolCall{
|
|
|
|
|
|
// ID: fmt.Sprintf("call_%s", common.GetUUID()),
|
|
|
|
|
|
// Type: "function",
|
|
|
|
|
|
// Function: dto.FunctionCall{
|
|
|
|
|
|
// Arguments: string(argsBytes),
|
|
|
|
|
|
// Name: item.FunctionCall.FunctionName,
|
|
|
|
|
|
// },
|
|
|
|
|
|
// }
|
|
|
|
|
|
// toolCalls = append(toolCalls, toolCall)
|
|
|
|
|
|
// return toolCalls
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2024-02-29 01:08:18 +08:00
|
|
|
|
func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResponse {
|
|
|
|
|
|
fullTextResponse := dto.OpenAITextResponse{
|
2023-12-18 23:45:08 +08:00
|
|
|
|
Id: fmt.Sprintf("chatcmpl-%s", common.GetUUID()),
|
|
|
|
|
|
Object: "chat.completion",
|
|
|
|
|
|
Created: common.GetTimestamp(),
|
2024-02-29 01:08:18 +08:00
|
|
|
|
Choices: make([]dto.OpenAITextResponseChoice, 0, len(response.Candidates)),
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
content, _ := json.Marshal("")
|
2024-12-24 20:46:02 +08:00
|
|
|
|
is_tool_call := false
|
|
|
|
|
|
for _, candidate := range response.Candidates {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
choice := dto.OpenAITextResponseChoice{
|
2024-12-24 20:46:02 +08:00
|
|
|
|
Index: int(candidate.Index),
|
2024-02-29 01:08:18 +08:00
|
|
|
|
Message: dto.Message{
|
2023-12-18 23:45:08 +08:00
|
|
|
|
Role: "assistant",
|
|
|
|
|
|
Content: content,
|
|
|
|
|
|
},
|
2024-12-06 14:31:27 +08:00
|
|
|
|
FinishReason: constant.FinishReasonStop,
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
if len(candidate.Content.Parts) > 0 {
|
2024-12-23 01:26:14 +08:00
|
|
|
|
var texts []string
|
|
|
|
|
|
var tool_calls []dto.ToolCall
|
|
|
|
|
|
for _, part := range candidate.Content.Parts {
|
|
|
|
|
|
if part.FunctionCall != nil {
|
|
|
|
|
|
choice.FinishReason = constant.FinishReasonToolCalls
|
|
|
|
|
|
if call := getToolCall(&part); call != nil {
|
|
|
|
|
|
tool_calls = append(tool_calls, *call)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if part.ExecutableCode != nil {
|
|
|
|
|
|
texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```")
|
|
|
|
|
|
} else if part.CodeExecutionResult != nil {
|
|
|
|
|
|
texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 过滤掉空行
|
|
|
|
|
|
if part.Text != "\n" {
|
|
|
|
|
|
texts = append(texts, part.Text)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-20 13:20:07 +08:00
|
|
|
|
}
|
2024-07-18 20:28:47 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if len(tool_calls) > 0 {
|
|
|
|
|
|
choice.Message.SetToolCalls(tool_calls)
|
|
|
|
|
|
is_tool_call = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-23 01:26:14 +08:00
|
|
|
|
choice.Message.SetStringContent(strings.Join(texts, "\n"))
|
2024-12-24 20:46:02 +08:00
|
|
|
|
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if candidate.FinishReason != nil {
|
|
|
|
|
|
switch *candidate.FinishReason {
|
|
|
|
|
|
case "STOP":
|
|
|
|
|
|
choice.FinishReason = constant.FinishReasonStop
|
|
|
|
|
|
case "MAX_TOKENS":
|
|
|
|
|
|
choice.FinishReason = constant.FinishReasonLength
|
|
|
|
|
|
default:
|
|
|
|
|
|
choice.FinishReason = constant.FinishReasonContentFilter
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if is_tool_call {
|
|
|
|
|
|
choice.FinishReason = constant.FinishReasonToolCalls
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 23:45:08 +08:00
|
|
|
|
fullTextResponse.Choices = append(fullTextResponse.Choices, choice)
|
|
|
|
|
|
}
|
|
|
|
|
|
return &fullTextResponse
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-24 20:46:02 +08:00
|
|
|
|
func streamResponseGeminiChat2OpenAI(geminiResponse *GeminiChatResponse) (*dto.ChatCompletionsStreamResponse, bool) {
|
|
|
|
|
|
choices := make([]dto.ChatCompletionsStreamResponseChoice, 0, len(geminiResponse.Candidates))
|
|
|
|
|
|
is_stop := false
|
|
|
|
|
|
for _, candidate := range geminiResponse.Candidates {
|
|
|
|
|
|
if candidate.FinishReason != nil && *candidate.FinishReason == "STOP" {
|
|
|
|
|
|
is_stop = true
|
|
|
|
|
|
candidate.FinishReason = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
choice := dto.ChatCompletionsStreamResponseChoice{
|
|
|
|
|
|
Index: int(candidate.Index),
|
|
|
|
|
|
Delta: dto.ChatCompletionsStreamResponseChoiceDelta{
|
|
|
|
|
|
Role: "assistant",
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
2024-12-23 01:26:14 +08:00
|
|
|
|
var texts []string
|
2024-12-24 20:46:02 +08:00
|
|
|
|
isTools := false
|
|
|
|
|
|
if candidate.FinishReason != nil {
|
|
|
|
|
|
// p := GeminiConvertFinishReason(*candidate.FinishReason)
|
|
|
|
|
|
switch *candidate.FinishReason {
|
|
|
|
|
|
case "STOP":
|
|
|
|
|
|
choice.FinishReason = &constant.FinishReasonStop
|
|
|
|
|
|
case "MAX_TOKENS":
|
|
|
|
|
|
choice.FinishReason = &constant.FinishReasonLength
|
|
|
|
|
|
default:
|
|
|
|
|
|
choice.FinishReason = &constant.FinishReasonContentFilter
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, part := range candidate.Content.Parts {
|
2024-12-23 01:26:14 +08:00
|
|
|
|
if part.FunctionCall != nil {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
isTools = true
|
2024-12-23 01:26:14 +08:00
|
|
|
|
if call := getToolCall(&part); call != nil {
|
2024-12-28 17:46:56 +08:00
|
|
|
|
call.SetIndex(len(choice.Delta.ToolCalls))
|
2024-12-24 20:46:02 +08:00
|
|
|
|
choice.Delta.ToolCalls = append(choice.Delta.ToolCalls, *call)
|
2024-12-23 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if part.ExecutableCode != nil {
|
|
|
|
|
|
texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```\n")
|
|
|
|
|
|
} else if part.CodeExecutionResult != nil {
|
|
|
|
|
|
texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```\n")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if part.Text != "\n" {
|
|
|
|
|
|
texts = append(texts, part.Text)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-20 20:24:49 +08:00
|
|
|
|
}
|
2024-12-23 01:26:14 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
choice.Delta.SetContentString(strings.Join(texts, "\n"))
|
|
|
|
|
|
if isTools {
|
|
|
|
|
|
choice.FinishReason = &constant.FinishReasonToolCalls
|
2024-12-23 01:26:14 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
choices = append(choices, choice)
|
2024-07-18 20:28:47 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
|
2024-02-29 01:08:18 +08:00
|
|
|
|
var response dto.ChatCompletionsStreamResponse
|
2023-12-18 23:45:08 +08:00
|
|
|
|
response.Object = "chat.completion.chunk"
|
|
|
|
|
|
response.Model = "gemini"
|
2024-12-24 20:46:02 +08:00
|
|
|
|
response.Choices = choices
|
|
|
|
|
|
return &response, is_stop
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-27 20:19:51 +08:00
|
|
|
|
func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
|
2024-12-24 20:46:02 +08:00
|
|
|
|
// responseText := ""
|
2024-07-10 16:01:09 +08:00
|
|
|
|
id := fmt.Sprintf("chatcmpl-%s", common.GetUUID())
|
|
|
|
|
|
createAt := common.GetTimestamp()
|
|
|
|
|
|
var usage = &dto.Usage{}
|
2023-12-18 23:45:08 +08:00
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
2024-07-18 20:28:47 +08:00
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
|
|
|
|
|
|
|
service.SetEventStreamHeaders(c)
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
|
data := scanner.Text()
|
|
|
|
|
|
info.SetFirstResponseTime()
|
|
|
|
|
|
data = strings.TrimSpace(data)
|
|
|
|
|
|
if !strings.HasPrefix(data, "data: ") {
|
|
|
|
|
|
continue
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
2024-07-18 20:28:47 +08:00
|
|
|
|
data = strings.TrimPrefix(data, "data: ")
|
|
|
|
|
|
data = strings.TrimSuffix(data, "\"")
|
|
|
|
|
|
var geminiResponse GeminiChatResponse
|
|
|
|
|
|
err := json.Unmarshal([]byte(data), &geminiResponse)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
common.LogError(c, "error unmarshalling stream response: "+err.Error())
|
|
|
|
|
|
continue
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
2024-07-18 20:28:47 +08:00
|
|
|
|
|
2024-12-24 20:46:02 +08:00
|
|
|
|
response, is_stop := streamResponseGeminiChat2OpenAI(&geminiResponse)
|
2024-07-18 20:28:47 +08:00
|
|
|
|
response.Id = id
|
|
|
|
|
|
response.Created = createAt
|
2024-12-23 00:02:15 +08:00
|
|
|
|
response.Model = info.UpstreamModelName
|
2024-12-24 20:46:02 +08:00
|
|
|
|
// responseText += response.Choices[0].Delta.GetContentString()
|
2024-07-18 20:28:47 +08:00
|
|
|
|
if geminiResponse.UsageMetadata.TotalTokenCount != 0 {
|
|
|
|
|
|
usage.PromptTokens = geminiResponse.UsageMetadata.PromptTokenCount
|
|
|
|
|
|
usage.CompletionTokens = geminiResponse.UsageMetadata.CandidatesTokenCount
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
2024-07-18 20:28:47 +08:00
|
|
|
|
err = service.ObjectData(c, response)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
common.LogError(c, err.Error())
|
2024-07-10 16:01:09 +08:00
|
|
|
|
}
|
2024-12-24 20:46:02 +08:00
|
|
|
|
if is_stop {
|
|
|
|
|
|
response := service.GenerateStopResponse(id, createAt, info.UpstreamModelName, constant.FinishReasonStop)
|
|
|
|
|
|
service.ObjectData(c, response)
|
|
|
|
|
|
}
|
2024-07-10 16:01:09 +08:00
|
|
|
|
}
|
2024-07-19 17:16:20 +08:00
|
|
|
|
|
2024-12-24 20:46:02 +08:00
|
|
|
|
var response *dto.ChatCompletionsStreamResponse
|
2024-07-19 17:16:20 +08:00
|
|
|
|
|
2024-07-18 20:28:47 +08:00
|
|
|
|
usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
|
2024-12-24 20:46:02 +08:00
|
|
|
|
usage.PromptTokensDetails.TextTokens = usage.PromptTokens
|
|
|
|
|
|
usage.CompletionTokenDetails.TextTokens = usage.CompletionTokens
|
2024-07-18 20:28:47 +08:00
|
|
|
|
|
2024-07-10 16:01:09 +08:00
|
|
|
|
if info.ShouldIncludeUsage {
|
2024-07-19 17:16:20 +08:00
|
|
|
|
response = service.GenerateFinalUsageResponse(id, createAt, info.UpstreamModelName, *usage)
|
2024-07-10 16:01:09 +08:00
|
|
|
|
err := service.ObjectData(c, response)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
common.SysError("send final response failed: " + err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
service.Done(c)
|
2024-07-18 20:28:47 +08:00
|
|
|
|
resp.Body.Close()
|
2024-07-10 16:01:09 +08:00
|
|
|
|
return nil, usage
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-23 00:02:15 +08:00
|
|
|
|
func GeminiChatHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
|
2023-12-18 23:45:08 +08:00
|
|
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return service.OpenAIErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
err = resp.Body.Close()
|
|
|
|
|
|
if err != nil {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return service.OpenAIErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
var geminiResponse GeminiChatResponse
|
|
|
|
|
|
err = json.Unmarshal(responseBody, &geminiResponse)
|
|
|
|
|
|
if err != nil {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
if len(geminiResponse.Candidates) == 0 {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return &dto.OpenAIErrorWithStatusCode{
|
2024-03-06 17:41:55 +08:00
|
|
|
|
Error: dto.OpenAIError{
|
2023-12-18 23:45:08 +08:00
|
|
|
|
Message: "No candidates returned",
|
|
|
|
|
|
Type: "server_error",
|
|
|
|
|
|
Param: "",
|
|
|
|
|
|
Code: 500,
|
|
|
|
|
|
},
|
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
fullTextResponse := responseGeminiChat2OpenAI(&geminiResponse)
|
2024-12-23 00:02:15 +08:00
|
|
|
|
fullTextResponse.Model = info.UpstreamModelName
|
2024-02-29 01:08:18 +08:00
|
|
|
|
usage := dto.Usage{
|
2024-07-10 16:01:09 +08:00
|
|
|
|
PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
|
|
|
|
|
|
CompletionTokens: geminiResponse.UsageMetadata.CandidatesTokenCount,
|
|
|
|
|
|
TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount,
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
fullTextResponse.Usage = usage
|
|
|
|
|
|
jsonResponse, err := json.Marshal(fullTextResponse)
|
|
|
|
|
|
if err != nil {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return service.OpenAIErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
2023-12-18 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
c.Writer.WriteHeader(resp.StatusCode)
|
|
|
|
|
|
_, err = c.Writer.Write(jsonResponse)
|
|
|
|
|
|
return nil, &usage
|
|
|
|
|
|
}
|