Repository navigation
Go: implement provider: Baichuan - #14832
Conversation
📝 WalkthroughWalkthroughThis PR adds comprehensive Baichuan model provider support to the system. A new provider configuration defines Baichuan's API endpoints and available models. A complete driver implementation provides chat, streaming chat, and embedding operations with proper error handling for unimplemented features. The factory is updated to dispatch "baichuan" requests to the new driver. Additionally, CoHere model methods are refactored to remove conditional URL suffix fallbacks. ChangesBaichuan Provider Integration
CoHere URL Suffix Cleanup
Minor Formatting FishAudio whitespace adjustment in Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 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: 1
🧹 Nitpick comments (2)
internal/entity/models/baichuan.go (2)
22-52: ⚡ Quick winExtract duplicated httpClient initialization to a helper function.
The httpClient configuration is identical in both
NewBaichuanModelandNewInstance. Consider extracting it to a helper function to improve maintainability.♻️ Proposed refactor
+func newBaichuanHTTPClient() *http.Client { + return &http.Client{ + Timeout: 120 * time.Second, + Transport: &http.Transport{ + MaxIdleConns: 100, + MaxIdleConnsPerHost: 10, + IdleConnTimeout: 90 * time.Second, + DisableCompression: false, + }, + } +} + func NewBaichuanModel(baseURL map[string]string, urlSuffix URLSuffix) *BaichuanModel { return &BaichuanModel{ BaseURL: baseURL, URLSuffix: urlSuffix, - httpClient: &http.Client{ - Timeout: 120 * time.Second, - Transport: &http.Transport{ - MaxIdleConns: 100, - MaxIdleConnsPerHost: 10, - IdleConnTimeout: 90 * time.Second, - DisableCompression: false, - }, - }, + httpClient: newBaichuanHTTPClient(), } } func (b *BaichuanModel) NewInstance(baseURL map[string]string) ModelDriver { return &BaichuanModel{ BaseURL: baseURL, URLSuffix: b.URLSuffix, - httpClient: &http.Client{ - Timeout: 120 * time.Second, - Transport: &http.Transport{ - MaxIdleConns: 100, - MaxIdleConnsPerHost: 10, - IdleConnTimeout: 90 * time.Second, - DisableCompression: false, - }, - }, + httpClient: newBaichuanHTTPClient(), } }🤖 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/baichuan.go` around lines 22 - 52, The http.Client construction is duplicated in NewBaichuanModel and BaichuanModel.NewInstance; extract that logic into a single helper function (e.g., newBaichuanHTTPClient or buildHTTPClient) that returns *http.Client and replace the inline client initializations in both NewBaichuanModel and NewInstance with calls to that helper, keeping the same Timeout and Transport settings (MaxIdleConns, MaxIdleConnsPerHost, IdleConnTimeout, DisableCompression) so behavior is unchanged.
309-311: 💤 Low valueConsider making empty input validation consistent.
Embedreturns an empty slice for emptytexts(line 310), whileChatWithMessagesreturns an error for emptymessages(line 63). Consider whether both methods should handle empty inputs consistently, either by returning errors or empty results.🤖 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/baichuan.go` around lines 309 - 311, Embed currently returns an empty slice for empty texts while ChatWithMessages returns an error for empty messages; make behavior consistent by having Embed return an error for empty input like ChatWithMessages does. Replace the early-return in Embed (function Embed) to return (nil, fmt.Errorf("texts must not be empty")) or the same error type/message used by ChatWithMessages, update any callers/tests that expect an empty slice, and ensure the function signature and imports support returning an error.
🤖 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/baichuan.go`:
- Line 15: Remove the hardcoded API key string
"sk-6e16f0a6bfaa7fc58e30a50962665d1d" from the comment in
internal/entity/models/baichuan.go: delete the key or replace it with a
non-secret placeholder like "<REDACTED_API_KEY>" so no real key remains in
source, then search the repo for any other occurrences of that key and remove
them; ensure the application uses a secure secret source (environment variable
or secret manager) instead of embedding keys in comments or code.
---
Nitpick comments:
In `@internal/entity/models/baichuan.go`:
- Around line 22-52: The http.Client construction is duplicated in
NewBaichuanModel and BaichuanModel.NewInstance; extract that logic into a single
helper function (e.g., newBaichuanHTTPClient or buildHTTPClient) that returns
*http.Client and replace the inline client initializations in both
NewBaichuanModel and NewInstance with calls to that helper, keeping the same
Timeout and Transport settings (MaxIdleConns, MaxIdleConnsPerHost,
IdleConnTimeout, DisableCompression) so behavior is unchanged.
- Around line 309-311: Embed currently returns an empty slice for empty texts
while ChatWithMessages returns an error for empty messages; make behavior
consistent by having Embed return an error for empty input like ChatWithMessages
does. Replace the early-return in Embed (function Embed) to return (nil,
fmt.Errorf("texts must not be empty")) or the same error type/message used by
ChatWithMessages, update any callers/tests that expect an empty slice, and
ensure the function signature and imports support returning an error.
🪄 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: ef1c7163-c7ab-48c4-b877-211c60516570
📒 Files selected for processing (5)
conf/models/baichuan.jsoninternal/entity/models/baichuan.gointernal/entity/models/cohere.gointernal/entity/models/factory.gointernal/entity/models/fishaudio.go
💤 Files with no reviewable changes (1)
- internal/entity/models/cohere.go
| "time" | ||
| ) | ||
|
|
||
| // sk-6e16f0a6bfaa7fc58e30a50962665d1d |
There was a problem hiding this comment.
Remove the hardcoded API key from the comment.
This commented API key is a security risk and should not be committed to the repository, even in a comment.
🔐 Proposed fix
-// sk-6e16f0a6bfaa7fc58e30a50962665d1d
type BaichuanModel struct {📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // sk-6e16f0a6bfaa7fc58e30a50962665d1d | |
| type BaichuanModel struct { |
🤖 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/baichuan.go` at line 15, Remove the hardcoded API key
string "sk-6e16f0a6bfaa7fc58e30a50962665d1d" from the comment in
internal/entity/models/baichuan.go: delete the key or replace it with a
non-secret placeholder like "<REDACTED_API_KEY>" so no real key remains in
source, then search the repo for any other occurrences of that key and remove
them; ensure the application uses a secure secret source (environment variable
or secret manager) instead of embedding keys in comments or code.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14832 +/- ##
=======================================
Coverage 94.16% 94.16%
=======================================
Files 10 10
Lines 703 703
Branches 112 112
=======================================
Hits 662 662
Misses 25 25
Partials 16 16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
### What problem does this PR solve? This PR completes the Baichuan provider **The following functionalities are now supported:** **Baichuan:** - [x] Chat / Stream Chat - [x] Embedding - [ ] ~~Rerank~~ - [ ] ~~Model listing~~ - [ ] ~~Provider connection checking~~ - [ ] ~~Balance~~ **Verified examples from the CLI:** ```plaintext # Baichuan RAGFlow(user)> embed text 'walkerwhat' 'jumperwho' with 'Baichuan-Text-Embedding@test@baichuan' dimension 16; +-----------+-------+ | dimension | index | +-----------+-------+ | 1024 | 0 | | 1024 | 1 | +-----------+-------+ AGFlow(user)> chat with 'Baichuan-M2@test@baichuan' message 'who r u' Answer: I'm BaiChuan, a helpful AI assistant created by Baichuan-AI. I'm designed to be a knowledgeable, friendly, and reliable assistant for various tasks like answering questions, explaining concepts, writing content, and more. Feel free to ask me anything! 😊 Time: 1.637975 RAGFlow(user)> stream chat with 'Baichuan-M2@test@baichuan' message 'who r u' Answer: I'm BaiChuan-m2, an AI assistant developed by Baichuan-AI. My purpose is to help you with a wide range of tasks by providing information, answering questions, solving problems, and assisting with creative projects. Think of me as a helpful digital companion! If you have any questions or need assistance, just let me know.😊 Time: 1.692321 ``` ### Type of change - [x] New Feature (non-breaking change which adds functionality) - [x] Refactoring
What problem does this PR solve?
This PR completes the Baichuan provider
The following functionalities are now supported:
Baichuan:
RerankModel listingProvider connection checkingBalanceVerified examples from the CLI:
Type of change