-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: 添加 Provider 级别代理支持及请求失败日志 #4949
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
Changes from all commits
b50e76a
ff1151b
a6808d4
49381bd
47013d6
014600f
d8708d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| from collections.abc import AsyncGenerator | ||
|
|
||
| import anthropic | ||
| import httpx | ||
| from anthropic import AsyncAnthropic | ||
| from anthropic.types import Message | ||
| from anthropic.types.message_delta_usage import MessageDeltaUsage | ||
|
|
@@ -14,6 +15,11 @@ | |
| from astrbot.core.provider.entities import LLMResponse, TokenUsage | ||
| from astrbot.core.provider.func_tool_manager import ToolSet | ||
| from astrbot.core.utils.io import download_image_by_url | ||
| from astrbot.core.utils.network_utils import ( | ||
| create_proxy_client, | ||
| is_connection_error, | ||
| log_connection_failure, | ||
| ) | ||
|
|
||
| from ..register import register_provider_adapter | ||
|
|
||
|
|
@@ -45,12 +51,18 @@ def __init__( | |
| api_key=self.chosen_api_key, | ||
| timeout=self.timeout, | ||
| base_url=self.base_url, | ||
| http_client=self._create_http_client(provider_config), | ||
| ) | ||
|
|
||
| self.thinking_config = provider_config.get("anth_thinking_config", {}) | ||
|
|
||
| self.set_model(provider_config.get("model", "unknown")) | ||
|
|
||
| def _create_http_client(self, provider_config: dict) -> httpx.AsyncClient | None: | ||
| """创建带代理的 HTTP 客户端""" | ||
| proxy = provider_config.get("proxy", "") | ||
| return create_proxy_client("Anthropic", proxy) | ||
|
|
||
| def _prepare_payload(self, messages: list[dict]): | ||
| """准备 Anthropic API 的请求 payload | ||
|
|
||
|
|
@@ -207,9 +219,19 @@ async def _query(self, payloads: dict, tools: ToolSet | None) -> LLMResponse: | |
| "type": "enabled", | ||
| } | ||
|
|
||
| completion = await self.client.messages.create( | ||
| **payloads, stream=False, extra_body=extra_body | ||
| ) | ||
| try: | ||
| completion = await self.client.messages.create( | ||
| **payloads, stream=False, extra_body=extra_body | ||
| ) | ||
| except httpx.RequestError as e: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. openai source 我看记录了 os.environ 里面的 proxy 作为provider_config.get("proxy", "")为空时的fallback,但是其他source好像没有。是不是可以考虑把 os.environ 里面的 proxy fallback 放到 log_connection_failure 内。如 proxy 为空的时候,就换成os.environ 里面的 proxy 去输出日志。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okk我看到了
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Soulter review |
||
| proxy = self.provider_config.get("proxy", "") | ||
| log_connection_failure("Anthropic", e, proxy) | ||
| raise | ||
| except Exception as e: | ||
| if is_connection_error(e): | ||
| proxy = self.provider_config.get("proxy", "") | ||
| log_connection_failure("Anthropic", e, proxy) | ||
| raise | ||
|
|
||
| assert isinstance(completion, Message) | ||
| logger.debug(f"completion: {completion}") | ||
|
|
@@ -622,3 +644,7 @@ async def get_models(self) -> list[str]: | |
|
|
||
| def set_key(self, key: str): | ||
| self.chosen_api_key = key | ||
|
|
||
| async def terminate(self): | ||
| if self.client: | ||
| await self.client.close() | ||
Uh oh!
There was an error while loading. Please reload this page.