relay-gemini.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 if part.Thought {
  503. choice.Message.ReasoningContent = part.Text
  504. } else {
  505. if part.ExecutableCode != nil {
  506. texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```")
  507. } else if part.CodeExecutionResult != nil {
  508. texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```")
  509. } else {
  510. // 过滤掉空行
  511. if part.Text != "\n" {
  512. texts = append(texts, part.Text)
  513. }
  514. }
  515. }
  516. }
  517. if len(toolCalls) > 0 {
  518. choice.Message.SetToolCalls(toolCalls)
  519. isToolCall = true
  520. }
  521. choice.Message.SetStringContent(strings.Join(texts, "\n"))
  522. }
  523. if candidate.FinishReason != nil {
  524. switch *candidate.FinishReason {
  525. case "STOP":
  526. choice.FinishReason = constant.FinishReasonStop
  527. case "MAX_TOKENS":
  528. choice.FinishReason = constant.FinishReasonLength
  529. default:
  530. choice.FinishReason = constant.FinishReasonContentFilter
  531. }
  532. }
  533. if isToolCall {
  534. choice.FinishReason = constant.FinishReasonToolCalls
  535. }
  536. fullTextResponse.Choices = append(fullTextResponse.Choices, choice)
  537. }
  538. return &fullTextResponse
  539. }
  540. func streamResponseGeminiChat2OpenAI(geminiResponse *GeminiChatResponse) (*dto.ChatCompletionsStreamResponse, bool, bool) {
  541. choices := make([]dto.ChatCompletionsStreamResponseChoice, 0, len(geminiResponse.Candidates))
  542. isStop := false
  543. hasImage := false
  544. for _, candidate := range geminiResponse.Candidates {
  545. if candidate.FinishReason != nil && *candidate.FinishReason == "STOP" {
  546. isStop = true
  547. candidate.FinishReason = nil
  548. }
  549. choice := dto.ChatCompletionsStreamResponseChoice{
  550. Index: int(candidate.Index),
  551. Delta: dto.ChatCompletionsStreamResponseChoiceDelta{
  552. Role: "assistant",
  553. },
  554. }
  555. var texts []string
  556. isTools := false
  557. isThought := false
  558. if candidate.FinishReason != nil {
  559. // p := GeminiConvertFinishReason(*candidate.FinishReason)
  560. switch *candidate.FinishReason {
  561. case "STOP":
  562. choice.FinishReason = &constant.FinishReasonStop
  563. case "MAX_TOKENS":
  564. choice.FinishReason = &constant.FinishReasonLength
  565. default:
  566. choice.FinishReason = &constant.FinishReasonContentFilter
  567. }
  568. }
  569. for _, part := range candidate.Content.Parts {
  570. if part.InlineData != nil {
  571. if strings.HasPrefix(part.InlineData.MimeType, "image") {
  572. imgText := "![image](data:" + part.InlineData.MimeType + ";base64," + part.InlineData.Data + ")"
  573. texts = append(texts, imgText)
  574. hasImage = true
  575. }
  576. } else if part.FunctionCall != nil {
  577. isTools = true
  578. if call := getResponseToolCall(&part); call != nil {
  579. call.SetIndex(len(choice.Delta.ToolCalls))
  580. choice.Delta.ToolCalls = append(choice.Delta.ToolCalls, *call)
  581. }
  582. } else if part.Thought {
  583. isThought = true
  584. texts = append(texts, part.Text)
  585. } else {
  586. if part.ExecutableCode != nil {
  587. texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```\n")
  588. } else if part.CodeExecutionResult != nil {
  589. texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```\n")
  590. } else {
  591. if part.Text != "\n" {
  592. texts = append(texts, part.Text)
  593. }
  594. }
  595. }
  596. }
  597. if isThought {
  598. choice.Delta.SetReasoningContent(strings.Join(texts, "\n"))
  599. } else {
  600. choice.Delta.SetContentString(strings.Join(texts, "\n"))
  601. }
  602. if isTools {
  603. choice.FinishReason = &constant.FinishReasonToolCalls
  604. }
  605. choices = append(choices, choice)
  606. }
  607. var response dto.ChatCompletionsStreamResponse
  608. response.Object = "chat.completion.chunk"
  609. response.Choices = choices
  610. return &response, isStop, hasImage
  611. }
  612. func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
  613. // responseText := ""
  614. id := fmt.Sprintf("chatcmpl-%s", common.GetUUID())
  615. createAt := common.GetTimestamp()
  616. var usage = &dto.Usage{}
  617. var imageCount int
  618. helper.StreamScannerHandler(c, resp, info, func(data string) bool {
  619. var geminiResponse GeminiChatResponse
  620. err := common.DecodeJsonStr(data, &geminiResponse)
  621. if err != nil {
  622. common.LogError(c, "error unmarshalling stream response: "+err.Error())
  623. return false
  624. }
  625. response, isStop, hasImage := streamResponseGeminiChat2OpenAI(&geminiResponse)
  626. if hasImage {
  627. imageCount++
  628. }
  629. response.Id = id
  630. response.Created = createAt
  631. response.Model = info.UpstreamModelName
  632. if geminiResponse.UsageMetadata.TotalTokenCount != 0 {
  633. usage.PromptTokens = geminiResponse.UsageMetadata.PromptTokenCount
  634. usage.CompletionTokens = geminiResponse.UsageMetadata.CandidatesTokenCount
  635. usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
  636. usage.TotalTokens = geminiResponse.UsageMetadata.TotalTokenCount
  637. }
  638. err = helper.ObjectData(c, response)
  639. if err != nil {
  640. common.LogError(c, err.Error())
  641. }
  642. if isStop {
  643. response := helper.GenerateStopResponse(id, createAt, info.UpstreamModelName, constant.FinishReasonStop)
  644. helper.ObjectData(c, response)
  645. }
  646. return true
  647. })
  648. var response *dto.ChatCompletionsStreamResponse
  649. if imageCount != 0 {
  650. if usage.CompletionTokens == 0 {
  651. usage.CompletionTokens = imageCount * 258
  652. }
  653. }
  654. usage.PromptTokensDetails.TextTokens = usage.PromptTokens
  655. usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
  656. if info.ShouldIncludeUsage {
  657. response = helper.GenerateFinalUsageResponse(id, createAt, info.UpstreamModelName, *usage)
  658. err := helper.ObjectData(c, response)
  659. if err != nil {
  660. common.SysError("send final response failed: " + err.Error())
  661. }
  662. }
  663. helper.Done(c)
  664. //resp.Body.Close()
  665. return nil, usage
  666. }
  667. func GeminiChatHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
  668. responseBody, err := io.ReadAll(resp.Body)
  669. if err != nil {
  670. return service.OpenAIErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
  671. }
  672. err = resp.Body.Close()
  673. if err != nil {
  674. return service.OpenAIErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
  675. }
  676. if common.DebugEnabled {
  677. println(string(responseBody))
  678. }
  679. var geminiResponse GeminiChatResponse
  680. err = common.DecodeJson(responseBody, &geminiResponse)
  681. if err != nil {
  682. return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
  683. }
  684. if len(geminiResponse.Candidates) == 0 {
  685. return &dto.OpenAIErrorWithStatusCode{
  686. Error: dto.OpenAIError{
  687. Message: "No candidates returned",
  688. Type: "server_error",
  689. Param: "",
  690. Code: 500,
  691. },
  692. StatusCode: resp.StatusCode,
  693. }, nil
  694. }
  695. fullTextResponse := responseGeminiChat2OpenAI(&geminiResponse)
  696. fullTextResponse.Model = info.UpstreamModelName
  697. usage := dto.Usage{
  698. PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
  699. CompletionTokens: geminiResponse.UsageMetadata.CandidatesTokenCount,
  700. TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount,
  701. }
  702. usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
  703. usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
  704. fullTextResponse.Usage = usage
  705. jsonResponse, err := json.Marshal(fullTextResponse)
  706. if err != nil {
  707. return service.OpenAIErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
  708. }
  709. c.Writer.Header().Set("Content-Type", "application/json")
  710. c.Writer.WriteHeader(resp.StatusCode)
  711. _, err = c.Writer.Write(jsonResponse)
  712. return nil, &usage
  713. }
  714. func GeminiEmbeddingHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *dto.OpenAIErrorWithStatusCode) {
  715. responseBody, readErr := io.ReadAll(resp.Body)
  716. if readErr != nil {
  717. return nil, service.OpenAIErrorWrapper(readErr, "read_response_body_failed", http.StatusInternalServerError)
  718. }
  719. _ = resp.Body.Close()
  720. var geminiResponse GeminiEmbeddingResponse
  721. if jsonErr := json.Unmarshal(responseBody, &geminiResponse); jsonErr != nil {
  722. return nil, service.OpenAIErrorWrapper(jsonErr, "unmarshal_response_body_failed", http.StatusInternalServerError)
  723. }
  724. // convert to openai format response
  725. openAIResponse := dto.OpenAIEmbeddingResponse{
  726. Object: "list",
  727. Data: []dto.OpenAIEmbeddingResponseItem{
  728. {
  729. Object: "embedding",
  730. Embedding: geminiResponse.Embedding.Values,
  731. Index: 0,
  732. },
  733. },
  734. Model: info.UpstreamModelName,
  735. }
  736. // calculate usage
  737. // https://ai.google.dev/gemini-api/docs/pricing?hl=zh-cn#text-embedding-004
  738. // Google has not yet clarified how embedding models will be billed
  739. // refer to openai billing method to use input tokens billing
  740. // https://platform.openai.com/docs/guides/embeddings#what-are-embeddings
  741. usage = &dto.Usage{
  742. PromptTokens: info.PromptTokens,
  743. CompletionTokens: 0,
  744. TotalTokens: info.PromptTokens,
  745. }
  746. openAIResponse.Usage = *usage.(*dto.Usage)
  747. jsonResponse, jsonErr := json.Marshal(openAIResponse)
  748. if jsonErr != nil {
  749. return nil, service.OpenAIErrorWrapper(jsonErr, "marshal_response_failed", http.StatusInternalServerError)
  750. }
  751. c.Writer.Header().Set("Content-Type", "application/json")
  752. c.Writer.WriteHeader(resp.StatusCode)
  753. _, _ = c.Writer.Write(jsonResponse)
  754. return usage, nil
  755. }