Repository navigation
Go: implement embed, rerank, tts for AstraFlow - #15135
Conversation
📝 WalkthroughWalkthroughThe PR extends the Astraflow provider with embedding, reranking, and text-to-speech support. Configuration registers new API endpoints and adds three new model types to the model registry. Implementation replaces stub methods with POST-based flows that validate input, select regions, send requests, unmarshal responses, and return typed results. ChangesAstraflow Multi-Modality Support
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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.
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 `@internal/entity/models/astraflow.go`:
- Around line 459-467: The Embed, Rerank, and AudioSpeech flows are
dereferencing nullable pointers and directly indexing a.BaseURL by region which
can panic or yield bad URLs; update Embed, Rerank, AudioSpeech (and affected
blocks around where modelName, apiConfig.ApiKey, rerankConfig are used) to first
validate non-nil pointers (modelName, apiConfig, apiConfig.ApiKey, rerankConfig)
and return clear errors if missing, then compute the region and call
baseURLForRegion(region) (instead of a.BaseURL[region]) to get a safe base URL
before constructing fmt.Sprintf("%s/%s", ...); also replace direct dereferences
of *modelName and *apiConfig.ApiKey with local validated variables so URL and
reqBody construction cannot panic.
- Around line 476-477: The HTTP POST requests created with http.NewRequest
(variables req, url, jsonData) lack per-request deadlines; wrap the parent
context with context.WithTimeout (choose a sensible timeout or a configurable
value), defer cancel(), attach it to the request via req =
req.WithContext(ctxWithTimeout) before calling c.httpClient.Do, and handle the
context timeout error path; apply the same change to the other http.NewRequest
sites mentioned (the occurrences around lines noted: the other request creations
using req, url, and jsonData in the same file).
🪄 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: 8fd77c49-0ce2-4ae7-bb8a-9548d2a35381
📒 Files selected for processing (2)
conf/models/astraflow.jsoninternal/entity/models/astraflow.go
| var region = "default" | ||
| if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" { | ||
| region = *apiConfig.Region | ||
| } | ||
|
|
||
| url := fmt.Sprintf("%s/%s", a.BaseURL[region], a.URLSuffix.Embedding) | ||
|
|
||
| reqBody := map[string]interface{}{ | ||
| "model": *modelName, |
There was a problem hiding this comment.
Validate required pointers and use baseURLForRegion before request construction.
Embed, Rerank, and AudioSpeech dereference nullable inputs (modelName, apiConfig.ApiKey, rerankConfig) and build URLs from a.BaseURL[region] directly. This can panic on nil input and can produce malformed URLs for unknown regions.
Suggested hardening pattern
func (a *AstraflowModel) Embed(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([]EmbeddingData, error) {
+ if modelName == nil || *modelName == "" {
+ return nil, fmt.Errorf("model name is required")
+ }
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return nil, fmt.Errorf("api key is required")
+ }
if len(texts) == 0 {
return []EmbeddingData{}, nil
}
region := "default"
if apiConfig.Region != nil && *apiConfig.Region != "" {
region = *apiConfig.Region
}
- url := fmt.Sprintf("%s/%s", a.BaseURL[region], a.URLSuffix.Embedding)
+ baseURL, err := a.baseURLForRegion(region)
+ if err != nil {
+ return nil, err
+ }
+ url := fmt.Sprintf("%s/%s", baseURL, a.URLSuffix.Embedding) func (a *AstraflowModel) Rerank(..., rerankConfig *RerankConfig) (*RerankResponse, error) {
+ if modelName == nil || *modelName == "" {
+ return nil, fmt.Errorf("model name is required")
+ }
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return nil, fmt.Errorf("api key is required")
+ }
+ topN := len(documents)
+ if rerankConfig != nil && rerankConfig.TopN > 0 {
+ topN = rerankConfig.TopN
+ }Also applies to: 482-483, 530-547, 559-560, 621-631
🤖 Prompt for 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.
In `@internal/entity/models/astraflow.go` around lines 459 - 467, The Embed,
Rerank, and AudioSpeech flows are dereferencing nullable pointers and directly
indexing a.BaseURL by region which can panic or yield bad URLs; update Embed,
Rerank, AudioSpeech (and affected blocks around where modelName,
apiConfig.ApiKey, rerankConfig are used) to first validate non-nil pointers
(modelName, apiConfig, apiConfig.ApiKey, rerankConfig) and return clear errors
if missing, then compute the region and call baseURLForRegion(region) (instead
of a.BaseURL[region]) to get a safe base URL before constructing
fmt.Sprintf("%s/%s", ...); also replace direct dereferences of *modelName and
*apiConfig.ApiKey with local validated variables so URL and reqBody construction
cannot panic.
| req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) | ||
| if err != nil { |
There was a problem hiding this comment.
Add deadline contexts to non-stream HTTP calls.
These methods use http.NewRequest(...) without a timeout context. Since this driver’s http.Client has no client-level Timeout, stalled non-stream calls can block indefinitely.
Suggested fix
+ctx, cancel := context.WithTimeout(context.Background(), nonStreamCallTimeout)
+defer cancel()
-req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))Based on learnings: if httpClient.Timeout is intentionally unset, per-request context.WithTimeout is the legitimate overall deadline.
Also applies to: 554-555, 647-648
🤖 Prompt for 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.
In `@internal/entity/models/astraflow.go` around lines 476 - 477, The HTTP POST
requests created with http.NewRequest (variables req, url, jsonData) lack
per-request deadlines; wrap the parent context with context.WithTimeout (choose
a sensible timeout or a configurable value), defer cancel(), attach it to the
request via req = req.WithContext(ctxWithTimeout) before calling
c.httpClient.Do, and handle the context timeout error path; apply the same
change to the other http.NewRequest sites mentioned (the occurrences around
lines noted: the other request creations using req, url, and jsonData in the
same file).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15135 +/- ##
==========================================
- Coverage 94.53% 93.12% -1.41%
==========================================
Files 10 10
Lines 713 713
Branches 116 116
==========================================
- Hits 674 664 -10
- Misses 25 29 +4
- Partials 14 20 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
implement embed, rerank, tts for AstraFlow
Verify from CLI
Type of change