|
21 | 21 | from .logger import MetaChainLogger, LoggerManager |
22 | 22 | from httpx import RemoteProtocolError, ConnectError, TimeoutException |
23 | 23 | from litellm.exceptions import APIError |
24 | | -from tenacity import RetryCallState |
25 | 24 | from research_agent.constant import API_BASE_URL, CHEEP_MODEL, COMPLETION_MODEL, NOT_SUPPORT_SENDER, MUST_ADD_USER, NOT_SUPPORT_FN_CALL, NOT_USE_FN_CALL |
26 | 25 | from research_agent.inno.fn_call_converter import convert_tools_to_description, convert_non_fncall_messages_to_fncall_messages, SYSTEM_PROMPT_SUFFIX_TEMPLATE, convert_fn_messages_to_non_fn_messages, interleave_user_into_messages |
27 | 26 | from research_agent.inno.memory.utils import encode_string_by_tiktoken, decode_tokens_by_tiktoken |
|
33 | 32 | LLM_RETRY_ATTEMPTS = int(os.getenv("LLM_RETRY_ATTEMPTS", "3")) |
34 | 33 | LLM_RETRY_BACKOFF_SECONDS = int(os.getenv("LLM_RETRY_BACKOFF_SECONDS", "5")) |
35 | 34 |
|
36 | | -def should_retry_error(retry_state: RetryCallState): |
37 | | - """检查是否应该重试错误 |
38 | | - |
39 | | - Args: |
40 | | - retry_state: RetryCallState对象,包含重试状态信息 |
41 | | - |
42 | | - Returns: |
43 | | - bool: 是否应该重试 |
44 | | - """ |
45 | | - if retry_state.outcome is None: |
46 | | - return False |
47 | | - |
48 | | - exception = retry_state.outcome.exception() |
49 | | - if exception is None: |
50 | | - return False |
51 | | - |
52 | | - print(f"Caught exception: {type(exception).__name__} - {str(exception)}") |
53 | | - |
54 | | - # 匹配更多错误类型 |
55 | | - if isinstance(exception, (APIError, RemoteProtocolError, ConnectError, TimeoutException, TimeoutError)): |
56 | | - return True |
57 | | - |
58 | | - # 通过错误消息匹配 |
59 | | - error_msg = str(exception).lower() |
60 | | - return any([ |
61 | | - "connection error" in error_msg, |
62 | | - "server disconnected" in error_msg, |
63 | | - "eof occurred" in error_msg, |
64 | | - "timeout" in error_msg, |
65 | | - "rate limit" in error_msg, # 添加 rate limit 错误检查 |
66 | | - "rate_limit_error" in error_msg, # Anthropic 的错误类型 |
67 | | - "too many requests" in error_msg, # HTTP 429 错误 |
68 | | - "overloaded" in error_msg, # 添加 Anthropic overloaded 错误 |
69 | | - "overloaded_error" in error_msg, # 添加 Anthropic overloaded 错误的另一种形式 |
70 | | - "负载已饱和" in error_msg, # 添加中文错误消息匹配 |
71 | | - "error code: 429" in error_msg, # 添加 HTTP 429 状态码匹配 |
72 | | - "context_length_exceeded" in error_msg # 添加上下文长度超限错误匹配 |
73 | | - ]) |
74 | 35 | __CTX_VARS_NAME__ = "context_variables" |
75 | 36 | logger = LoggerManager.get_logger() |
76 | 37 |
|
|
0 commit comments