Skip to content

Simplify Encode - #14437

Merged
JinHai-CN merged 1 commit into
infiniflow:mainfrom
qinling0210:simplify_encode
Apr 28, 2026
Merged

JinHai-CN merged 1 commit into
infiniflow:mainfrom
qinling0210:simplify_encode

Conversation

@qinling0210

@qinling0210 qinling0210 commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Simplify Encode

Type of change

  • Refactoring

@coderabbitai

coderabbitai Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The PR standardizes the embedding API surface across all model drivers by renaming EncodeToEmbedding to Encode with an EmbeddingConfig parameter, removing convenience method overloads (Encode without embeddingConfig and EncodeQuery), and updating interface definitions and callers accordingly.

Changes

Cohort / File(s) Summary
Model Driver Implementations
internal/entity/models/aliyun.go, internal/entity/models/deepseek.go, internal/entity/models/dummy.go, internal/entity/models/gitee.go, internal/entity/models/google.go, internal/entity/models/minimax.go, internal/entity/models/moonshot.go, internal/entity/models/siliconflow.go, internal/entity/models/volcengine.go, internal/entity/models/zhipu-ai.go
Renamed EncodeToEmbedding to Encode with EmbeddingConfig parameter; removed Encode(modelName, texts, apiConfig) and EncodeQuery convenience methods.
Interface Definitions
internal/entity/models/types.go, internal/entity/types.go
Updated ModelDriver and EmbeddingModel interfaces: removed EncodeToEmbedding and convenience methods; standardized Encode signature to require embeddingConfig parameter.
API Callers
internal/service/model_bundle.go, internal/service/nlp/retrieval.go
Updated embedding calls: model_bundle.go now passes embeddingConfig to Encode; retrieval.go replaces EncodeQuery call with Encode on single-element slice.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

size:XL


🐰 A hop, skip, and a jump through embedding's refactored lane,
One method to encode them all—no more convenience's reign!
From scattered signatures we spring to unified delight,
Configuration bundled in, the API shines so bright! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete; it only states the purpose as 'Simplify Encode' and marks the change as refactoring, but provides no explanation of what problem is solved or why the changes are beneficial. Expand the description to explain the specific API changes, why they improve the codebase (reduced method overloads, clearer interface), and any breaking changes or migration guidance for dependent code.
Title check ❓ Inconclusive The title 'Simplify Encode' is vague and generic, using a non-descriptive term that doesn't convey meaningful information about the specific changes. Consider a more specific title that describes the actual refactoring, such as 'Refactor embedding API: consolidate Encode methods and remove convenience wrappers' or 'Unify embedding model interface with single Encode signature'.
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ 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.

@qinling0210 qinling0210 self-assigned this Apr 28, 2026
@qinling0210 qinling0210 added the ci Continue Integration label Apr 28, 2026
@qinling0210
qinling0210 marked this pull request as ready for review April 28, 2026 09:37
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 28, 2026

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/service/nlp/retrieval.go (1)

599-616: ⚠️ Potential issue | 🟡 Minor

Add guard for empty embeddings slice to prevent panic.

While the Encode() call with nil for EmbeddingConfig is not an issue (the struct is currently empty with no fields, and EmbeddingModel lacks a field to carry it), embeddings[0] will panic if a driver returns an empty slice. Even though GetVector passes a single text, some drivers may return empty responses in error paths or edge cases.

Suggested fix
 embeddings, err := embModel.ModelDriver.Encode(embModel.ModelName, []string{txt}, embModel.APIConfig, nil)
 if err != nil {
     return nil, err
 }
+if len(embeddings) == 0 {
+    return nil, fmt.Errorf("no embedding returned")
+}
 
 vector := embeddings[0]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/service/nlp/retrieval.go` around lines 599 - 616, GetVector
currently assumes embeddings has at least one element and does embeddings[0],
which can panic if the driver returns an empty slice; add a guard after the
Encode call to check len(embeddings) > 0 and return a descriptive error (e.g.,
"empty embeddings returned from model driver") if it's zero so the function
exits safely instead of panicking; update handling around the variables
embeddings, vector, vectorSize, and vectorColumnName in the GetVector method to
only access embeddings[0] after the length check.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@internal/service/nlp/retrieval.go`:
- Around line 599-616: GetVector currently assumes embeddings has at least one
element and does embeddings[0], which can panic if the driver returns an empty
slice; add a guard after the Encode call to check len(embeddings) > 0 and return
a descriptive error (e.g., "empty embeddings returned from model driver") if
it's zero so the function exits safely instead of panicking; update handling
around the variables embeddings, vector, vectorSize, and vectorColumnName in the
GetVector method to only access embeddings[0] after the length check.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f8ed11fb-37a4-4119-8562-f703866b881a

📥 Commits

Reviewing files that changed from the base of the PR and between c330005 and 8c37855.

📒 Files selected for processing (14)
  • internal/entity/models/aliyun.go
  • internal/entity/models/deepseek.go
  • internal/entity/models/dummy.go
  • internal/entity/models/gitee.go
  • internal/entity/models/google.go
  • internal/entity/models/minimax.go
  • internal/entity/models/moonshot.go
  • internal/entity/models/siliconflow.go
  • internal/entity/models/types.go
  • internal/entity/models/volcengine.go
  • internal/entity/models/zhipu-ai.go
  • internal/entity/types.go
  • internal/service/model_bundle.go
  • internal/service/nlp/retrieval.go

@codecov

codecov Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.30%. Comparing base (c330005) to head (8c37855).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14437   +/-   ##
=======================================
  Coverage   95.30%   95.30%           
=======================================
  Files          10       10           
  Lines         703      703           
  Branches      112      112           
=======================================
  Hits          670      670           
  Misses         16       16           
  Partials       17       17           

☔ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants