From 535c4a8a11911064181a18bbfa8f7be9f65c9b7a Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Mon, 23 Dec 2024 21:35:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20anthropic=20sdk=E5=88=A0=E9=99=A4proxies?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E5=90=AF=E5=8A=A8=20(#962,?= =?UTF-8?q?=20#960)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/core/app.py | 4 +--- pkg/provider/modelmgr/requesters/anthropicmsgs.py | 15 ++++++++++++--- pkg/utils/ip.py | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/core/app.py b/pkg/core/app.py index 06d8c1bc..b5d7c809 100644 --- a/pkg/core/app.py +++ b/pkg/core/app.py @@ -143,9 +143,7 @@ async def print_web_access_info(self): self.logger.warning("WebUI 文件缺失,请根据文档获取:https://docs.langbot.app/webui/intro.html") return - import socket - - host_ip = socket.gethostbyname(socket.gethostname()) + host_ip = "127.0.0.1" public_ip = await ip.get_myip() diff --git a/pkg/provider/modelmgr/requesters/anthropicmsgs.py b/pkg/provider/modelmgr/requesters/anthropicmsgs.py index a60c8f00..cf2fa2da 100644 --- a/pkg/provider/modelmgr/requesters/anthropicmsgs.py +++ b/pkg/provider/modelmgr/requesters/anthropicmsgs.py @@ -4,6 +4,7 @@ import traceback import anthropic +import httpx from .. import entities, errors, requester @@ -21,13 +22,21 @@ class AnthropicMessages(requester.LLMAPIRequester): client: anthropic.AsyncAnthropic async def initialize(self): - self.client = anthropic.AsyncAnthropic( - api_key="", + + httpx_client = anthropic._base_client.AsyncHttpxClientWrapper( base_url=self.ap.provider_cfg.data['requester']['anthropic-messages']['base-url'], - timeout=self.ap.provider_cfg.data['requester']['anthropic-messages']['timeout'], + # cast to a valid type because mypy doesn't understand our type narrowing + timeout=typing.cast(httpx.Timeout, self.ap.provider_cfg.data['requester']['anthropic-messages']['timeout']), + limits=anthropic._constants.DEFAULT_CONNECTION_LIMITS, + follow_redirects=True, proxies=self.ap.proxy_mgr.get_forward_proxies() ) + self.client = anthropic.AsyncAnthropic( + api_key="", + http_client=httpx_client, + ) + async def call( self, model: entities.LLMModelInfo, diff --git a/pkg/utils/ip.py b/pkg/utils/ip.py index 4f54bad2..1250f99e 100644 --- a/pkg/utils/ip.py +++ b/pkg/utils/ip.py @@ -2,7 +2,7 @@ async def get_myip() -> str: try: - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as session: async with session.get("https://ip.useragentinfo.com/myip") as response: return await response.text() except Exception as e: