fix: anthropic sdk删除proxies导致无法启动 (#962, #960)

This commit is contained in:
Junyan Qin
2024-12-23 21:35:16 +08:00
parent 6606c671b2
commit 535c4a8a11
3 changed files with 14 additions and 7 deletions
+1 -3
View File
@@ -143,9 +143,7 @@ class Application:
self.logger.warning("WebUI 文件缺失,请根据文档获取:https://docs.langbot.app/webui/intro.html") self.logger.warning("WebUI 文件缺失,请根据文档获取:https://docs.langbot.app/webui/intro.html")
return return
import socket host_ip = "127.0.0.1"
host_ip = socket.gethostbyname(socket.gethostname())
public_ip = await ip.get_myip() public_ip = await ip.get_myip()
@@ -4,6 +4,7 @@ import typing
import traceback import traceback
import anthropic import anthropic
import httpx
from .. import entities, errors, requester from .. import entities, errors, requester
@@ -21,11 +22,19 @@ class AnthropicMessages(requester.LLMAPIRequester):
client: anthropic.AsyncAnthropic client: anthropic.AsyncAnthropic
async def initialize(self): async def initialize(self):
httpx_client = anthropic._base_client.AsyncHttpxClientWrapper(
base_url=self.ap.provider_cfg.data['requester']['anthropic-messages']['base-url'],
# 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( self.client = anthropic.AsyncAnthropic(
api_key="", api_key="",
base_url=self.ap.provider_cfg.data['requester']['anthropic-messages']['base-url'], http_client=httpx_client,
timeout=self.ap.provider_cfg.data['requester']['anthropic-messages']['timeout'],
proxies=self.ap.proxy_mgr.get_forward_proxies()
) )
async def call( async def call(
+1 -1
View File
@@ -2,7 +2,7 @@ import aiohttp
async def get_myip() -> str: async def get_myip() -> str:
try: 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: async with session.get("https://ip.useragentinfo.com/myip") as response:
return await response.text() return await response.text()
except Exception as e: except Exception as e: