Repository navigation
Simplify Encode - #14437
Simplify Encode#14437
Conversation
📝 WalkthroughWalkthroughThe PR standardizes the embedding API surface across all model drivers by renaming Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
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 | 🟡 MinorAdd guard for empty embeddings slice to prevent panic.
While the
Encode()call withnilforEmbeddingConfigis not an issue (the struct is currently empty with no fields, andEmbeddingModellacks a field to carry it),embeddings[0]will panic if a driver returns an empty slice. Even thoughGetVectorpasses 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
📒 Files selected for processing (14)
internal/entity/models/aliyun.gointernal/entity/models/deepseek.gointernal/entity/models/dummy.gointernal/entity/models/gitee.gointernal/entity/models/google.gointernal/entity/models/minimax.gointernal/entity/models/moonshot.gointernal/entity/models/siliconflow.gointernal/entity/models/types.gointernal/entity/models/volcengine.gointernal/entity/models/zhipu-ai.gointernal/entity/types.gointernal/service/model_bundle.gointernal/service/nlp/retrieval.go
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
Simplify Encode
Type of change