Skip to content

Go: implement Encode (embeddings) in Google Gemini driver - #14682

Merged
JinHai-CN merged 1 commit into
infiniflow:mainfrom
Joseff531:go-deepseek-encode
May 11, 2026
Merged

JinHai-CN merged 1 commit into
infiniflow:mainfrom
Joseff531:go-deepseek-encode

Conversation

@Joseff531

@Joseff531 Joseff531 commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

  • Implements the Encode method in the Google Gemini driver, which was previously a stub returning not implemented
  • Uses the google.golang.org/genai SDK's EmbedContent API, which routes to the batchEmbedContents endpoint internally — all texts are sent in a single request
  • Adds text-embedding-004 (max 2048 tokens) to conf/models/google.json
  • Response values are []float32 from the SDK and are cast to []float64 to satisfy the ModelDriver interface

Files changed

  • internal/entity/models/google.go — full Encode implementation
  • conf/models/google.json — adds text-embedding-004 embedding model

Type of change

  • New Feature (non-breaking change which adds functionality)

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label May 8, 2026
@coderabbitai

coderabbitai Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This PR registers text-embedding-004 in Google model config and implements GoogleModel.Encode to call Gemini's EmbedContent API with validation, timeout handling, and response parsing; it also updates CheckConnection to call ListModels.

Changes

Google Gemini Embedding Feature

Layer / File(s) Summary
Model Configuration
conf/models/google.json
Registers text-embedding-004 with max_tokens: 2048 and model_types: ["embedding"].
Encode Implementation
internal/entity/models/google.go
Implements GoogleModel.Encode to validate inputs, create a timeout-bound context, batch texts into genai.Content, call EmbedContent, verify response count, and return embeddings as [][]float64 with wrapped errors.
Connection Check Update
internal/entity/models/google.go
Changes GoogleModel.CheckConnection to delegate to ListModels(apiConfig) and return its error.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Encode
  participant GeminiAPI
  Caller->>Encode: Encode(modelName, texts, apiConfig, embeddingConfig)
  Encode->>Encode: validate apiKey, modelName, texts
  Encode->>Encode: create timeout-bound context
  Encode->>Encode: build genai.Content per text
  Encode->>GeminiAPI: EmbedContent(ctx, request)
  GeminiAPI-->>Encode: response with embeddings array
  Encode->>Encode: verify counts and convert Values -> [][]float64
  Encode-->>Caller: [][]float64 or error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • infiniflow/ragflow#14395: Introduced the Google provider foundation and left the embedding method unimplemented; this PR completes that implementation.

Poem

🐰 I hopped through configs, tiny and neat,
Added embeddings so vectors can meet.
From stub to embed, with context and care,
Gemini sings vectors into the air.
Hop—now the model returns numbers to share.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: implementing the Encode method (embeddings) in the Google Gemini driver.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description covers the problem solved, files changed, and type of change, but omits the detailed context about earlier mistakes and scope repurposing mentioned in PR comments.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/entity/models/deepseek.go`:
- Around line 462-463: The context timeout in the Deepseek driver uses a
hardcoded 30*time.Second (context.WithTimeout(context.Background(),
30*time.Second)); replace that literal with the shared nonStreamCallTimeout
constant so non-stream calls use the centralized timeout; update the
context.WithTimeout call in deepseek.go (the code that creates ctx, cancel) to
use nonStreamCallTimeout and ensure the symbol is referenced/imported from the
package where it’s defined.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 874f773b-d353-410f-bd69-0131f43508d8

📥 Commits

Reviewing files that changed from the base of the PR and between ce2ec86 and 2650eb5.

📒 Files selected for processing (2)
  • conf/models/deepseek.json
  • internal/entity/models/deepseek.go

Comment thread internal/entity/models/deepseek.go Resolved

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Joseff531 Where can I find the DeepSeek embedding documentation?
DeepSeek Api Docs

@Joseff531 Joseff531 closed this May 8, 2026
@Joseff531 Joseff531 reopened this May 8, 2026
@Joseff531 Joseff531 changed the title Go: implement Encode (embeddings) in DeepSeek driver Go: implement Encode (embeddings) in OpenAI driver May 8, 2026
@Joseff531

Joseff531 commented May 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @Joseff531 Where can I find the DeepSeek embedding documentation? DeepSeek Api Docs

Hi @Haruko386, you're right to question the original scope. After checking the official DeepSeek API docs (api-docs.deepseek.com), DeepSeek does not expose an /embeddings endpoint. The text-embedding-v3 model referenced in the original issue is an Aliyun/DashScope model, not a DeepSeek one.

I repurposed this PR once more - implementing OpenAI Encode turned out to duplicate #14630 (same files, same goal). So this PR now targets the Google Gemini driver instead, which had two real gaps:

  1. Encode was a stub returning "not implemented" - implemented using the genai SDK's EmbedContent API (routes to batchEmbedContents internally). Added text-embedding-004 to conf/models/google.json.
  2. CheckConnection always failed with "no such method"- fixed to delegate to ListModels, consistent with how OpenAI and xAI drivers handle it.

Sorry for the churn on the scope, the implementation is solid now.

@Joseff531

Copy link
Copy Markdown
Contributor Author

Hope your review again, Thanks.

@Joseff531
Joseff531 requested a review from Haruko386 May 8, 2026 10:40

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Joseff531, LGTM

@Haruko386 Haruko386 added the ci Continue Integration label May 8, 2026
@Joseff531
Joseff531 requested a review from Haruko386 May 8, 2026 18:30
@Joseff531
Joseff531 force-pushed the go-deepseek-encode branch from 2189f6e to 7b886c8 Compare May 8, 2026 19:42
@codecov

codecov Bot commented May 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.16%. Comparing base (048ec2f) to head (dbbde3e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14682      +/-   ##
==========================================
- Coverage   95.59%   94.16%   -1.43%     
==========================================
  Files          10       10              
  Lines         703      703              
  Branches      112      112              
==========================================
- Hits          672      662      -10     
- Misses         21       25       +4     
- Partials       10       16       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@conf/models/google.json`:
- Around line 22-28: Replace the deprecated "text-embedding-004" entry in
conf/models/google.json with the new model name "gemini-embedding-001" and
update its token limit to 8192; specifically update the object where "name":
"text-embedding-004" (and "max_tokens": 2048) to use "name":
"gemini-embedding-001" and "max_tokens": 8192 while keeping "model_types":
["embedding"] intact.

In `@internal/entity/models/google.go`:
- Around line 215-263: The Encode method uses an undefined nonStreamCallTimeout
and lacks the time import; define the constant nonStreamCallTimeout (e.g., const
nonStreamCallTimeout = 120 * time.Second) in this file and add "time" to the
import block so Encode (and its context.WithTimeout call) compiles; update
google.go near the top with the import and the constant definition, leaving the
rest of the Encode function unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: abb6d7d8-ad9a-49cf-8f9b-e140b1d8b3f8

📥 Commits

Reviewing files that changed from the base of the PR and between 2189f6e and 7b886c8.

📒 Files selected for processing (2)
  • conf/models/google.json
  • internal/entity/models/google.go

Comment thread conf/models/google.json Resolved
Comment thread internal/entity/models/google.go Resolved
@Joseff531 Joseff531 changed the title Go: implement Encode (embeddings) in OpenAI driver Go: implement Encode (embeddings) in Google Gemini driver May 8, 2026
ivannnnnik pushed a commit to ivannnnnik/ragflow that referenced this pull request May 8, 2026
Wires the existing stub to the DeepSeek OpenAI-compatible /embeddings
endpoint and adds the embedding URL suffix and text-embedding-v3 model
entry to conf/models/deepseek.json.

The implementation mirrors the recently merged Aliyun Encode and the
in-flight OpenAI (infiniflow#14682) and Ollama (infiniflow#14664) PRs:

- POST to ${BaseURL}/${URLSuffix.Embedding}
- Authorization: Bearer <api_key>
- OpenAI-compatible request body {"model", "input"}
- Per-call context.WithTimeout(nonStreamCallTimeout) so a slow upstream
  cannot block forever
- [][]float64 returned in the original input order, sorted by the
  response `index` field, with explicit checks for out-of-range,
  duplicate, and missing indices

Adds internal/entity/models/deepseek_test.go covering the happy path,
empty-input short-circuit, missing api key / model name, non-OK
upstream status, and out-of-range / missing-index responses.

Closes infiniflow#14680

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
ivannnnnik pushed a commit to ivannnnnik/ragflow that referenced this pull request May 8, 2026
Wires the existing stub to the DeepSeek OpenAI-compatible /embeddings
endpoint and adds the embedding URL suffix and text-embedding-v3 model
entry to conf/models/deepseek.json.

The implementation mirrors the recently merged Aliyun Encode and the
in-flight OpenAI (infiniflow#14682) and Ollama (infiniflow#14664) PRs:

- POST to ${BaseURL}/${URLSuffix.Embedding}
- Authorization: Bearer <api_key>
- OpenAI-compatible request body {"model", "input"}
- Per-call context.WithTimeout(nonStreamCallTimeout) so a slow upstream
  cannot block forever
- [][]float64 returned in the original input order, sorted by the
  response `index` field, with explicit checks for out-of-range,
  duplicate, and missing indices

Adds internal/entity/models/deepseek_test.go covering the happy path,
empty-input short-circuit, missing api key / model name, non-OK
upstream status, and out-of-range / missing-index responses.

Closes infiniflow#14680
ivannnnnik added a commit to ivannnnnik/ragflow that referenced this pull request May 8, 2026
Wires the existing stub to the DeepSeek OpenAI-compatible /embeddings
endpoint and adds the embedding URL suffix and text-embedding-v3 model
entry to conf/models/deepseek.json.

The implementation mirrors the recently merged Aliyun Encode and the
in-flight OpenAI (infiniflow#14682) and Ollama (infiniflow#14664) PRs:

- POST to ${BaseURL}/${URLSuffix.Embedding}
- Authorization: Bearer <api_key>
- OpenAI-compatible request body {"model", "input"}
- Per-call context.WithTimeout(nonStreamCallTimeout) so a slow upstream
  cannot block forever
- [][]float64 returned in the original input order, sorted by the
  response `index` field, with explicit checks for out-of-range,
  duplicate, and missing indices

Adds internal/entity/models/deepseek_test.go covering the happy path,
empty-input short-circuit, missing api key / model name, non-OK
upstream status, and out-of-range / missing-index responses.

Closes infiniflow#14680
@Joseff531

Copy link
Copy Markdown
Contributor Author

Sorry for the inconvenience, I'd be happy if you could review the PR again when you have a chance. ❤️

@JinHai-CN

Copy link
Copy Markdown
Member

@Joseff531 Encode interface is updated, would you please update your implementation accordingly? please refer to #14735

@Joseff531
Joseff531 force-pushed the go-deepseek-encode branch from f7b6938 to dbbde3e Compare May 9, 2026 18:13
@Joseff531

Copy link
Copy Markdown
Contributor Author

@Joseff531 Encode interface is updated, would you please update your implementation accordingly? please refer to #14735

Thanks for the heads-up. I've rebased onto current main (which now has the new EmbeddingConfig.Dimension field from #14735) and updated the Gemini driver to honor it — when set, it's passed as OutputDimensionality on mbedContentConfig
to the genai SDK.

@JinHai-CN
JinHai-CN merged commit f4f8bed into infiniflow:main May 11, 2026
1 check passed
@coderabbitai coderabbitai Bot mentioned this pull request May 11, 2026
1 task done
Ricardo-M-L pushed a commit to Ricardo-M-L/ragflow that referenced this pull request Aug 17, 2026
…#14682)

### What problem does this PR solve?

- Implements the `Encode` method in the Google Gemini driver, which was
previously a stub returning `not implemented`
- Uses the `google.golang.org/genai` SDK's `EmbedContent` API, which
routes to the `batchEmbedContents` endpoint internally — all texts are
sent in a single request
- Adds `text-embedding-004` (max 2048 tokens) to
`conf/models/google.json`
- Response values are `[]float32` from the SDK and are cast to
`[]float64` to satisfy the `ModelDriver` interface

## Files changed

- `internal/entity/models/google.go` — full `Encode` implementation
- `conf/models/google.json` — adds `text-embedding-004` embedding model

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants