refactor: 统一类型注解风格并优化代码结构
- 将裸 except 改为显式 Exception 捕获 - 用列表推导式替换冗余 for 循环 - 为类属性添加 ClassVar 注解 - 统一 Union/Optional 写法为 | - 移除未使用的导入 - 修复 SQLAlchemy 空值比较语法 - 优化字符串拼接与字典更新逻辑 - 补充缺失的 noqa 注释与异常链 BREAKING CHANGE: 所有插件基类的类级字段现要求显式 ClassVar 注解,自定义插件需同步更新
This commit is contained in:
@@ -372,10 +372,7 @@ def _default_normal_response_parser(
|
||||
|
||||
# 解析文本内容
|
||||
if "content" in candidate and "parts" in candidate["content"]:
|
||||
content_parts = []
|
||||
for part in candidate["content"]["parts"]:
|
||||
if "text" in part:
|
||||
content_parts.append(part["text"])
|
||||
content_parts = [part["text"] for part in candidate["content"]["parts"] if "text" in part]
|
||||
|
||||
if content_parts:
|
||||
api_response.content = "".join(content_parts)
|
||||
|
||||
@@ -3,7 +3,7 @@ import base64
|
||||
import io
|
||||
import re
|
||||
from collections.abc import Callable, Coroutine, Iterable
|
||||
from typing import Any
|
||||
from typing import Any, ClassVar
|
||||
|
||||
import orjson
|
||||
from json_repair import repair_json
|
||||
@@ -384,7 +384,7 @@ def _default_normal_response_parser(
|
||||
@client_registry.register_client_class("openai")
|
||||
class OpenaiClient(BaseClient):
|
||||
# 类级别的全局缓存:所有 OpenaiClient 实例共享
|
||||
_global_client_cache: dict[int, AsyncOpenAI] = {}
|
||||
_global_client_cache: ClassVar[dict[int, AsyncOpenAI] ] = {}
|
||||
"""全局 AsyncOpenAI 客户端缓存:config_hash -> AsyncOpenAI 实例"""
|
||||
|
||||
def __init__(self, api_provider: APIProvider):
|
||||
|
||||
@@ -535,7 +535,7 @@ class _RequestExecutor:
|
||||
retry_interval = api_provider.retry_interval
|
||||
|
||||
if isinstance(e, NetworkConnectionError | ReqAbortException):
|
||||
return self._check_retry(remain_try, retry_interval, "连接异常", model_name)
|
||||
return await self._check_retry(remain_try, retry_interval, "连接异常", model_name)
|
||||
elif isinstance(e, RespNotOkException):
|
||||
return await self._handle_resp_not_ok(e, model_info, api_provider, remain_try, messages_info)
|
||||
elif isinstance(e, RespParseException):
|
||||
@@ -1064,7 +1064,8 @@ class LLMRequest:
|
||||
# 遍历工具的参数
|
||||
for param in tool.get("parameters", []):
|
||||
# 严格验证参数格式是否为包含5个元素的元组
|
||||
assert isinstance(param, tuple) and len(param) == 5, "参数必须是包含5个元素的元组"
|
||||
assert isinstance(param, tuple), "参数必须是元组"
|
||||
assert len(param) == 5, "参数必须包含5个元素"
|
||||
builder.add_param(
|
||||
name=param[0],
|
||||
param_type=param[1],
|
||||
|
||||
Reference in New Issue
Block a user