Skip to content

fix: 添加了第三方api中转对Anthropic格式的支持#24

Merged
shenminglinyi merged 1 commit into
shenminglinyi:masterfrom
xiaoyi198:feat/llm-settings-ui
Apr 15, 2026
Merged

fix: 添加了第三方api中转对Anthropic格式的支持#24
shenminglinyi merged 1 commit into
shenminglinyi:masterfrom
xiaoyi198:feat/llm-settings-ui

Conversation

@xiaoyi198
Copy link
Copy Markdown
Collaborator

@xiaoyi198 xiaoyi198 commented Apr 15, 2026

增加了使用Anthropic格式调用第三方中转站的功能
但是有什么意义呢?谁会在中转站使用Anthropic格式调用模型?

Anthropic 提供商

generate() 的 messages 包含了 Anthropic 不支持的 "role": "system",导致代理返回 500;改为仅传 user message,system prompt 走顶层参数
generate() 未传 base_url 给 SDK,自定义代理对调用不生效
兼容 Extended Thinking:遍历 response.content 找 TextBlock,不再硬取 index 0
base_url 统一归一化,strip 尾部 /v1,避免拼接出 /v1/v1/messages
model fallback chain:config.model → settings.default_model → DEFAULT_MODEL

模型列表获取

移除硬编码 _ANTHROPIC_MODELS,改为实时调用 /v1/models API
支持 Anthropic 原生认证(x-api-key)失败后自动 fallback 到 OpenAI Bearer 认证
fallback 时正确补全 /v1 路径

Summary by CodeRabbit

  • Bug Fixes

    • Improved Anthropic API endpoint normalization and configuration reliability
    • Enhanced response parsing to correctly identify and extract text content
    • Added fallback mechanism for model fetching to improve resilience
  • Refactor

    • Optimized model selection to prioritize configured default models
    • Streamlined Anthropic integration initialization logic

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 15, 2026

📝 Walkthrough

Walkthrough

Modified Anthropic provider implementation to normalize base URLs, prefer default model settings, simplify message formatting to single-message payload, enhance response parsing through content block iteration, and implemented dynamic model fetching with endpoint-aware URL construction and fallback retry logic for robustness.

Changes

Cohort / File(s) Summary
Anthropic Provider Updates
infrastructure/ai/providers/anthropic_provider.py
Normalized base URL handling by trimming trailing slashes and /v1 suffix. Updated generate() and stream_generate() to prefer default_model when model is unset. Changed request messages from prompt.to_messages() to single prompt.user message. Reworked response parsing to iterate through content blocks and extract first text block, raising error if none found.
Settings and Model Discovery
interfaces/api/v1/core/settings.py
Removed static _ANTHROPIC_MODELS list. Updated fetch_models to dynamically call new _fetch_anthropic_models() for Anthropic provider. Implemented _fetch_anthropic_models() with base URL normalization, endpoint-aware /models URL construction, Anthropic API headers (x-api-key, anthropic-version), and fallback retry using OpenAI-style endpoint on failure with 502 error handling.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 ✨ The Anthropic paths are now so clean,
With normalized URLs in between!
Messages simplified, responses parsed right,
Models discovered with fallback delight.
From base to endpoint, the logic takes flight! 🚀

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description provides comprehensive details about the changes, objectives, and technical context. However, it does not follow the required template structure with checkboxes for testing or risk assessment sections. Add the required template sections including testing checkboxes, risk assessment, and rollback procedures to align with repository standards.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding Anthropic format support for third-party API relay services. It directly relates to the core modifications in both files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@interfaces/api/v1/core/settings.py`:
- Around line 161-171: The current exception handling in the model-fetch block
drops exception context and reports the wrong error when the OpenAI fallback
fails; update the two raises to preserve chaining and include both errors: when
catching Exception as exc and immediately raising HTTPException (the branch
where not base_url), re-raise using "from exc" so the original stack is
preserved; in the OpenAI fallback, catch the second exception (e.g., exc2) and
raise HTTPException with a combined message referencing both exc and exc2 (e.g.,
"Failed to fetch Anthropic models: {exc}; OpenAI fallback failed: {exc2}") and
chain it using "from exc2"; reference _fetch_openai_models, the local variables
exc and fallback_base, and the HTTPException raises to locate and apply these
changes.
🪄 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: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8c92fb89-ae1e-4d7f-bbc9-80f55e45fdab

📥 Commits

Reviewing files that changed from the base of the PR and between 027b66b and 5093f58.

📒 Files selected for processing (2)
  • infrastructure/ai/providers/anthropic_provider.py
  • interfaces/api/v1/core/settings.py

Comment on lines +161 to +171
except Exception as exc:
if not base_url:
raise HTTPException(502, f"Failed to fetch Anthropic models: {exc}")
logger.info("Anthropic-style fetch failed, trying OpenAI-style: %s", exc)
try:
fallback_base = base_url.rstrip("/")
if not fallback_base.endswith("/v1"):
fallback_base += "/v1"
return await _fetch_openai_models(api_key, fallback_base)
except Exception:
raise HTTPException(502, f"Failed to fetch models (tried both Anthropic and OpenAI format): {exc}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add exception chaining and fix error message on fallback failure.

Two issues:

  1. Missing from exc/from None on raise HTTPException statements loses stack trace context for debugging.

  2. Line 171 reports the original Anthropic exception exc, but the OpenAI fallback also failed. The error message should include both failures for better diagnostics.

🔧 Proposed fix
     except Exception as exc:
         if not base_url:
-            raise HTTPException(502, f"Failed to fetch Anthropic models: {exc}")
+            raise HTTPException(502, f"Failed to fetch Anthropic models: {exc}") from None
         logger.info("Anthropic-style fetch failed, trying OpenAI-style: %s", exc)
         try:
             fallback_base = base_url.rstrip("/")
             if not fallback_base.endswith("/v1"):
                 fallback_base += "/v1"
             return await _fetch_openai_models(api_key, fallback_base)
-        except Exception:
-            raise HTTPException(502, f"Failed to fetch models (tried both Anthropic and OpenAI format): {exc}")
+        except Exception as openai_exc:
+            raise HTTPException(
+                502,
+                f"Failed to fetch models (Anthropic: {exc}, OpenAI: {openai_exc})"
+            ) from None
📝 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.

Suggested change
except Exception as exc:
if not base_url:
raise HTTPException(502, f"Failed to fetch Anthropic models: {exc}")
logger.info("Anthropic-style fetch failed, trying OpenAI-style: %s", exc)
try:
fallback_base = base_url.rstrip("/")
if not fallback_base.endswith("/v1"):
fallback_base += "/v1"
return await _fetch_openai_models(api_key, fallback_base)
except Exception:
raise HTTPException(502, f"Failed to fetch models (tried both Anthropic and OpenAI format): {exc}")
except Exception as exc:
if not base_url:
raise HTTPException(502, f"Failed to fetch Anthropic models: {exc}") from None
logger.info("Anthropic-style fetch failed, trying OpenAI-style: %s", exc)
try:
fallback_base = base_url.rstrip("/")
if not fallback_base.endswith("/v1"):
fallback_base += "/v1"
return await _fetch_openai_models(api_key, fallback_base)
except Exception as openai_exc:
raise HTTPException(
502,
f"Failed to fetch models (Anthropic: {exc}, OpenAI: {openai_exc})"
) from None
🧰 Tools
🪛 Ruff (0.15.10)

[warning] 161-161: Do not catch blind exception: Exception

(BLE001)


[warning] 163-163: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


[warning] 170-170: Do not catch blind exception: Exception

(BLE001)


[warning] 171-171: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@interfaces/api/v1/core/settings.py` around lines 161 - 171, The current
exception handling in the model-fetch block drops exception context and reports
the wrong error when the OpenAI fallback fails; update the two raises to
preserve chaining and include both errors: when catching Exception as exc and
immediately raising HTTPException (the branch where not base_url), re-raise
using "from exc" so the original stack is preserved; in the OpenAI fallback,
catch the second exception (e.g., exc2) and raise HTTPException with a combined
message referencing both exc and exc2 (e.g., "Failed to fetch Anthropic models:
{exc}; OpenAI fallback failed: {exc2}") and chain it using "from exc2";
reference _fetch_openai_models, the local variables exc and fallback_base, and
the HTTPException raises to locate and apply these changes.

@shenminglinyi shenminglinyi merged commit de4ed67 into shenminglinyi:master Apr 15, 2026
1 check passed
shenminglinyi added a commit that referenced this pull request Apr 17, 2026
fix: 添加了第三方api中转对Anthropic格式的支持
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants