-
-
Notifications
You must be signed in to change notification settings - Fork 2k
支持 Anthropic API 拉取模型列表(新结构体和认证头处理) #1571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: alpha
Are you sure you want to change the base?
Conversation
重构 FetchUpstreamModels 和 FetchModels,通过辅助函数(getChannelBaseURL、buildModelsURL、getAuthHeaders、parseModelsResponse)提高了代码复用性和可维护性。 新 FetchModels 改进了错误处理(更明确的错误信息)和类型检查。
使用已有的 getChannelBaseURL 函数,避免重复实现相同的逻辑
WalkthroughAdds Anthropic channel support and channel-aware model-fetching logic in controller/channel.go, including new Anthropic response types, an Anthropic-specific HTTP request helper, and branched URL/header/parsing paths for Anthropic, Gemini, Ali, and default channels. Also updates imports and centralizes ID normalization (e.g., trimming Gemini “models/” prefixes). Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Controller
participant Anthropic as Anthropic API
participant Gemini as Gemini API
participant Other as Other Upstream
Client->>Controller: FetchModels / FetchUpstreamModels(channel, key)
alt channel == Anthropic
Controller->>Anthropic: GET models (x-api-key, anthropic-version)
Anthropic-->>Controller: AnthropicModelsResponse
Controller-->>Client: model IDs (from Data)
else channel == Gemini
Controller->>Gemini: GET /v1beta/openai/models (Bearer)
Gemini-->>Controller: OpenAI-compatible response
Controller-->>Client: model IDs (trim "models/" prefix)
else channel == Ali or default
Controller->>Other: GET channel-specific URL (Bearer)
Other-->>Controller: OpenAI-compatible response
Controller-->>Client: model IDs
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~18 minutes Possibly related PRs
Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (3)
controller/channel.go (3)
236-237
: Consider using consistent error message languageThe error messages use Chinese ("解析响应失败") while other parts of the codebase might use English. Consider maintaining consistency across error messages.
- "message": fmt.Sprintf("解析响应失败: %s", err.Error()), + "message": fmt.Sprintf("Failed to parse response: %s", err.Error()),Also applies to: 256-257
291-291
: Consider making the Anthropic API version configurableThe API version "2023-06-01" is hardcoded. Consider making this configurable or at least defining it as a constant for easier maintenance.
+const AnthropicAPIVersion = "2023-06-01" + func GetAnthropicResponseBody(method, url, apiKey string) ([]byte, error) { // ... - req.Header.Set("anthropic-version", "2023-06-01") + req.Header.Set("anthropic-version", AnthropicAPIVersion)
279-306
: Consider reusing existing GetResponseBody function to reduce code duplicationThe
GetAnthropicResponseBody
function duplicates logic from the existingGetResponseBody
function incontroller/channel-billing.go
. Consider refactoring to reuse the existing function by passing appropriate headers.Instead of creating a new function, you could modify the calling code to use the existing
GetResponseBody
:// In FetchUpstreamModels if channel.Type == constant.ChannelTypeAnthropic { headers := http.Header{} headers.Set("x-api-key", key) headers.Set("anthropic-version", "2023-06-01") headers.Set("Content-Type", "application/json") body, err = GetResponseBody("GET", url, channel, headers) }This would eliminate the need for the separate
GetAnthropicResponseBody
function and reduce code duplication.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
controller/channel.go
(6 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
controller/channel.go (3)
constant/channel.go (3)
ChannelTypeAnthropic
(18-18)ChannelTypeGemini
(28-28)ChannelTypeAli
(21-21)controller/channel-billing.go (2)
GetResponseBody
(130-154)GetAuthHeader
(124-128)common/gin.go (1)
ApiError
(94-99)
🔇 Additional comments (2)
controller/channel.go (2)
46-58
: LGTM! Well-structured Anthropic model types.The new struct definitions properly represent the Anthropic API response format with appropriate JSON tags and pagination support.
915-1017
: LGTM! Consistent implementation of Anthropic support in FetchModels.The modifications properly handle:
- URL construction for different channel types
- Anthropic-specific headers
- Response parsing for both Anthropic and non-Anthropic formats
- Consistent Gemini ID prefix trimming
The implementation aligns well with the FetchUpstreamModels function.
支持 Anthropic API 拉取模型列表(新结构体和认证头处理)
Summary by CodeRabbit