feat: Update core agent logic, code execution utilities, and LLM configuration.
This commit is contained in:
@@ -35,6 +35,7 @@ class CodeExecutor:
|
||||
"duckdb",
|
||||
"scipy",
|
||||
"sklearn",
|
||||
"sklearn.feature_extraction.text",
|
||||
"statsmodels",
|
||||
"plotly",
|
||||
"dash",
|
||||
|
||||
@@ -29,6 +29,22 @@ def extract_code_from_response(response: str) -> Optional[str]:
|
||||
end = response.find('```', start)
|
||||
if end != -1:
|
||||
return response[start:end].strip()
|
||||
|
||||
# 尝试提取 code: | 形式的代码块(针对YAML格式错误但结构清晰的情况)
|
||||
import re
|
||||
# 匹配 code: | 后面的内容,直到遇到下一个键(next_key:)或结尾
|
||||
# 假设代码块至少缩进2个空格
|
||||
pattern = r'code:\s*\|\s*\n((?: {2,}.*\n?)+)'
|
||||
match = re.search(pattern, response)
|
||||
if match:
|
||||
code_block = match.group(1)
|
||||
# 尝试去除公共缩进
|
||||
try:
|
||||
import textwrap
|
||||
return textwrap.dedent(code_block).strip()
|
||||
except:
|
||||
return code_block.strip()
|
||||
|
||||
elif '```' in response:
|
||||
start = response.find('```') + 3
|
||||
end = response.find('```', start)
|
||||
|
||||
@@ -97,23 +97,48 @@ class AsyncFallbackOpenAIClient:
|
||||
print(f"❌ {api_name} API 在达到最大重试次数后仍然失败。")
|
||||
except APIStatusError as e: # API 返回的特定状态码错误
|
||||
is_content_filter_error = False
|
||||
if e.status_code == 400:
|
||||
try:
|
||||
error_json = e.response.json()
|
||||
error_details = error_json.get("error", {})
|
||||
if (error_details.get("code") == self.content_filter_error_code and
|
||||
self.content_filter_error_field in error_json):
|
||||
is_content_filter_error = True
|
||||
except Exception:
|
||||
pass # 解析错误响应失败,不认为是内容过滤错误
|
||||
retry_after = None
|
||||
|
||||
# 尝试解析错误详情以获取更多信息(如 Google RPC RetryInfo)
|
||||
try:
|
||||
error_json = e.response.json()
|
||||
error_details = error_json.get("error", {})
|
||||
|
||||
# 检查内容过滤错误(针对特定服务商)
|
||||
if (error_details.get("code") == self.content_filter_error_code and
|
||||
self.content_filter_error_field in error_json):
|
||||
is_content_filter_error = True
|
||||
|
||||
# 检查 Google RPC RetryInfo
|
||||
# 格式示例: {'error': {'details': [{'@type': 'type.googleapis.com/google.rpc.RetryInfo', 'retryDelay': '38s'}]}}
|
||||
if "details" in error_details:
|
||||
for detail in error_details["details"]:
|
||||
if detail.get("@type") == "type.googleapis.com/google.rpc.RetryInfo":
|
||||
delay_str = detail.get("retryDelay", "")
|
||||
if delay_str.endswith("s"):
|
||||
try:
|
||||
retry_after = float(delay_str[:-1])
|
||||
print(f"⏳ 收到服务器 RetryInfo,等待时间: {retry_after}秒")
|
||||
except ValueError:
|
||||
pass
|
||||
except Exception:
|
||||
pass # 解析错误响应失败,忽略
|
||||
|
||||
if is_content_filter_error and api_name == "主": # 如果是主 API 的内容过滤错误,则直接抛出以便回退
|
||||
raise e
|
||||
|
||||
last_exception = e
|
||||
print(f"⚠️ {api_name} API 调用时发生 APIStatusError ({e.status_code}): {e}. 尝试次数 {attempt + 1}/{max_retries + 1}")
|
||||
|
||||
if attempt < max_retries:
|
||||
await asyncio.sleep(self.retry_delay_seconds * (attempt + 1))
|
||||
# 如果获取到了明确的 retry_after,则使用它;否则使用默认的指数退避
|
||||
wait_time = retry_after if retry_after is not None else (self.retry_delay_seconds * (attempt + 1))
|
||||
# 如果是 429 Too Many Requests 且没有解析出 retry_after,建议加大等待时间
|
||||
if e.status_code == 429 and retry_after is None:
|
||||
wait_time = max(wait_time, 5.0 * (attempt + 1)) # 429 默认至少等 5 秒
|
||||
|
||||
print(f"💤 将等待 {wait_time:.2f} 秒后重试...")
|
||||
await asyncio.sleep(wait_time)
|
||||
else:
|
||||
print(f"❌ {api_name} API 在达到最大重试次数后仍然失败 (APIStatusError)。")
|
||||
except APIError as e: # 其他不可轻易重试的 OpenAI 错误
|
||||
|
||||
Reference in New Issue
Block a user