gemini.go 19 KB

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