agent增加system_proxy_enable参数
This commit is contained in:
@@ -48,6 +48,7 @@ class AgentConfig:
|
|||||||
timeout: int = 1200 # 单位(秒),这个值是httpx.TimeOut中read的值,并非总的超时时间
|
timeout: int = 1200 # 单位(秒),这个值是httpx.TimeOut中read的值,并非总的超时时间
|
||||||
thinking: ThinkingMode = "disable"
|
thinking: ThinkingMode = "disable"
|
||||||
retry: int = 2
|
retry: int = 2
|
||||||
|
system_proxy_enable: bool = USE_PROXY
|
||||||
|
|
||||||
|
|
||||||
class TotalErrorCounter:
|
class TotalErrorCounter:
|
||||||
@@ -141,7 +142,7 @@ def extract_token_info(response_data: dict) -> tuple[int, int, int, int]:
|
|||||||
return input_tokens, cached_tokens, output_tokens, reasoning_tokens
|
return input_tokens, cached_tokens, output_tokens, reasoning_tokens
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
print(f"获取token发生错误:{e.__repr__()}")
|
print(f"获取token发生错误:{e.__repr__()}")
|
||||||
return -1,-1,-1,-1
|
return -1, -1, -1, -1
|
||||||
|
|
||||||
|
|
||||||
class TokenCounter:
|
class TokenCounter:
|
||||||
@@ -248,6 +249,7 @@ class Agent:
|
|||||||
|
|
||||||
self.retry = config.retry
|
self.retry = config.retry
|
||||||
|
|
||||||
|
self.system_proxy_enable=config.system_proxy_enable
|
||||||
def _add_thinking_mode(self, data: dict):
|
def _add_thinking_mode(self, data: dict):
|
||||||
if self.domain not in self._think_factory:
|
if self.domain not in self._think_factory:
|
||||||
return
|
return
|
||||||
@@ -459,7 +461,7 @@ class Agent:
|
|||||||
semaphore = asyncio.Semaphore(max_concurrent)
|
semaphore = asyncio.Semaphore(max_concurrent)
|
||||||
tasks = []
|
tasks = []
|
||||||
|
|
||||||
proxies = get_httpx_proxies() if USE_PROXY else None
|
proxies = get_httpx_proxies() if self.system_proxy_enable else None
|
||||||
|
|
||||||
limits = httpx.Limits(
|
limits = httpx.Limits(
|
||||||
max_connections=self.max_concurrent * 2, # 为重试和并发预留空间
|
max_connections=self.max_concurrent * 2, # 为重试和并发预留空间
|
||||||
@@ -498,7 +500,7 @@ class Agent:
|
|||||||
|
|
||||||
# 新增:打印token使用统计
|
# 新增:打印token使用统计
|
||||||
token_stats = self.token_counter.get_stats()
|
token_stats = self.token_counter.get_stats()
|
||||||
if token_stats['input_tokens']<0:
|
if token_stats['input_tokens'] < 0:
|
||||||
self.logger.info("Token统计失败")
|
self.logger.info("Token统计失败")
|
||||||
else:
|
else:
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
@@ -710,7 +712,7 @@ class Agent:
|
|||||||
max_connections=self.max_concurrent * 2, # 允许连接复用
|
max_connections=self.max_concurrent * 2, # 允许连接复用
|
||||||
max_keepalive_connections=self.max_concurrent, # 保持活跃连接
|
max_keepalive_connections=self.max_concurrent, # 保持活跃连接
|
||||||
)
|
)
|
||||||
proxies = get_httpx_proxies() if USE_PROXY else None
|
proxies = get_httpx_proxies() if self.system_proxy_enable else None
|
||||||
with httpx.Client(
|
with httpx.Client(
|
||||||
trust_env=False, proxies=proxies, verify=False, limits=limits
|
trust_env=False, proxies=proxies, verify=False, limits=limits
|
||||||
) as client:
|
) as client:
|
||||||
|
|||||||
@@ -38,7 +38,8 @@ class AssTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.insert_mode = config.insert_mode
|
self.insert_mode = config.insert_mode
|
||||||
|
|||||||
@@ -53,7 +53,8 @@ class DocxTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.insert_mode = config.insert_mode
|
self.insert_mode = config.insert_mode
|
||||||
|
|||||||
@@ -44,7 +44,8 @@ class EpubTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.insert_mode = config.insert_mode
|
self.insert_mode = config.insert_mode
|
||||||
|
|||||||
@@ -101,7 +101,8 @@ class HtmlTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.insert_mode = config.insert_mode
|
self.insert_mode = config.insert_mode
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ class JsonTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.json_paths = config.json_paths
|
self.json_paths = config.json_paths
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ class MDTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry)
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable)
|
||||||
self.translate_agent = MDTranslateAgent(agent_config)
|
self.translate_agent = MDTranslateAgent(agent_config)
|
||||||
|
|
||||||
def translate(self, document: MarkdownDocument) -> Self:
|
def translate(self, document: MarkdownDocument) -> Self:
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ class SrtTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.insert_mode = config.insert_mode
|
self.insert_mode = config.insert_mode
|
||||||
|
|||||||
@@ -58,7 +58,8 @@ class TXTTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.insert_mode = config.insert_mode
|
self.insert_mode = config.insert_mode
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ class XlsxTranslator(AiTranslator):
|
|||||||
timeout=config.timeout,
|
timeout=config.timeout,
|
||||||
logger=self.logger,
|
logger=self.logger,
|
||||||
glossary_dict=config.glossary_dict,
|
glossary_dict=config.glossary_dict,
|
||||||
retry=config.retry
|
retry=config.retry,
|
||||||
|
system_proxy_enable=config.system_proxy_enable
|
||||||
)
|
)
|
||||||
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
self.translate_agent = SegmentsTranslateAgent(agent_config)
|
||||||
self.insert_mode = config.insert_mode
|
self.insert_mode = config.insert_mode
|
||||||
|
|||||||
Reference in New Issue
Block a user