gemini.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. package dto
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/logger"
  7. "github.com/QuantumNous/new-api/types"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type GeminiChatRequest struct {
  11. Requests []GeminiChatRequest `json:"requests,omitempty"` // For batch requests
  12. Contents []GeminiChatContent `json:"contents"`
  13. SafetySettings []GeminiChatSafetySettings `json:"safetySettings,omitempty"`
  14. GenerationConfig GeminiChatGenerationConfig `json:"generationConfig,omitempty"`
  15. Tools json.RawMessage `json:"tools,omitempty"`
  16. ToolConfig *ToolConfig `json:"toolConfig,omitempty"`
  17. SystemInstructions *GeminiChatContent `json:"systemInstruction,omitempty"`
  18. CachedContent string `json:"cachedContent,omitempty"`
  19. }
  20. // UnmarshalJSON allows GeminiChatRequest to accept both snake_case and camelCase fields.
  21. func (r *GeminiChatRequest) UnmarshalJSON(data []byte) error {
  22. type Alias GeminiChatRequest
  23. var aux struct {
  24. Alias
  25. SystemInstructionSnake *GeminiChatContent `json:"system_instruction,omitempty"`
  26. }
  27. if err := common.Unmarshal(data, &aux); err != nil {
  28. return err
  29. }
  30. *r = GeminiChatRequest(aux.Alias)
  31. if aux.SystemInstructionSnake != nil {
  32. r.SystemInstructions = aux.SystemInstructionSnake
  33. }
  34. return nil
  35. }
  36. type ToolConfig struct {
  37. FunctionCallingConfig *FunctionCallingConfig `json:"functionCallingConfig,omitempty"`
  38. RetrievalConfig *RetrievalConfig `json:"retrievalConfig,omitempty"`
  39. IncludeServerSideToolInvocations *bool `json:"includeServerSideToolInvocations,omitempty"`
  40. }
  41. type FunctionCallingConfig struct {
  42. Mode FunctionCallingConfigMode `json:"mode,omitempty"`
  43. AllowedFunctionNames []string `json:"allowedFunctionNames,omitempty"`
  44. }
  45. type FunctionCallingConfigMode string
  46. type RetrievalConfig struct {
  47. LatLng *LatLng `json:"latLng,omitempty"`
  48. LanguageCode string `json:"languageCode,omitempty"`
  49. }
  50. type LatLng struct {
  51. Latitude *float64 `json:"latitude,omitempty"`
  52. Longitude *float64 `json:"longitude,omitempty"`
  53. }
  54. func (r *GeminiChatRequest) GetTokenCountMeta() *types.TokenCountMeta {
  55. var files []*types.FileMeta = make([]*types.FileMeta, 0)
  56. var maxTokens int
  57. if r.GenerationConfig.MaxOutputTokens != nil && *r.GenerationConfig.MaxOutputTokens > 0 {
  58. maxTokens = int(*r.GenerationConfig.MaxOutputTokens)
  59. }
  60. var inputTexts []string
  61. for _, content := range r.Contents {
  62. for _, part := range content.Parts {
  63. if part.Text != "" {
  64. inputTexts = append(inputTexts, part.Text)
  65. }
  66. if source := part.InlineData.ToFileSource(); source != nil {
  67. mimeType := part.InlineData.MimeType
  68. var fileType types.FileType
  69. if strings.HasPrefix(mimeType, "image/") {
  70. fileType = types.FileTypeImage
  71. } else if strings.HasPrefix(mimeType, "audio/") {
  72. fileType = types.FileTypeAudio
  73. } else if strings.HasPrefix(mimeType, "video/") {
  74. fileType = types.FileTypeVideo
  75. } else {
  76. fileType = types.FileTypeFile
  77. }
  78. files = append(files, &types.FileMeta{
  79. FileType: fileType,
  80. Source: source,
  81. })
  82. }
  83. }
  84. }
  85. inputText := strings.Join(inputTexts, "\n")
  86. return &types.TokenCountMeta{
  87. CombineText: inputText,
  88. Files: files,
  89. MaxTokens: maxTokens,
  90. }
  91. }
  92. func (r *GeminiChatRequest) IsStream(c *gin.Context) bool {
  93. if c.Query("alt") == "sse" {
  94. return true
  95. }
  96. // Native Gemini API uses URL action to indicate streaming:
  97. // /v1beta/models/{model}:streamGenerateContent
  98. if strings.Contains(c.Request.URL.Path, "streamGenerateContent") {
  99. return true
  100. }
  101. return false
  102. }
  103. func (r *GeminiChatRequest) SetModelName(modelName string) {
  104. // GeminiChatRequest does not have a model field, so this method does nothing.
  105. }
  106. func (r *GeminiChatRequest) GetTools() []GeminiChatTool {
  107. var tools []GeminiChatTool
  108. if strings.HasPrefix(string(r.Tools), "[") {
  109. // is array
  110. if err := common.Unmarshal(r.Tools, &tools); err != nil {
  111. logger.LogError(nil, "error_unmarshalling_tools: "+err.Error())
  112. return nil
  113. }
  114. } else if strings.HasPrefix(string(r.Tools), "{") {
  115. // is object
  116. singleTool := GeminiChatTool{}
  117. if err := common.Unmarshal(r.Tools, &singleTool); err != nil {
  118. logger.LogError(nil, "error_unmarshalling_single_tool: "+err.Error())
  119. return nil
  120. }
  121. tools = []GeminiChatTool{singleTool}
  122. }
  123. return tools
  124. }
  125. func (r *GeminiChatRequest) SetTools(tools []GeminiChatTool) {
  126. if len(tools) == 0 {
  127. r.Tools = json.RawMessage("[]")
  128. return
  129. }
  130. // Marshal the tools to JSON
  131. data, err := common.Marshal(tools)
  132. if err != nil {
  133. logger.LogError(nil, "error_marshalling_tools: "+err.Error())
  134. return
  135. }
  136. r.Tools = data
  137. }
  138. type GeminiThinkingConfig struct {
  139. IncludeThoughts bool `json:"includeThoughts,omitempty"`
  140. ThinkingBudget *int `json:"thinkingBudget,omitempty"`
  141. // TODO Conflict with thinkingbudget.
  142. ThinkingLevel string `json:"thinkingLevel,omitempty"`
  143. }
  144. // UnmarshalJSON allows GeminiThinkingConfig to accept both snake_case and camelCase fields.
  145. func (c *GeminiThinkingConfig) UnmarshalJSON(data []byte) error {
  146. type Alias GeminiThinkingConfig
  147. var aux struct {
  148. Alias
  149. IncludeThoughtsSnake *bool `json:"include_thoughts,omitempty"`
  150. ThinkingBudgetSnake *int `json:"thinking_budget,omitempty"`
  151. ThinkingLevelSnake string `json:"thinking_level,omitempty"`
  152. }
  153. if err := common.Unmarshal(data, &aux); err != nil {
  154. return err
  155. }
  156. *c = GeminiThinkingConfig(aux.Alias)
  157. if aux.IncludeThoughtsSnake != nil {
  158. c.IncludeThoughts = *aux.IncludeThoughtsSnake
  159. }
  160. if aux.ThinkingBudgetSnake != nil {
  161. c.ThinkingBudget = aux.ThinkingBudgetSnake
  162. }
  163. if aux.ThinkingLevelSnake != "" {
  164. c.ThinkingLevel = aux.ThinkingLevelSnake
  165. }
  166. return nil
  167. }
  168. func (c *GeminiThinkingConfig) SetThinkingBudget(budget int) {
  169. c.ThinkingBudget = &budget
  170. }
  171. type GeminiInlineData struct {
  172. MimeType string `json:"mimeType"`
  173. Data string `json:"data"`
  174. }
  175. func (d *GeminiInlineData) ToFileSource() types.FileSource {
  176. if d == nil || d.Data == "" {
  177. return nil
  178. }
  179. return types.NewFileSourceFromData(d.Data, d.MimeType)
  180. }
  181. // UnmarshalJSON custom unmarshaler for GeminiInlineData to support snake_case and camelCase for MimeType
  182. func (g *GeminiInlineData) UnmarshalJSON(data []byte) error {
  183. type Alias GeminiInlineData // Use type alias to avoid recursion
  184. var aux struct {
  185. Alias
  186. MimeTypeSnake string `json:"mime_type"`
  187. }
  188. if err := common.Unmarshal(data, &aux); err != nil {
  189. return err
  190. }
  191. *g = GeminiInlineData(aux.Alias) // Copy other fields if any in future
  192. // Prioritize snake_case if present
  193. if aux.MimeTypeSnake != "" {
  194. g.MimeType = aux.MimeTypeSnake
  195. } else if aux.MimeType != "" { // Fallback to camelCase from Alias
  196. g.MimeType = aux.MimeType
  197. }
  198. // g.Data would be populated by aux.Alias.Data
  199. return nil
  200. }
  201. type FunctionCall struct {
  202. FunctionName string `json:"name"`
  203. Arguments any `json:"args"`
  204. }
  205. type GeminiFunctionResponse struct {
  206. Name string `json:"name"`
  207. Response map[string]interface{} `json:"response"`
  208. WillContinue json.RawMessage `json:"willContinue,omitempty"`
  209. Scheduling json.RawMessage `json:"scheduling,omitempty"`
  210. Parts json.RawMessage `json:"parts,omitempty"`
  211. ID json.RawMessage `json:"id,omitempty"`
  212. }
  213. type GeminiPartExecutableCode struct {
  214. Language string `json:"language,omitempty"`
  215. Code string `json:"code,omitempty"`
  216. }
  217. type GeminiPartCodeExecutionResult struct {
  218. Outcome string `json:"outcome,omitempty"`
  219. Output string `json:"output,omitempty"`
  220. }
  221. type GeminiFileData struct {
  222. MimeType string `json:"mimeType,omitempty"`
  223. FileUri string `json:"fileUri,omitempty"`
  224. }
  225. type GeminiPart struct {
  226. Text string `json:"text,omitempty"`
  227. Thought bool `json:"thought,omitempty"`
  228. InlineData *GeminiInlineData `json:"inlineData,omitempty"`
  229. FunctionCall *FunctionCall `json:"functionCall,omitempty"`
  230. ThoughtSignature json.RawMessage `json:"thoughtSignature,omitempty"`
  231. FunctionResponse *GeminiFunctionResponse `json:"functionResponse,omitempty"`
  232. // Optional. Media resolution for the input media.
  233. MediaResolution json.RawMessage `json:"mediaResolution,omitempty"`
  234. VideoMetadata json.RawMessage `json:"videoMetadata,omitempty"`
  235. FileData *GeminiFileData `json:"fileData,omitempty"`
  236. ExecutableCode *GeminiPartExecutableCode `json:"executableCode,omitempty"`
  237. CodeExecutionResult *GeminiPartCodeExecutionResult `json:"codeExecutionResult,omitempty"`
  238. }
  239. // UnmarshalJSON custom unmarshaler for GeminiPart to support snake_case and camelCase for InlineData
  240. func (p *GeminiPart) UnmarshalJSON(data []byte) error {
  241. // Alias to avoid recursion during unmarshalling
  242. type Alias GeminiPart
  243. var aux struct {
  244. Alias
  245. InlineDataSnake *GeminiInlineData `json:"inline_data,omitempty"` // snake_case variant
  246. }
  247. if err := common.Unmarshal(data, &aux); err != nil {
  248. return err
  249. }
  250. // Assign fields from alias
  251. *p = GeminiPart(aux.Alias)
  252. // Prioritize snake_case for InlineData if present
  253. if aux.InlineDataSnake != nil {
  254. p.InlineData = aux.InlineDataSnake
  255. } else if aux.InlineData != nil { // Fallback to camelCase from Alias
  256. p.InlineData = aux.InlineData
  257. }
  258. // Other fields like Text, FunctionCall etc. are already populated via aux.Alias
  259. return nil
  260. }
  261. type GeminiChatContent struct {
  262. Role string `json:"role,omitempty"`
  263. Parts []GeminiPart `json:"parts"`
  264. }
  265. type GeminiChatSafetySettings struct {
  266. Category string `json:"category"`
  267. Threshold string `json:"threshold"`
  268. }
  269. type GeminiChatTool struct {
  270. GoogleSearch any `json:"googleSearch,omitempty"`
  271. GoogleSearchRetrieval any `json:"googleSearchRetrieval,omitempty"`
  272. CodeExecution any `json:"codeExecution,omitempty"`
  273. FunctionDeclarations any `json:"functionDeclarations,omitempty"`
  274. URLContext any `json:"urlContext,omitempty"`
  275. }
  276. type GeminiChatGenerationConfig struct {
  277. Temperature *float64 `json:"temperature,omitempty"`
  278. TopP *float64 `json:"topP,omitempty"`
  279. TopK *float64 `json:"topK,omitempty"`
  280. MaxOutputTokens *uint `json:"maxOutputTokens,omitempty"`
  281. CandidateCount *int `json:"candidateCount,omitempty"`
  282. StopSequences []string `json:"stopSequences,omitempty"`
  283. ResponseMimeType string `json:"responseMimeType,omitempty"`
  284. ResponseSchema any `json:"responseSchema,omitempty"`
  285. ResponseJsonSchema json.RawMessage `json:"responseJsonSchema,omitempty"`
  286. PresencePenalty *float32 `json:"presencePenalty,omitempty"`
  287. FrequencyPenalty *float32 `json:"frequencyPenalty,omitempty"`
  288. ResponseLogprobs *bool `json:"responseLogprobs,omitempty"`
  289. Logprobs *int32 `json:"logprobs,omitempty"`
  290. EnableEnhancedCivicAnswers *bool `json:"enableEnhancedCivicAnswers,omitempty"`
  291. MediaResolution MediaResolution `json:"mediaResolution,omitempty"`
  292. Seed *int64 `json:"seed,omitempty"`
  293. ResponseModalities []string `json:"responseModalities,omitempty"`
  294. ThinkingConfig *GeminiThinkingConfig `json:"thinkingConfig,omitempty"`
  295. SpeechConfig json.RawMessage `json:"speechConfig,omitempty"` // RawMessage to allow flexible speech config
  296. ImageConfig json.RawMessage `json:"imageConfig,omitempty"` // RawMessage to allow flexible image config
  297. }
  298. // UnmarshalJSON allows GeminiChatGenerationConfig to accept both snake_case and camelCase fields.
  299. func (c *GeminiChatGenerationConfig) UnmarshalJSON(data []byte) error {
  300. type Alias GeminiChatGenerationConfig
  301. var aux struct {
  302. Alias
  303. TopPSnake *float64 `json:"top_p,omitempty"`
  304. TopKSnake *float64 `json:"top_k,omitempty"`
  305. MaxOutputTokensSnake *uint `json:"max_output_tokens,omitempty"`
  306. CandidateCountSnake *int `json:"candidate_count,omitempty"`
  307. StopSequencesSnake []string `json:"stop_sequences,omitempty"`
  308. ResponseMimeTypeSnake string `json:"response_mime_type,omitempty"`
  309. ResponseSchemaSnake any `json:"response_schema,omitempty"`
  310. ResponseJsonSchemaSnake json.RawMessage `json:"response_json_schema,omitempty"`
  311. PresencePenaltySnake *float32 `json:"presence_penalty,omitempty"`
  312. FrequencyPenaltySnake *float32 `json:"frequency_penalty,omitempty"`
  313. ResponseLogprobsSnake *bool `json:"response_logprobs,omitempty"`
  314. EnableEnhancedCivicAnswersSnake *bool `json:"enable_enhanced_civic_answers,omitempty"`
  315. MediaResolutionSnake MediaResolution `json:"media_resolution,omitempty"`
  316. ResponseModalitiesSnake []string `json:"response_modalities,omitempty"`
  317. ThinkingConfigSnake *GeminiThinkingConfig `json:"thinking_config,omitempty"`
  318. SpeechConfigSnake json.RawMessage `json:"speech_config,omitempty"`
  319. ImageConfigSnake json.RawMessage `json:"image_config,omitempty"`
  320. }
  321. if err := common.Unmarshal(data, &aux); err != nil {
  322. return err
  323. }
  324. *c = GeminiChatGenerationConfig(aux.Alias)
  325. // Prioritize snake_case if present
  326. if aux.TopPSnake != nil {
  327. c.TopP = aux.TopPSnake
  328. }
  329. if aux.TopKSnake != nil {
  330. c.TopK = aux.TopKSnake
  331. }
  332. if aux.MaxOutputTokensSnake != nil {
  333. c.MaxOutputTokens = aux.MaxOutputTokensSnake
  334. }
  335. if aux.CandidateCountSnake != nil {
  336. c.CandidateCount = aux.CandidateCountSnake
  337. }
  338. if len(aux.StopSequencesSnake) > 0 {
  339. c.StopSequences = aux.StopSequencesSnake
  340. }
  341. if aux.ResponseMimeTypeSnake != "" {
  342. c.ResponseMimeType = aux.ResponseMimeTypeSnake
  343. }
  344. if aux.ResponseSchemaSnake != nil {
  345. c.ResponseSchema = aux.ResponseSchemaSnake
  346. }
  347. if len(aux.ResponseJsonSchemaSnake) > 0 {
  348. c.ResponseJsonSchema = aux.ResponseJsonSchemaSnake
  349. }
  350. if aux.PresencePenaltySnake != nil {
  351. c.PresencePenalty = aux.PresencePenaltySnake
  352. }
  353. if aux.FrequencyPenaltySnake != nil {
  354. c.FrequencyPenalty = aux.FrequencyPenaltySnake
  355. }
  356. if aux.ResponseLogprobsSnake != nil {
  357. c.ResponseLogprobs = aux.ResponseLogprobsSnake
  358. }
  359. if aux.EnableEnhancedCivicAnswersSnake != nil {
  360. c.EnableEnhancedCivicAnswers = aux.EnableEnhancedCivicAnswersSnake
  361. }
  362. if aux.MediaResolutionSnake != "" {
  363. c.MediaResolution = aux.MediaResolutionSnake
  364. }
  365. if len(aux.ResponseModalitiesSnake) > 0 {
  366. c.ResponseModalities = aux.ResponseModalitiesSnake
  367. }
  368. if aux.ThinkingConfigSnake != nil {
  369. c.ThinkingConfig = aux.ThinkingConfigSnake
  370. }
  371. if len(aux.SpeechConfigSnake) > 0 {
  372. c.SpeechConfig = aux.SpeechConfigSnake
  373. }
  374. if len(aux.ImageConfigSnake) > 0 {
  375. c.ImageConfig = aux.ImageConfigSnake
  376. }
  377. return nil
  378. }
  379. type MediaResolution string
  380. type GeminiChatCandidate struct {
  381. Content GeminiChatContent `json:"content"`
  382. FinishReason *string `json:"finishReason"`
  383. Index int64 `json:"index"`
  384. SafetyRatings []GeminiChatSafetyRating `json:"safetyRatings"`
  385. }
  386. type GeminiChatSafetyRating struct {
  387. Category string `json:"category"`
  388. Probability string `json:"probability"`
  389. }
  390. type GeminiChatPromptFeedback struct {
  391. SafetyRatings []GeminiChatSafetyRating `json:"safetyRatings"`
  392. BlockReason *string `json:"blockReason,omitempty"`
  393. }
  394. type GeminiChatResponse struct {
  395. Candidates []GeminiChatCandidate `json:"candidates"`
  396. PromptFeedback *GeminiChatPromptFeedback `json:"promptFeedback,omitempty"`
  397. UsageMetadata GeminiUsageMetadata `json:"usageMetadata"`
  398. }
  399. type GeminiUsageMetadata struct {
  400. PromptTokenCount int `json:"promptTokenCount"`
  401. ToolUsePromptTokenCount int `json:"toolUsePromptTokenCount"`
  402. CandidatesTokenCount int `json:"candidatesTokenCount"`
  403. TotalTokenCount int `json:"totalTokenCount"`
  404. ThoughtsTokenCount int `json:"thoughtsTokenCount"`
  405. CachedContentTokenCount int `json:"cachedContentTokenCount"`
  406. PromptTokensDetails []GeminiPromptTokensDetails `json:"promptTokensDetails"`
  407. ToolUsePromptTokensDetails []GeminiPromptTokensDetails `json:"toolUsePromptTokensDetails"`
  408. }
  409. type GeminiPromptTokensDetails struct {
  410. Modality string `json:"modality"`
  411. TokenCount int `json:"tokenCount"`
  412. }
  413. // Imagen related structs
  414. type GeminiImageRequest struct {
  415. Instances []GeminiImageInstance `json:"instances"`
  416. Parameters GeminiImageParameters `json:"parameters"`
  417. }
  418. type GeminiImageInstance struct {
  419. Prompt string `json:"prompt"`
  420. }
  421. type GeminiImageParameters struct {
  422. SampleCount int `json:"sampleCount,omitempty"`
  423. AspectRatio string `json:"aspectRatio,omitempty"`
  424. PersonGeneration string `json:"personGeneration,omitempty"`
  425. ImageSize string `json:"imageSize,omitempty"`
  426. }
  427. type GeminiImageResponse struct {
  428. Predictions []GeminiImagePrediction `json:"predictions"`
  429. }
  430. type GeminiImagePrediction struct {
  431. MimeType string `json:"mimeType"`
  432. BytesBase64Encoded string `json:"bytesBase64Encoded"`
  433. RaiFilteredReason string `json:"raiFilteredReason,omitempty"`
  434. SafetyAttributes any `json:"safetyAttributes,omitempty"`
  435. }
  436. // Embedding related structs
  437. type GeminiEmbeddingRequest struct {
  438. Model string `json:"model,omitempty"`
  439. Content GeminiChatContent `json:"content"`
  440. TaskType string `json:"taskType,omitempty"`
  441. Title string `json:"title,omitempty"`
  442. OutputDimensionality int `json:"outputDimensionality,omitempty"`
  443. }
  444. func (r *GeminiEmbeddingRequest) IsStream(c *gin.Context) bool {
  445. // Gemini embedding requests are not streamed
  446. return false
  447. }
  448. func (r *GeminiEmbeddingRequest) GetTokenCountMeta() *types.TokenCountMeta {
  449. var inputTexts []string
  450. for _, part := range r.Content.Parts {
  451. if part.Text != "" {
  452. inputTexts = append(inputTexts, part.Text)
  453. }
  454. }
  455. inputText := strings.Join(inputTexts, "\n")
  456. return &types.TokenCountMeta{
  457. CombineText: inputText,
  458. }
  459. }
  460. func (r *GeminiEmbeddingRequest) SetModelName(modelName string) {
  461. if modelName != "" {
  462. r.Model = modelName
  463. }
  464. }
  465. type GeminiBatchEmbeddingRequest struct {
  466. Requests []*GeminiEmbeddingRequest `json:"requests"`
  467. }
  468. func (r *GeminiBatchEmbeddingRequest) IsStream(c *gin.Context) bool {
  469. // Gemini batch embedding requests are not streamed
  470. return false
  471. }
  472. func (r *GeminiBatchEmbeddingRequest) GetTokenCountMeta() *types.TokenCountMeta {
  473. var inputTexts []string
  474. for _, request := range r.Requests {
  475. meta := request.GetTokenCountMeta()
  476. if meta != nil && meta.CombineText != "" {
  477. inputTexts = append(inputTexts, meta.CombineText)
  478. }
  479. }
  480. inputText := strings.Join(inputTexts, "\n")
  481. return &types.TokenCountMeta{
  482. CombineText: inputText,
  483. }
  484. }
  485. func (r *GeminiBatchEmbeddingRequest) SetModelName(modelName string) {
  486. if modelName != "" {
  487. for _, req := range r.Requests {
  488. req.SetModelName(modelName)
  489. }
  490. }
  491. }
  492. type GeminiEmbeddingResponse struct {
  493. Embedding ContentEmbedding `json:"embedding"`
  494. }
  495. type GeminiBatchEmbeddingResponse struct {
  496. Embeddings []*ContentEmbedding `json:"embeddings"`
  497. }
  498. type ContentEmbedding struct {
  499. Values []float64 `json:"values"`
  500. }