feat: 改进 RequestOpenAI2ClaudeMessage 和添加 claude web search 计费 - #1384
Conversation
WalkthroughThe changes add support for Claude web search tool integration, tool choice handling, and reasoning effort configuration. New data structures and methods are introduced for managing tools, tracking web search usage, and billing for web search calls. Constants and functions for web search pricing are also added across relevant modules. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Relay
participant ClaudeAPI
Client->>Relay: Send request (may include web search/tool choice/reasoning)
Relay->>Relay: Map tools, tool choice, and reasoning effort
alt WebSearchOptions present
Relay->>Relay: Add ClaudeWebSearchTool to request
end
Relay->>ClaudeAPI: Forward constructed ClaudeRequest
ClaudeAPI-->>Relay: Respond with ClaudeUsage (may include web search usage)
Relay->>Relay: Extract web search usage and set in context
Relay->>Relay: Calculate billing for Claude web search
Relay-->>Client: Return response with usage and billing info
Possibly related PRs
Poem
✨ Finishing Touches
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. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
relay/channel/claude/relay-claude.go (2)
106-132: Consider adding validation for user location fields.The user location parsing is well-structured, but consider adding validation for the string fields to ensure they meet any API requirements (e.g., timezone format, country codes).
You could add basic validation:
if timezone, ok := approximateData["timezone"].(string); ok && timezone != "" { + // Validate timezone format if needed anthropicUserLocation.Timezone = timezone } if country, ok := approximateData["country"].(string); ok && country != "" { + // Consider validating country code format (e.g., ISO 3166) anthropicUserLocation.Country = country }
785-798: Consider adding validation for unknown tool choice values.While the current implementation handles the documented values, consider adding a default case or validation for unexpected string values.
switch toolChoiceStr { case "auto": claudeToolChoice = &dto.ClaudeToolChoice{ Type: "auto", } case "required": claudeToolChoice = &dto.ClaudeToolChoice{ Type: "any", } case "none": claudeToolChoice = &dto.ClaudeToolChoice{ Type: "none", } +default: + // Log warning or return error for unknown values + common.LogWarn(nil, fmt.Sprintf("Unknown tool_choice value: %s, defaulting to auto", toolChoiceStr)) + claudeToolChoice = &dto.ClaudeToolChoice{ + Type: "auto", + } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
dto/claude.go(3 hunks)relay/channel/claude/relay-claude.go(7 hunks)relay/relay-text.go(3 hunks)setting/operation_setting/tools.go(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
relay/relay-text.go (1)
Learnt from: 9Ninety
PR: QuantumNous/new-api#1273
File: relay/channel/gemini/relay-gemini.go:97-116
Timestamp: 2025-06-21T03:37:41.726Z
Learning: In relay/channel/gemini/relay-gemini.go, the thinking budget calculation logic (including the MaxOutputTokens multiplication) was introduced in PR #1247. PR #1273 focused specifically on decoupling the thoughts summary feature from thinking budget settings and did not modify the existing thinking budget behavior.
relay/channel/claude/relay-claude.go (1)
Learnt from: 9Ninety
PR: QuantumNous/new-api#1273
File: relay/channel/gemini/relay-gemini.go:97-116
Timestamp: 2025-06-21T03:37:41.726Z
Learning: In relay/channel/gemini/relay-gemini.go, the thinking budget calculation logic (including the MaxOutputTokens multiplication) was introduced in PR #1247. PR #1273 focused specifically on decoupling the thoughts summary feature from thinking budget settings and did not modify the existing thinking budget behavior.
🧬 Code Graph Analysis (2)
relay/relay-text.go (1)
setting/operation_setting/tools.go (1)
GetClaudeWebSearchPricePerThousand(31-33)
relay/channel/claude/relay-claude.go (3)
dto/openai_request.go (1)
WebSearchOptions(600-603)dto/claude.go (4)
ClaudeWebSearchTool(161-166)ClaudeWebSearchUserLocation(168-174)Thinking(253-256)ClaudeToolChoice(176-180)common/utils.go (1)
GetPointer(218-220)
🔇 Additional comments (7)
setting/operation_setting/tools.go (1)
26-33: LGTM! The Claude web search pricing is well-structured.The constant and getter function follow the established patterns in this file and integrate cleanly with the existing pricing system.
relay/relay-text.go (1)
409-418: Well-implemented Claude web search billing integration!The billing logic correctly:
- Retrieves the web search request count from the Gin context
- Calculates quota using the appropriate price function and multipliers
- Follows the established pattern for tool billing
- Properly updates the usage tracking in the
othermapAlso applies to: 542-546
dto/claude.go (1)
161-180: Excellent data structure design for Claude tool support!The new types and methods are well-structured:
ClaudeWebSearchToolandClaudeWebSearchUserLocationproperly model the Anthropic APIClaudeToolChoicecorrectly represents tool selection options- The
AddTool/GetToolsmethods handle type safety gracefullyProcessToolsfunction elegantly separates different tool typesClaudeServerToolUseprovides clean usage trackingAlso applies to: 200-251, 327-336
relay/channel/claude/relay-claude.go (4)
20-24: Clear and reasonable web search usage limits.The constants align well with the low/medium/high context size mapping.
160-166: Tool choice mapping is correctly implemented!The implementation properly handles:
- String values mapping ("auto" → "auto", "required" → "any", "none" → "none")
- Object form for specific tool selection
- Correct inversion of
parallel_tool_callstodisable_parallel_tool_useThis aligns perfectly with the PR objectives referencing litellm behavior.
Also applies to: 780-826
192-211: Reasoning effort mapping matches PR objectives.The budget token mappings are correctly implemented:
- low: 1280 tokens
- medium: 2048 tokens
- high: 4096 tokens
748-750: Web search usage tracking properly integrated.The code correctly extracts the web search request count from the Claude response and sets it in the Gin context for downstream billing calculations.
|
我正在寻找以claude messages格式请求openai模型的方法,请问这个issus是在实现这个功能吗,还是说我必须去别的地方寻找答案? |
alpha版本已经可以实现 |
@Calcium-Ion 在哪里设置? |
…audeMessage feat: 改进 RequestOpenAI2ClaudeMessage 和添加 claude web search 计费
参考 litellm 改进了 RequestOpenAI2ClaudeMessage:
处理 tool_choice 和 parallel_tool_calls.
"auto"对应{"type": "auto"}"required"对应{"type": "any"}"none"对应{"type": "none"}转换 openai 的
parallel_tool_calls到 Anthropicdisable_parallel_tool_use支持 claude web search tool 计费
支持 chat completion 的
web_search_options转换为 claude web search tool 调用openai
search_context_size和 Anthropicmax_uses的对应关系为:"low"对应1,"medium"对应5,"high"对应10转换
reasoning_effort为 thinking 的budget_tokens,对应关系为:"low"对应1280,"medium"对应2048,"high"对应4096Summary by CodeRabbit
New Features
Bug Fixes
Chores