relay-gemini.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. package gemini
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "one-api/common"
  8. "one-api/constant"
  9. "one-api/dto"
  10. relaycommon "one-api/relay/common"
  11. "one-api/relay/helper"
  12. "one-api/service"
  13. "one-api/setting/model_setting"
  14. "strings"
  15. "unicode/utf8"
  16. "github.com/gin-gonic/gin"
  17. )
  18. // Setting safety to the lowest possible values since Gemini is already powerless enough
  19. func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest, info *relaycommon.RelayInfo) (*GeminiChatRequest, error) {
  20. geminiRequest := GeminiChatRequest{
  21. Contents: make([]GeminiChatContent, 0, len(textRequest.Messages)),
  22. GenerationConfig: GeminiChatGenerationConfig{
  23. Temperature: textRequest.Temperature,
  24. TopP: textRequest.TopP,
  25. MaxOutputTokens: textRequest.MaxTokens,
  26. Seed: int64(textRequest.Seed),
  27. },
  28. }
  29. if model_setting.IsGeminiModelSupportImagine(info.UpstreamModelName) {
  30. geminiRequest.GenerationConfig.ResponseModalities = []string{
  31. "TEXT",
  32. "IMAGE",
  33. }
  34. }
  35. if model_setting.GetGeminiSettings().ThinkingAdapterEnabled {
  36. if strings.HasSuffix(info.OriginModelName, "-thinking") {
  37. budgetTokens := model_setting.GetGeminiSettings().ThinkingAdapterBudgetTokensPercentage * float64(geminiRequest.GenerationConfig.MaxOutputTokens)
  38. if budgetTokens == 0 || budgetTokens > 24576 {
  39. budgetTokens = 24576
  40. }
  41. geminiRequest.GenerationConfig.ThinkingConfig = &GeminiThinkingConfig{
  42. ThinkingBudget: common.GetPointer(int(budgetTokens)),
  43. IncludeThoughts: true,
  44. }
  45. } else if strings.HasSuffix(info.OriginModelName, "-nothinking") {
  46. geminiRequest.GenerationConfig.ThinkingConfig = &GeminiThinkingConfig{
  47. ThinkingBudget: common.GetPointer(0),
  48. }
  49. }
  50. }
  51. safetySettings := make([]GeminiChatSafetySettings, 0, len(SafetySettingList))
  52. for _, category := range SafetySettingList {
  53. safetySettings = append(safetySettings, GeminiChatSafetySettings{
  54. Category: category,
  55. Threshold: model_setting.GetGeminiSafetySetting(category),
  56. })
  57. }
  58. geminiRequest.SafetySettings = safetySettings
  59. // openaiContent.FuncToToolCalls()
  60. if textRequest.Tools != nil {
  61. functions := make([]dto.FunctionRequest, 0, len(textRequest.Tools))
  62. googleSearch := false
  63. codeExecution := false
  64. for _, tool := range textRequest.Tools {
  65. if tool.Function.Name == "googleSearch" {
  66. googleSearch = true
  67. continue
  68. }
  69. if tool.Function.Name == "codeExecution" {
  70. codeExecution = true
  71. continue
  72. }
  73. if tool.Function.Parameters != nil {
  74. params, ok := tool.Function.Parameters.(map[string]interface{})
  75. if ok {
  76. if props, hasProps := params["properties"].(map[string]interface{}); hasProps {
  77. if len(props) == 0 {
  78. tool.Function.Parameters = nil
  79. }
  80. }
  81. }
  82. }
  83. // Clean the parameters before appending
  84. cleanedParams := cleanFunctionParameters(tool.Function.Parameters)
  85. tool.Function.Parameters = cleanedParams
  86. functions = append(functions, tool.Function)
  87. }
  88. if codeExecution {
  89. geminiRequest.Tools = append(geminiRequest.Tools, GeminiChatTool{
  90. CodeExecution: make(map[string]string),
  91. })
  92. }
  93. if googleSearch {
  94. geminiRequest.Tools = append(geminiRequest.Tools, GeminiChatTool{
  95. GoogleSearch: make(map[string]string),
  96. })
  97. }
  98. if len(functions) > 0 {
  99. geminiRequest.Tools = append(geminiRequest.Tools, GeminiChatTool{
  100. FunctionDeclarations: functions,
  101. })
  102. }
  103. // common.SysLog("tools: " + fmt.Sprintf("%+v", geminiRequest.Tools))
  104. // json_data, _ := json.Marshal(geminiRequest.Tools)
  105. // common.SysLog("tools_json: " + string(json_data))
  106. } else if textRequest.Functions != nil {
  107. //geminiRequest.Tools = []GeminiChatTool{
  108. // {
  109. // FunctionDeclarations: textRequest.Functions,
  110. // },
  111. //}
  112. }
  113. if textRequest.ResponseFormat != nil && (textRequest.ResponseFormat.Type == "json_schema" || textRequest.ResponseFormat.Type == "json_object") {
  114. geminiRequest.GenerationConfig.ResponseMimeType = "application/json"
  115. if textRequest.ResponseFormat.JsonSchema != nil && textRequest.ResponseFormat.JsonSchema.Schema != nil {
  116. cleanedSchema := removeAdditionalPropertiesWithDepth(textRequest.ResponseFormat.JsonSchema.Schema, 0)
  117. geminiRequest.GenerationConfig.ResponseSchema = cleanedSchema
  118. }
  119. }
  120. tool_call_ids := make(map[string]string)
  121. var system_content []string
  122. //shouldAddDummyModelMessage := false
  123. for _, message := range textRequest.Messages {
  124. if message.Role == "system" {
  125. system_content = append(system_content, message.StringContent())
  126. continue
  127. } else if message.Role == "tool" || message.Role == "function" {
  128. if len(geminiRequest.Contents) == 0 || geminiRequest.Contents[len(geminiRequest.Contents)-1].Role == "model" {
  129. geminiRequest.Contents = append(geminiRequest.Contents, GeminiChatContent{
  130. Role: "user",
  131. })
  132. }
  133. var parts = &geminiRequest.Contents[len(geminiRequest.Contents)-1].Parts
  134. name := ""
  135. if message.Name != nil {
  136. name = *message.Name
  137. } else if val, exists := tool_call_ids[message.ToolCallId]; exists {
  138. name = val
  139. }
  140. content := common.StrToMap(message.StringContent())
  141. functionResp := &FunctionResponse{
  142. Name: name,
  143. Response: GeminiFunctionResponseContent{
  144. Name: name,
  145. Content: content,
  146. },
  147. }
  148. if content == nil {
  149. functionResp.Response.Content = message.StringContent()
  150. }
  151. *parts = append(*parts, GeminiPart{
  152. FunctionResponse: functionResp,
  153. })
  154. continue
  155. }
  156. var parts []GeminiPart
  157. content := GeminiChatContent{
  158. Role: message.Role,
  159. }
  160. // isToolCall := false
  161. if message.ToolCalls != nil {
  162. // message.Role = "model"
  163. // isToolCall = true
  164. for _, call := range message.ParseToolCalls() {
  165. args := map[string]interface{}{}
  166. if call.Function.Arguments != "" {
  167. if json.Unmarshal([]byte(call.Function.Arguments), &args) != nil {
  168. return nil, fmt.Errorf("invalid arguments for function %s, args: %s", call.Function.Name, call.Function.Arguments)
  169. }
  170. }
  171. toolCall := GeminiPart{
  172. FunctionCall: &FunctionCall{
  173. FunctionName: call.Function.Name,
  174. Arguments: args,
  175. },
  176. }
  177. parts = append(parts, toolCall)
  178. tool_call_ids[call.ID] = call.Function.Name
  179. }
  180. }
  181. openaiContent := message.ParseContent()
  182. imageNum := 0
  183. for _, part := range openaiContent {
  184. if part.Type == dto.ContentTypeText {
  185. if part.Text == "" {
  186. continue
  187. }
  188. parts = append(parts, GeminiPart{
  189. Text: part.Text,
  190. })
  191. } else if part.Type == dto.ContentTypeImageURL {
  192. imageNum += 1
  193. if constant.GeminiVisionMaxImageNum != -1 && imageNum > constant.GeminiVisionMaxImageNum {
  194. return nil, fmt.Errorf("too many images in the message, max allowed is %d", constant.GeminiVisionMaxImageNum)
  195. }
  196. // 判断是否是url
  197. if strings.HasPrefix(part.GetImageMedia().Url, "http") {
  198. // 是url,获取图片的类型和base64编码的数据
  199. fileData, err := service.GetFileBase64FromUrl(part.GetImageMedia().Url)
  200. if err != nil {
  201. return nil, fmt.Errorf("get file base64 from url failed: %s", err.Error())
  202. }
  203. parts = append(parts, GeminiPart{
  204. InlineData: &GeminiInlineData{
  205. MimeType: fileData.MimeType,
  206. Data: fileData.Base64Data,
  207. },
  208. })
  209. } else {
  210. format, base64String, err := service.DecodeBase64FileData(part.GetImageMedia().Url)
  211. if err != nil {
  212. return nil, fmt.Errorf("decode base64 image data failed: %s", err.Error())
  213. }
  214. parts = append(parts, GeminiPart{
  215. InlineData: &GeminiInlineData{
  216. MimeType: format,
  217. Data: base64String,
  218. },
  219. })
  220. }
  221. } else if part.Type == dto.ContentTypeFile {
  222. if part.GetFile().FileId != "" {
  223. return nil, fmt.Errorf("only base64 file is supported in gemini")
  224. }
  225. format, base64String, err := service.DecodeBase64FileData(part.GetFile().FileData)
  226. if err != nil {
  227. return nil, fmt.Errorf("decode base64 file data failed: %s", err.Error())
  228. }
  229. parts = append(parts, GeminiPart{
  230. InlineData: &GeminiInlineData{
  231. MimeType: format,
  232. Data: base64String,
  233. },
  234. })
  235. } else if part.Type == dto.ContentTypeInputAudio {
  236. if part.GetInputAudio().Data == "" {
  237. return nil, fmt.Errorf("only base64 audio is supported in gemini")
  238. }
  239. format, base64String, err := service.DecodeBase64FileData(part.GetInputAudio().Data)
  240. if err != nil {
  241. return nil, fmt.Errorf("decode base64 audio data failed: %s", err.Error())
  242. }
  243. parts = append(parts, GeminiPart{
  244. InlineData: &GeminiInlineData{
  245. MimeType: format,
  246. Data: base64String,
  247. },
  248. })
  249. }
  250. }
  251. content.Parts = parts
  252. // there's no assistant role in gemini and API shall vomit if Role is not user or model
  253. if content.Role == "assistant" {
  254. content.Role = "model"
  255. }
  256. geminiRequest.Contents = append(geminiRequest.Contents, content)
  257. }
  258. if len(system_content) > 0 {
  259. geminiRequest.SystemInstructions = &GeminiChatContent{
  260. Parts: []GeminiPart{
  261. {
  262. Text: strings.Join(system_content, "\n"),
  263. },
  264. },
  265. }
  266. }
  267. return &geminiRequest, nil
  268. }
  269. // cleanFunctionParameters recursively removes unsupported fields from Gemini function parameters.
  270. func cleanFunctionParameters(params interface{}) interface{} {
  271. if params == nil {
  272. return nil
  273. }
  274. paramMap, ok := params.(map[string]interface{})
  275. if !ok {
  276. // Not a map, return as is (e.g., could be an array or primitive)
  277. return params
  278. }
  279. // Create a copy to avoid modifying the original
  280. cleanedMap := make(map[string]interface{})
  281. for k, v := range paramMap {
  282. cleanedMap[k] = v
  283. }
  284. // Remove unsupported root-level fields
  285. delete(cleanedMap, "default")
  286. delete(cleanedMap, "exclusiveMaximum")
  287. delete(cleanedMap, "exclusiveMinimum")
  288. delete(cleanedMap, "$schema")
  289. delete(cleanedMap, "additionalProperties")
  290. // Clean properties
  291. if props, ok := cleanedMap["properties"].(map[string]interface{}); ok && props != nil {
  292. cleanedProps := make(map[string]interface{})
  293. for propName, propValue := range props {
  294. propMap, ok := propValue.(map[string]interface{})
  295. if !ok {
  296. cleanedProps[propName] = propValue // Keep non-map properties
  297. continue
  298. }
  299. // Create a copy of the property map
  300. cleanedPropMap := make(map[string]interface{})
  301. for k, v := range propMap {
  302. cleanedPropMap[k] = v
  303. }
  304. // Remove unsupported fields
  305. delete(cleanedPropMap, "default")
  306. delete(cleanedPropMap, "exclusiveMaximum")
  307. delete(cleanedPropMap, "exclusiveMinimum")
  308. delete(cleanedPropMap, "$schema")
  309. delete(cleanedPropMap, "additionalProperties")
  310. // Check and clean 'format' for string types
  311. if propType, typeExists := cleanedPropMap["type"].(string); typeExists && propType == "string" {
  312. if formatValue, formatExists := cleanedPropMap["format"].(string); formatExists {
  313. if formatValue != "enum" && formatValue != "date-time" {
  314. delete(cleanedPropMap, "format")
  315. }
  316. }
  317. }
  318. // Recursively clean nested properties within this property if it's an object/array
  319. // Check the type before recursing
  320. if propType, typeExists := cleanedPropMap["type"].(string); typeExists && (propType == "object" || propType == "array") {
  321. cleanedProps[propName] = cleanFunctionParameters(cleanedPropMap)
  322. } else {
  323. cleanedProps[propName] = cleanedPropMap // Assign the cleaned map back if not recursing
  324. }
  325. }
  326. cleanedMap["properties"] = cleanedProps
  327. }
  328. // Recursively clean items in arrays if needed (e.g., type: array, items: { ... })
  329. if items, ok := cleanedMap["items"].(map[string]interface{}); ok && items != nil {
  330. cleanedMap["items"] = cleanFunctionParameters(items)
  331. }
  332. // Also handle items if it's an array of schemas
  333. if itemsArray, ok := cleanedMap["items"].([]interface{}); ok {
  334. cleanedItemsArray := make([]interface{}, len(itemsArray))
  335. for i, item := range itemsArray {
  336. cleanedItemsArray[i] = cleanFunctionParameters(item)
  337. }
  338. cleanedMap["items"] = cleanedItemsArray
  339. }
  340. // Recursively clean other schema composition keywords if necessary
  341. for _, field := range []string{"allOf", "anyOf", "oneOf"} {
  342. if nested, ok := cleanedMap[field].([]interface{}); ok {
  343. cleanedNested := make([]interface{}, len(nested))
  344. for i, item := range nested {
  345. cleanedNested[i] = cleanFunctionParameters(item)
  346. }
  347. cleanedMap[field] = cleanedNested
  348. }
  349. }
  350. return cleanedMap
  351. }
  352. func removeAdditionalPropertiesWithDepth(schema interface{}, depth int) interface{} {
  353. if depth >= 5 {
  354. return schema
  355. }
  356. v, ok := schema.(map[string]interface{})
  357. if !ok || len(v) == 0 {
  358. return schema
  359. }
  360. // 删除所有的title字段
  361. delete(v, "title")
  362. delete(v, "$schema")
  363. // 如果type不为object和array,则直接返回
  364. if typeVal, exists := v["type"]; !exists || (typeVal != "object" && typeVal != "array") {
  365. return schema
  366. }
  367. switch v["type"] {
  368. case "object":
  369. delete(v, "additionalProperties")
  370. // 处理 properties
  371. if properties, ok := v["properties"].(map[string]interface{}); ok {
  372. for key, value := range properties {
  373. properties[key] = removeAdditionalPropertiesWithDepth(value, depth+1)
  374. }
  375. }
  376. for _, field := range []string{"allOf", "anyOf", "oneOf"} {
  377. if nested, ok := v[field].([]interface{}); ok {
  378. for i, item := range nested {
  379. nested[i] = removeAdditionalPropertiesWithDepth(item, depth+1)
  380. }
  381. }
  382. }
  383. case "array":
  384. if items, ok := v["items"].(map[string]interface{}); ok {
  385. v["items"] = removeAdditionalPropertiesWithDepth(items, depth+1)
  386. }
  387. }
  388. return v
  389. }
  390. func unescapeString(s string) (string, error) {
  391. var result []rune
  392. escaped := false
  393. i := 0
  394. for i < len(s) {
  395. r, size := utf8.DecodeRuneInString(s[i:]) // 正确解码UTF-8字符
  396. if r == utf8.RuneError {
  397. return "", fmt.Errorf("invalid UTF-8 encoding")
  398. }
  399. if escaped {
  400. // 如果是转义符后的字符,检查其类型
  401. switch r {
  402. case '"':
  403. result = append(result, '"')
  404. case '\\':
  405. result = append(result, '\\')
  406. case '/':
  407. result = append(result, '/')
  408. case 'b':
  409. result = append(result, '\b')
  410. case 'f':
  411. result = append(result, '\f')
  412. case 'n':
  413. result = append(result, '\n')
  414. case 'r':
  415. result = append(result, '\r')
  416. case 't':
  417. result = append(result, '\t')
  418. case '\'':
  419. result = append(result, '\'')
  420. default:
  421. // 如果遇到一个非法的转义字符,直接按原样输出
  422. result = append(result, '\\', r)
  423. }
  424. escaped = false
  425. } else {
  426. if r == '\\' {
  427. escaped = true // 记录反斜杠作为转义符
  428. } else {
  429. result = append(result, r)
  430. }
  431. }
  432. i += size // 移动到下一个字符
  433. }
  434. return string(result), nil
  435. }
  436. func unescapeMapOrSlice(data interface{}) interface{} {
  437. switch v := data.(type) {
  438. case map[string]interface{}:
  439. for k, val := range v {
  440. v[k] = unescapeMapOrSlice(val)
  441. }
  442. case []interface{}:
  443. for i, val := range v {
  444. v[i] = unescapeMapOrSlice(val)
  445. }
  446. case string:
  447. if unescaped, err := unescapeString(v); err != nil {
  448. return v
  449. } else {
  450. return unescaped
  451. }
  452. }
  453. return data
  454. }
  455. func getResponseToolCall(item *GeminiPart) *dto.ToolCallResponse {
  456. var argsBytes []byte
  457. var err error
  458. if result, ok := item.FunctionCall.Arguments.(map[string]interface{}); ok {
  459. argsBytes, err = json.Marshal(unescapeMapOrSlice(result))
  460. } else {
  461. argsBytes, err = json.Marshal(item.FunctionCall.Arguments)
  462. }
  463. if err != nil {
  464. return nil
  465. }
  466. return &dto.ToolCallResponse{
  467. ID: fmt.Sprintf("call_%s", common.GetUUID()),
  468. Type: "function",
  469. Function: dto.FunctionResponse{
  470. Arguments: string(argsBytes),
  471. Name: item.FunctionCall.FunctionName,
  472. },
  473. }
  474. }
  475. func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResponse {
  476. fullTextResponse := dto.OpenAITextResponse{
  477. Id: fmt.Sprintf("chatcmpl-%s", common.GetUUID()),
  478. Object: "chat.completion",
  479. Created: common.GetTimestamp(),
  480. Choices: make([]dto.OpenAITextResponseChoice, 0, len(response.Candidates)),
  481. }
  482. content, _ := json.Marshal("")
  483. isToolCall := false
  484. for _, candidate := range response.Candidates {
  485. choice := dto.OpenAITextResponseChoice{
  486. Index: int(candidate.Index),
  487. Message: dto.Message{
  488. Role: "assistant",
  489. Content: content,
  490. },
  491. FinishReason: constant.FinishReasonStop,
  492. }
  493. if len(candidate.Content.Parts) > 0 {
  494. var texts []string
  495. var toolCalls []dto.ToolCallResponse
  496. for _, part := range candidate.Content.Parts {
  497. if part.FunctionCall != nil {
  498. choice.FinishReason = constant.FinishReasonToolCalls
  499. if call := getResponseToolCall(&part); call != nil {
  500. toolCalls = append(toolCalls, *call)
  501. }
  502. } else {
  503. if part.ExecutableCode != nil {
  504. texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```")
  505. } else if part.CodeExecutionResult != nil {
  506. texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```")
  507. } else {
  508. // 过滤掉空行
  509. if part.Text != "\n" {
  510. texts = append(texts, part.Text)
  511. }
  512. }
  513. }
  514. }
  515. if len(toolCalls) > 0 {
  516. choice.Message.SetToolCalls(toolCalls)
  517. isToolCall = true
  518. }
  519. choice.Message.SetStringContent(strings.Join(texts, "\n"))
  520. }
  521. if candidate.FinishReason != nil {
  522. switch *candidate.FinishReason {
  523. case "STOP":
  524. choice.FinishReason = constant.FinishReasonStop
  525. case "MAX_TOKENS":
  526. choice.FinishReason = constant.FinishReasonLength
  527. default:
  528. choice.FinishReason = constant.FinishReasonContentFilter
  529. }
  530. }
  531. if isToolCall {
  532. choice.FinishReason = constant.FinishReasonToolCalls
  533. }
  534. fullTextResponse.Choices = append(fullTextResponse.Choices, choice)
  535. }
  536. return &fullTextResponse
  537. }
  538. func streamResponseGeminiChat2OpenAI(geminiResponse *GeminiChatResponse) (*dto.ChatCompletionsStreamResponse, bool, bool) {
  539. choices := make([]dto.ChatCompletionsStreamResponseChoice, 0, len(geminiResponse.Candidates))
  540. isStop := false
  541. hasImage := false
  542. for _, candidate := range geminiResponse.Candidates {
  543. if candidate.FinishReason != nil && *candidate.FinishReason == "STOP" {
  544. isStop = true
  545. candidate.FinishReason = nil
  546. }
  547. choice := dto.ChatCompletionsStreamResponseChoice{
  548. Index: int(candidate.Index),
  549. Delta: dto.ChatCompletionsStreamResponseChoiceDelta{
  550. Role: "assistant",
  551. },
  552. }
  553. var texts []string
  554. isTools := false
  555. if candidate.FinishReason != nil {
  556. // p := GeminiConvertFinishReason(*candidate.FinishReason)
  557. switch *candidate.FinishReason {
  558. case "STOP":
  559. choice.FinishReason = &constant.FinishReasonStop
  560. case "MAX_TOKENS":
  561. choice.FinishReason = &constant.FinishReasonLength
  562. default:
  563. choice.FinishReason = &constant.FinishReasonContentFilter
  564. }
  565. }
  566. for _, part := range candidate.Content.Parts {
  567. if part.InlineData != nil {
  568. if strings.HasPrefix(part.InlineData.MimeType, "image") {
  569. imgText := "![image](data:" + part.InlineData.MimeType + ";base64," + part.InlineData.Data + ")"
  570. texts = append(texts, imgText)
  571. hasImage = true
  572. }
  573. } else if part.FunctionCall != nil {
  574. isTools = true
  575. if call := getResponseToolCall(&part); call != nil {
  576. call.SetIndex(len(choice.Delta.ToolCalls))
  577. choice.Delta.ToolCalls = append(choice.Delta.ToolCalls, *call)
  578. }
  579. } else {
  580. if part.ExecutableCode != nil {
  581. texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```\n")
  582. } else if part.CodeExecutionResult != nil {
  583. texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```\n")
  584. } else {
  585. if part.Text != "\n" {
  586. texts = append(texts, part.Text)
  587. }
  588. }
  589. }
  590. }
  591. choice.Delta.SetContentString(strings.Join(texts, "\n"))
  592. if isTools {
  593. choice.FinishReason = &constant.FinishReasonToolCalls
  594. }
  595. choices = append(choices, choice)
  596. }
  597. var response dto.ChatCompletionsStreamResponse
  598. response.Object = "chat.completion.chunk"
  599. response.Choices = choices
  600. return &response, isStop, hasImage
  601. }
  602. func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
  603. // responseText := ""
  604. id := fmt.Sprintf("chatcmpl-%s", common.GetUUID())
  605. createAt := common.GetTimestamp()
  606. var usage = &dto.Usage{}
  607. var imageCount int
  608. helper.StreamScannerHandler(c, resp, info, func(data string) bool {
  609. var geminiResponse GeminiChatResponse
  610. err := common.DecodeJsonStr(data, &geminiResponse)
  611. if err != nil {
  612. common.LogError(c, "error unmarshalling stream response: "+err.Error())
  613. return false
  614. }
  615. response, isStop, hasImage := streamResponseGeminiChat2OpenAI(&geminiResponse)
  616. if hasImage {
  617. imageCount++
  618. }
  619. response.Id = id
  620. response.Created = createAt
  621. response.Model = info.UpstreamModelName
  622. if geminiResponse.UsageMetadata.TotalTokenCount != 0 {
  623. usage.PromptTokens = geminiResponse.UsageMetadata.PromptTokenCount
  624. usage.CompletionTokens = geminiResponse.UsageMetadata.CandidatesTokenCount
  625. usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
  626. usage.TotalTokens = geminiResponse.UsageMetadata.TotalTokenCount
  627. }
  628. err = helper.ObjectData(c, response)
  629. if err != nil {
  630. common.LogError(c, err.Error())
  631. }
  632. if isStop {
  633. response := helper.GenerateStopResponse(id, createAt, info.UpstreamModelName, constant.FinishReasonStop)
  634. helper.ObjectData(c, response)
  635. }
  636. return true
  637. })
  638. var response *dto.ChatCompletionsStreamResponse
  639. if imageCount != 0 {
  640. if usage.CompletionTokens == 0 {
  641. usage.CompletionTokens = imageCount * 258
  642. }
  643. }
  644. usage.PromptTokensDetails.TextTokens = usage.PromptTokens
  645. usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
  646. if info.ShouldIncludeUsage {
  647. response = helper.GenerateFinalUsageResponse(id, createAt, info.UpstreamModelName, *usage)
  648. err := helper.ObjectData(c, response)
  649. if err != nil {
  650. common.SysError("send final response failed: " + err.Error())
  651. }
  652. }
  653. helper.Done(c)
  654. //resp.Body.Close()
  655. return nil, usage
  656. }
  657. func GeminiChatHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
  658. responseBody, err := io.ReadAll(resp.Body)
  659. if err != nil {
  660. return service.OpenAIErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
  661. }
  662. err = resp.Body.Close()
  663. if err != nil {
  664. return service.OpenAIErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
  665. }
  666. var geminiResponse GeminiChatResponse
  667. err = json.Unmarshal(responseBody, &geminiResponse)
  668. if err != nil {
  669. return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
  670. }
  671. if len(geminiResponse.Candidates) == 0 {
  672. return &dto.OpenAIErrorWithStatusCode{
  673. Error: dto.OpenAIError{
  674. Message: "No candidates returned",
  675. Type: "server_error",
  676. Param: "",
  677. Code: 500,
  678. },
  679. StatusCode: resp.StatusCode,
  680. }, nil
  681. }
  682. fullTextResponse := responseGeminiChat2OpenAI(&geminiResponse)
  683. fullTextResponse.Model = info.UpstreamModelName
  684. usage := dto.Usage{
  685. PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
  686. CompletionTokens: geminiResponse.UsageMetadata.CandidatesTokenCount,
  687. TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount,
  688. }
  689. usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
  690. usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
  691. fullTextResponse.Usage = usage
  692. jsonResponse, err := json.Marshal(fullTextResponse)
  693. if err != nil {
  694. return service.OpenAIErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
  695. }
  696. c.Writer.Header().Set("Content-Type", "application/json")
  697. c.Writer.WriteHeader(resp.StatusCode)
  698. _, err = c.Writer.Write(jsonResponse)
  699. return nil, &usage
  700. }
  701. func GeminiEmbeddingHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *dto.OpenAIErrorWithStatusCode) {
  702. responseBody, readErr := io.ReadAll(resp.Body)
  703. if readErr != nil {
  704. return nil, service.OpenAIErrorWrapper(readErr, "read_response_body_failed", http.StatusInternalServerError)
  705. }
  706. _ = resp.Body.Close()
  707. var geminiResponse GeminiEmbeddingResponse
  708. if jsonErr := json.Unmarshal(responseBody, &geminiResponse); jsonErr != nil {
  709. return nil, service.OpenAIErrorWrapper(jsonErr, "unmarshal_response_body_failed", http.StatusInternalServerError)
  710. }
  711. // convert to openai format response
  712. openAIResponse := dto.OpenAIEmbeddingResponse{
  713. Object: "list",
  714. Data: []dto.OpenAIEmbeddingResponseItem{
  715. {
  716. Object: "embedding",
  717. Embedding: geminiResponse.Embedding.Values,
  718. Index: 0,
  719. },
  720. },
  721. Model: info.UpstreamModelName,
  722. }
  723. // calculate usage
  724. // https://ai.google.dev/gemini-api/docs/pricing?hl=zh-cn#text-embedding-004
  725. // Google has not yet clarified how embedding models will be billed
  726. // refer to openai billing method to use input tokens billing
  727. // https://platform.openai.com/docs/guides/embeddings#what-are-embeddings
  728. usage = &dto.Usage{
  729. PromptTokens: info.PromptTokens,
  730. CompletionTokens: 0,
  731. TotalTokens: info.PromptTokens,
  732. }
  733. openAIResponse.Usage = *usage.(*dto.Usage)
  734. jsonResponse, jsonErr := json.Marshal(openAIResponse)
  735. if jsonErr != nil {
  736. return nil, service.OpenAIErrorWrapper(jsonErr, "marshal_response_failed", http.StatusInternalServerError)
  737. }
  738. c.Writer.Header().Set("Content-Type", "application/json")
  739. c.Writer.WriteHeader(resp.StatusCode)
  740. _, _ = c.Writer.Write(jsonResponse)
  741. return usage, nil
  742. }