大修LLMReq
This commit is contained in:
@@ -1,380 +0,0 @@
|
||||
import asyncio
|
||||
from typing import Callable, Any
|
||||
|
||||
from openai import AsyncStream
|
||||
from openai.types.chat import ChatCompletionChunk, ChatCompletion
|
||||
|
||||
from .base_client import BaseClient, APIResponse
|
||||
from src.config.api_ada_configs import (
|
||||
ModelInfo,
|
||||
ModelUsageArgConfigItem,
|
||||
RequestConfig,
|
||||
ModuleConfig,
|
||||
)
|
||||
from ..exceptions import (
|
||||
NetworkConnectionError,
|
||||
ReqAbortException,
|
||||
RespNotOkException,
|
||||
RespParseException,
|
||||
)
|
||||
from ..payload_content.message import Message
|
||||
from ..payload_content.resp_format import RespFormat
|
||||
from ..payload_content.tool_option import ToolOption
|
||||
from ..utils import compress_messages
|
||||
from src.common.logger import get_logger
|
||||
|
||||
logger = get_logger("模型客户端")
|
||||
|
||||
|
||||
def _check_retry(
|
||||
remain_try: int,
|
||||
retry_interval: int,
|
||||
can_retry_msg: str,
|
||||
cannot_retry_msg: str,
|
||||
can_retry_callable: Callable | None = None,
|
||||
**kwargs,
|
||||
) -> tuple[int, Any | None]:
|
||||
"""
|
||||
辅助函数:检查是否可以重试
|
||||
:param remain_try: 剩余尝试次数
|
||||
:param retry_interval: 重试间隔
|
||||
:param can_retry_msg: 可以重试时的提示信息
|
||||
:param cannot_retry_msg: 不可以重试时的提示信息
|
||||
:return: (等待间隔(如果为0则不等待,为-1则不再请求该模型), 新的消息列表(适用于压缩消息))
|
||||
"""
|
||||
if remain_try > 0:
|
||||
# 还有重试机会
|
||||
logger.warning(f"{can_retry_msg}")
|
||||
if can_retry_callable is not None:
|
||||
return retry_interval, can_retry_callable(**kwargs)
|
||||
else:
|
||||
return retry_interval, None
|
||||
else:
|
||||
# 达到最大重试次数
|
||||
logger.warning(f"{cannot_retry_msg}")
|
||||
return -1, None # 不再重试请求该模型
|
||||
|
||||
|
||||
def _handle_resp_not_ok(
|
||||
e: RespNotOkException,
|
||||
task_name: str,
|
||||
model_name: str,
|
||||
remain_try: int,
|
||||
retry_interval: int = 10,
|
||||
messages: tuple[list[Message], bool] | None = None,
|
||||
):
|
||||
"""
|
||||
处理响应错误异常
|
||||
:param e: 异常对象
|
||||
:param task_name: 任务名称
|
||||
:param model_name: 模型名称
|
||||
:param remain_try: 剩余尝试次数
|
||||
:param retry_interval: 重试间隔
|
||||
:param messages: (消息列表, 是否已压缩过)
|
||||
:return: (等待间隔(如果为0则不等待,为-1则不再请求该模型), 新的消息列表(适用于压缩消息))
|
||||
"""
|
||||
# 响应错误
|
||||
if e.status_code in [401, 403]:
|
||||
# API Key认证错误 - 让多API Key机制处理,给一次重试机会
|
||||
if remain_try > 0:
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"API Key认证失败(错误代码-{e.status_code}),多API Key机制会自动切换"
|
||||
)
|
||||
return 0, None # 立即重试,让底层客户端切换API Key
|
||||
else:
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"所有API Key都认证失败,错误代码-{e.status_code},错误信息-{e.message}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
elif e.status_code in [400, 402, 404]:
|
||||
# 其他客户端错误(不应该重试)
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"请求失败,错误代码-{e.status_code},错误信息-{e.message}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
elif e.status_code == 413:
|
||||
if messages and not messages[1]:
|
||||
# 消息列表不为空且未压缩,尝试压缩消息
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
0,
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求体过大,尝试压缩消息后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求体过大,压缩消息后仍然过大,放弃请求"
|
||||
),
|
||||
can_retry_callable=compress_messages,
|
||||
messages=messages[0],
|
||||
)
|
||||
# 没有消息可压缩
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求体过大,无法压缩消息,放弃请求。"
|
||||
)
|
||||
return -1, None
|
||||
elif e.status_code == 429:
|
||||
# 请求过于频繁 - 让多API Key机制处理,适当延迟后重试
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
min(retry_interval, 5), # 限制最大延迟为5秒,让API Key切换更快生效
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"请求过于频繁,多API Key机制会自动切换,{min(retry_interval, 5)}秒后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求过于频繁,所有API Key都被限制,放弃请求"
|
||||
),
|
||||
)
|
||||
elif e.status_code >= 500:
|
||||
# 服务器错误
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
retry_interval,
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"服务器错误,将于{retry_interval}秒后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"服务器错误,超过最大重试次数,请稍后再试"
|
||||
),
|
||||
)
|
||||
else:
|
||||
# 未知错误
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"未知错误,错误代码-{e.status_code},错误信息-{e.message}"
|
||||
)
|
||||
return -1, None
|
||||
|
||||
|
||||
def default_exception_handler(
|
||||
e: Exception,
|
||||
task_name: str,
|
||||
model_name: str,
|
||||
remain_try: int,
|
||||
retry_interval: int = 10,
|
||||
messages: tuple[list[Message], bool] | None = None,
|
||||
) -> tuple[int, list[Message] | None]:
|
||||
"""
|
||||
默认异常处理函数
|
||||
:param e: 异常对象
|
||||
:param task_name: 任务名称
|
||||
:param model_name: 模型名称
|
||||
:param remain_try: 剩余尝试次数
|
||||
:param retry_interval: 重试间隔
|
||||
:param messages: (消息列表, 是否已压缩过)
|
||||
:return (等待间隔(如果为0则不等待,为-1则不再请求该模型), 新的消息列表(适用于压缩消息))
|
||||
"""
|
||||
|
||||
if isinstance(e, NetworkConnectionError): # 网络连接错误
|
||||
# 网络错误可能是某个API Key的端点问题,给多API Key机制一次快速重试机会
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
min(retry_interval, 3), # 网络错误时减少等待时间,让API Key切换更快
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"连接异常,多API Key机制会尝试其他Key,{min(retry_interval, 3)}秒后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"连接异常,超过最大重试次数,请检查网络连接状态或URL是否正确"
|
||||
),
|
||||
)
|
||||
elif isinstance(e, ReqAbortException):
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n请求被中断,详细信息-{str(e.message)}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
elif isinstance(e, RespNotOkException):
|
||||
return _handle_resp_not_ok(
|
||||
e,
|
||||
task_name,
|
||||
model_name,
|
||||
remain_try,
|
||||
retry_interval,
|
||||
messages,
|
||||
)
|
||||
elif isinstance(e, RespParseException):
|
||||
# 响应解析错误
|
||||
logger.error(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"响应解析错误,错误信息-{e.message}\n"
|
||||
)
|
||||
logger.debug(f"附加内容:\n{str(e.ext_info)}")
|
||||
return -1, None # 不再重试请求该模型
|
||||
else:
|
||||
logger.error(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n未知异常,错误信息-{str(e)}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
|
||||
|
||||
class ModelRequestHandler:
|
||||
"""
|
||||
模型请求处理器
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
task_name: str,
|
||||
config: ModuleConfig,
|
||||
api_client_map: dict[str, BaseClient],
|
||||
):
|
||||
self.task_name: str = task_name
|
||||
"""任务名称"""
|
||||
|
||||
self.client_map: dict[str, BaseClient] = {}
|
||||
"""API客户端列表"""
|
||||
|
||||
self.configs: list[tuple[ModelInfo, ModelUsageArgConfigItem]] = []
|
||||
"""模型参数配置"""
|
||||
|
||||
self.req_conf: RequestConfig = config.req_conf
|
||||
"""请求配置"""
|
||||
|
||||
# 获取模型与使用配置
|
||||
for model_usage in config.task_model_arg_map[task_name].usage:
|
||||
if model_usage.name not in config.models:
|
||||
logger.error(f"Model '{model_usage.name}' not found in ModelManager")
|
||||
raise KeyError(f"Model '{model_usage.name}' not found in ModelManager")
|
||||
model_info = config.models[model_usage.name]
|
||||
|
||||
if model_info.api_provider not in self.client_map:
|
||||
# 缓存API客户端
|
||||
self.client_map[model_info.api_provider] = api_client_map[
|
||||
model_info.api_provider
|
||||
]
|
||||
|
||||
self.configs.append((model_info, model_usage)) # 添加模型与使用配置
|
||||
|
||||
async def get_response(
|
||||
self,
|
||||
messages: list[Message],
|
||||
tool_options: list[ToolOption] | None = None,
|
||||
response_format: RespFormat | None = None, # 暂不启用
|
||||
stream_response_handler: Callable[
|
||||
[AsyncStream[ChatCompletionChunk], asyncio.Event | None], APIResponse
|
||||
]
|
||||
| None = None,
|
||||
async_response_parser: Callable[[ChatCompletion], APIResponse] | None = None,
|
||||
interrupt_flag: asyncio.Event | None = None,
|
||||
) -> APIResponse:
|
||||
"""
|
||||
获取对话响应
|
||||
:param messages: 消息列表
|
||||
:param tool_options: 工具选项列表
|
||||
:param response_format: 响应格式
|
||||
:param stream_response_handler: 流式响应处理函数(可选)
|
||||
:param async_response_parser: 响应解析函数(可选)
|
||||
:param interrupt_flag: 中断信号量(可选,默认为None)
|
||||
:return: APIResponse
|
||||
"""
|
||||
# 遍历可用模型,若获取响应失败,则使用下一个模型继续请求
|
||||
for config_item in self.configs:
|
||||
client = self.client_map[config_item[0].api_provider]
|
||||
model_info: ModelInfo = config_item[0]
|
||||
model_usage_config: ModelUsageArgConfigItem = config_item[1]
|
||||
|
||||
remain_try = (
|
||||
model_usage_config.max_retry or self.req_conf.max_retry
|
||||
) + 1 # 初始化:剩余尝试次数 = 最大重试次数 + 1
|
||||
|
||||
compressed_messages = None
|
||||
retry_interval = self.req_conf.retry_interval
|
||||
while remain_try > 0:
|
||||
try:
|
||||
return await client.get_response(
|
||||
model_info,
|
||||
message_list=(compressed_messages or messages),
|
||||
tool_options=tool_options,
|
||||
max_tokens=model_usage_config.max_tokens
|
||||
or self.req_conf.default_max_tokens,
|
||||
temperature=model_usage_config.temperature
|
||||
or self.req_conf.default_temperature,
|
||||
response_format=response_format,
|
||||
stream_response_handler=stream_response_handler,
|
||||
async_response_parser=async_response_parser,
|
||||
interrupt_flag=interrupt_flag,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(e)
|
||||
remain_try -= 1 # 剩余尝试次数减1
|
||||
|
||||
# 处理异常
|
||||
handle_res = default_exception_handler(
|
||||
e,
|
||||
self.task_name,
|
||||
model_info.name,
|
||||
remain_try,
|
||||
retry_interval=self.req_conf.retry_interval,
|
||||
messages=(messages, compressed_messages is not None),
|
||||
)
|
||||
|
||||
if handle_res[0] == -1:
|
||||
# 等待间隔为-1,表示不再请求该模型
|
||||
remain_try = 0
|
||||
elif handle_res[0] != 0:
|
||||
# 等待间隔不为0,表示需要等待
|
||||
await asyncio.sleep(handle_res[0])
|
||||
retry_interval *= 2
|
||||
|
||||
if handle_res[1] is not None:
|
||||
# 压缩消息
|
||||
compressed_messages = handle_res[1]
|
||||
|
||||
logger.error(f"任务-'{self.task_name}' 请求执行失败,所有模型均不可用")
|
||||
raise RuntimeError("请求失败,所有模型均不可用") # 所有请求尝试均失败
|
||||
|
||||
async def get_embedding(
|
||||
self,
|
||||
embedding_input: str,
|
||||
) -> APIResponse:
|
||||
"""
|
||||
获取嵌入向量
|
||||
:param embedding_input: 嵌入输入
|
||||
:return: APIResponse
|
||||
"""
|
||||
for config in self.configs:
|
||||
client = self.client_map[config[0].api_provider]
|
||||
model_info: ModelInfo = config[0]
|
||||
model_usage_config: ModelUsageArgConfigItem = config[1]
|
||||
remain_try = (
|
||||
model_usage_config.max_retry or self.req_conf.max_retry
|
||||
) + 1 # 初始化:剩余尝试次数 = 最大重试次数 + 1
|
||||
|
||||
while remain_try:
|
||||
try:
|
||||
return await client.get_embedding(
|
||||
model_info=model_info,
|
||||
embedding_input=embedding_input,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(e)
|
||||
remain_try -= 1 # 剩余尝试次数减1
|
||||
|
||||
# 处理异常
|
||||
handle_res = default_exception_handler(
|
||||
e,
|
||||
self.task_name,
|
||||
model_info.name,
|
||||
remain_try,
|
||||
retry_interval=self.req_conf.retry_interval,
|
||||
)
|
||||
|
||||
if handle_res[0] == -1:
|
||||
# 等待间隔为-1,表示不再请求该模型
|
||||
remain_try = 0
|
||||
elif handle_res[0] != 0:
|
||||
# 等待间隔不为0,表示需要等待
|
||||
await asyncio.sleep(handle_res[0])
|
||||
|
||||
logger.error(f"任务-'{self.task_name}' 请求执行失败,所有模型均不可用")
|
||||
raise RuntimeError("请求失败,所有模型均不可用") # 所有请求尝试均失败
|
||||
|
||||
380
src/llm_models/model_client/__init__bak.py
Normal file
380
src/llm_models/model_client/__init__bak.py
Normal file
@@ -0,0 +1,380 @@
|
||||
import asyncio
|
||||
from typing import Callable, Any
|
||||
|
||||
from openai import AsyncStream
|
||||
from openai.types.chat import ChatCompletionChunk, ChatCompletion
|
||||
|
||||
from .base_client import BaseClient, APIResponse
|
||||
from src.config.api_ada_configs import (
|
||||
ModelInfo,
|
||||
ModelUsageArgConfigItem,
|
||||
RequestConfig,
|
||||
ModuleConfig,
|
||||
)
|
||||
from ..exceptions import (
|
||||
NetworkConnectionError,
|
||||
ReqAbortException,
|
||||
RespNotOkException,
|
||||
RespParseException,
|
||||
)
|
||||
from ..payload_content.message import Message
|
||||
from ..payload_content.resp_format import RespFormat
|
||||
from ..payload_content.tool_option import ToolOption
|
||||
from ..utils import compress_messages
|
||||
from src.common.logger import get_logger
|
||||
|
||||
logger = get_logger("模型客户端")
|
||||
|
||||
|
||||
def _check_retry(
|
||||
remain_try: int,
|
||||
retry_interval: int,
|
||||
can_retry_msg: str,
|
||||
cannot_retry_msg: str,
|
||||
can_retry_callable: Callable | None = None,
|
||||
**kwargs,
|
||||
) -> tuple[int, Any | None]:
|
||||
"""
|
||||
辅助函数:检查是否可以重试
|
||||
:param remain_try: 剩余尝试次数
|
||||
:param retry_interval: 重试间隔
|
||||
:param can_retry_msg: 可以重试时的提示信息
|
||||
:param cannot_retry_msg: 不可以重试时的提示信息
|
||||
:return: (等待间隔(如果为0则不等待,为-1则不再请求该模型), 新的消息列表(适用于压缩消息))
|
||||
"""
|
||||
if remain_try > 0:
|
||||
# 还有重试机会
|
||||
logger.warning(f"{can_retry_msg}")
|
||||
if can_retry_callable is not None:
|
||||
return retry_interval, can_retry_callable(**kwargs)
|
||||
else:
|
||||
return retry_interval, None
|
||||
else:
|
||||
# 达到最大重试次数
|
||||
logger.warning(f"{cannot_retry_msg}")
|
||||
return -1, None # 不再重试请求该模型
|
||||
|
||||
|
||||
def _handle_resp_not_ok(
|
||||
e: RespNotOkException,
|
||||
task_name: str,
|
||||
model_name: str,
|
||||
remain_try: int,
|
||||
retry_interval: int = 10,
|
||||
messages: tuple[list[Message], bool] | None = None,
|
||||
):
|
||||
"""
|
||||
处理响应错误异常
|
||||
:param e: 异常对象
|
||||
:param task_name: 任务名称
|
||||
:param model_name: 模型名称
|
||||
:param remain_try: 剩余尝试次数
|
||||
:param retry_interval: 重试间隔
|
||||
:param messages: (消息列表, 是否已压缩过)
|
||||
:return: (等待间隔(如果为0则不等待,为-1则不再请求该模型), 新的消息列表(适用于压缩消息))
|
||||
"""
|
||||
# 响应错误
|
||||
if e.status_code in [401, 403]:
|
||||
# API Key认证错误 - 让多API Key机制处理,给一次重试机会
|
||||
if remain_try > 0:
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"API Key认证失败(错误代码-{e.status_code}),多API Key机制会自动切换"
|
||||
)
|
||||
return 0, None # 立即重试,让底层客户端切换API Key
|
||||
else:
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"所有API Key都认证失败,错误代码-{e.status_code},错误信息-{e.message}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
elif e.status_code in [400, 402, 404]:
|
||||
# 其他客户端错误(不应该重试)
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"请求失败,错误代码-{e.status_code},错误信息-{e.message}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
elif e.status_code == 413:
|
||||
if messages and not messages[1]:
|
||||
# 消息列表不为空且未压缩,尝试压缩消息
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
0,
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求体过大,尝试压缩消息后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求体过大,压缩消息后仍然过大,放弃请求"
|
||||
),
|
||||
can_retry_callable=compress_messages,
|
||||
messages=messages[0],
|
||||
)
|
||||
# 没有消息可压缩
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求体过大,无法压缩消息,放弃请求。"
|
||||
)
|
||||
return -1, None
|
||||
elif e.status_code == 429:
|
||||
# 请求过于频繁 - 让多API Key机制处理,适当延迟后重试
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
min(retry_interval, 5), # 限制最大延迟为5秒,让API Key切换更快生效
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"请求过于频繁,多API Key机制会自动切换,{min(retry_interval, 5)}秒后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"请求过于频繁,所有API Key都被限制,放弃请求"
|
||||
),
|
||||
)
|
||||
elif e.status_code >= 500:
|
||||
# 服务器错误
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
retry_interval,
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"服务器错误,将于{retry_interval}秒后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
"服务器错误,超过最大重试次数,请稍后再试"
|
||||
),
|
||||
)
|
||||
else:
|
||||
# 未知错误
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"未知错误,错误代码-{e.status_code},错误信息-{e.message}"
|
||||
)
|
||||
return -1, None
|
||||
|
||||
|
||||
def default_exception_handler(
|
||||
e: Exception,
|
||||
task_name: str,
|
||||
model_name: str,
|
||||
remain_try: int,
|
||||
retry_interval: int = 10,
|
||||
messages: tuple[list[Message], bool] | None = None,
|
||||
) -> tuple[int, list[Message] | None]:
|
||||
"""
|
||||
默认异常处理函数
|
||||
:param e: 异常对象
|
||||
:param task_name: 任务名称
|
||||
:param model_name: 模型名称
|
||||
:param remain_try: 剩余尝试次数
|
||||
:param retry_interval: 重试间隔
|
||||
:param messages: (消息列表, 是否已压缩过)
|
||||
:return (等待间隔(如果为0则不等待,为-1则不再请求该模型), 新的消息列表(适用于压缩消息))
|
||||
"""
|
||||
|
||||
if isinstance(e, NetworkConnectionError): # 网络连接错误
|
||||
# 网络错误可能是某个API Key的端点问题,给多API Key机制一次快速重试机会
|
||||
return _check_retry(
|
||||
remain_try,
|
||||
min(retry_interval, 3), # 网络错误时减少等待时间,让API Key切换更快
|
||||
can_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"连接异常,多API Key机制会尝试其他Key,{min(retry_interval, 3)}秒后重试"
|
||||
),
|
||||
cannot_retry_msg=(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"连接异常,超过最大重试次数,请检查网络连接状态或URL是否正确"
|
||||
),
|
||||
)
|
||||
elif isinstance(e, ReqAbortException):
|
||||
logger.warning(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n请求被中断,详细信息-{str(e.message)}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
elif isinstance(e, RespNotOkException):
|
||||
return _handle_resp_not_ok(
|
||||
e,
|
||||
task_name,
|
||||
model_name,
|
||||
remain_try,
|
||||
retry_interval,
|
||||
messages,
|
||||
)
|
||||
elif isinstance(e, RespParseException):
|
||||
# 响应解析错误
|
||||
logger.error(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n"
|
||||
f"响应解析错误,错误信息-{e.message}\n"
|
||||
)
|
||||
logger.debug(f"附加内容:\n{str(e.ext_info)}")
|
||||
return -1, None # 不再重试请求该模型
|
||||
else:
|
||||
logger.error(
|
||||
f"任务-'{task_name}' 模型-'{model_name}'\n未知异常,错误信息-{str(e)}"
|
||||
)
|
||||
return -1, None # 不再重试请求该模型
|
||||
|
||||
|
||||
class ModelRequestHandler:
|
||||
"""
|
||||
模型请求处理器
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
task_name: str,
|
||||
config: ModuleConfig,
|
||||
api_client_map: dict[str, BaseClient],
|
||||
):
|
||||
self.task_name: str = task_name
|
||||
"""任务名称"""
|
||||
|
||||
self.client_map: dict[str, BaseClient] = {}
|
||||
"""API客户端列表"""
|
||||
|
||||
self.configs: list[tuple[ModelInfo, ModelUsageArgConfigItem]] = []
|
||||
"""模型参数配置"""
|
||||
|
||||
self.req_conf: RequestConfig = config.req_conf
|
||||
"""请求配置"""
|
||||
|
||||
# 获取模型与使用配置
|
||||
for model_usage in config.task_model_arg_map[task_name].usage:
|
||||
if model_usage.name not in config.models:
|
||||
logger.error(f"Model '{model_usage.name}' not found in ModelManager")
|
||||
raise KeyError(f"Model '{model_usage.name}' not found in ModelManager")
|
||||
model_info = config.models[model_usage.name]
|
||||
|
||||
if model_info.api_provider not in self.client_map:
|
||||
# 缓存API客户端
|
||||
self.client_map[model_info.api_provider] = api_client_map[
|
||||
model_info.api_provider
|
||||
]
|
||||
|
||||
self.configs.append((model_info, model_usage)) # 添加模型与使用配置
|
||||
|
||||
async def get_response(
|
||||
self,
|
||||
messages: list[Message],
|
||||
tool_options: list[ToolOption] | None = None,
|
||||
response_format: RespFormat | None = None, # 暂不启用
|
||||
stream_response_handler: Callable[
|
||||
[AsyncStream[ChatCompletionChunk], asyncio.Event | None], APIResponse
|
||||
]
|
||||
| None = None,
|
||||
async_response_parser: Callable[[ChatCompletion], APIResponse] | None = None,
|
||||
interrupt_flag: asyncio.Event | None = None,
|
||||
) -> APIResponse:
|
||||
"""
|
||||
获取对话响应
|
||||
:param messages: 消息列表
|
||||
:param tool_options: 工具选项列表
|
||||
:param response_format: 响应格式
|
||||
:param stream_response_handler: 流式响应处理函数(可选)
|
||||
:param async_response_parser: 响应解析函数(可选)
|
||||
:param interrupt_flag: 中断信号量(可选,默认为None)
|
||||
:return: APIResponse
|
||||
"""
|
||||
# 遍历可用模型,若获取响应失败,则使用下一个模型继续请求
|
||||
for config_item in self.configs:
|
||||
client = self.client_map[config_item[0].api_provider]
|
||||
model_info: ModelInfo = config_item[0]
|
||||
model_usage_config: ModelUsageArgConfigItem = config_item[1]
|
||||
|
||||
remain_try = (
|
||||
model_usage_config.max_retry or self.req_conf.max_retry
|
||||
) + 1 # 初始化:剩余尝试次数 = 最大重试次数 + 1
|
||||
|
||||
compressed_messages = None
|
||||
retry_interval = self.req_conf.retry_interval
|
||||
while remain_try > 0:
|
||||
try:
|
||||
return await client.get_response(
|
||||
model_info,
|
||||
message_list=(compressed_messages or messages),
|
||||
tool_options=tool_options,
|
||||
max_tokens=model_usage_config.max_tokens
|
||||
or self.req_conf.default_max_tokens,
|
||||
temperature=model_usage_config.temperature
|
||||
or self.req_conf.default_temperature,
|
||||
response_format=response_format,
|
||||
stream_response_handler=stream_response_handler,
|
||||
async_response_parser=async_response_parser,
|
||||
interrupt_flag=interrupt_flag,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(e)
|
||||
remain_try -= 1 # 剩余尝试次数减1
|
||||
|
||||
# 处理异常
|
||||
handle_res = default_exception_handler(
|
||||
e,
|
||||
self.task_name,
|
||||
model_info.name,
|
||||
remain_try,
|
||||
retry_interval=self.req_conf.retry_interval,
|
||||
messages=(messages, compressed_messages is not None),
|
||||
)
|
||||
|
||||
if handle_res[0] == -1:
|
||||
# 等待间隔为-1,表示不再请求该模型
|
||||
remain_try = 0
|
||||
elif handle_res[0] != 0:
|
||||
# 等待间隔不为0,表示需要等待
|
||||
await asyncio.sleep(handle_res[0])
|
||||
retry_interval *= 2
|
||||
|
||||
if handle_res[1] is not None:
|
||||
# 压缩消息
|
||||
compressed_messages = handle_res[1]
|
||||
|
||||
logger.error(f"任务-'{self.task_name}' 请求执行失败,所有模型均不可用")
|
||||
raise RuntimeError("请求失败,所有模型均不可用") # 所有请求尝试均失败
|
||||
|
||||
async def get_embedding(
|
||||
self,
|
||||
embedding_input: str,
|
||||
) -> APIResponse:
|
||||
"""
|
||||
获取嵌入向量
|
||||
:param embedding_input: 嵌入输入
|
||||
:return: APIResponse
|
||||
"""
|
||||
for config in self.configs:
|
||||
client = self.client_map[config[0].api_provider]
|
||||
model_info: ModelInfo = config[0]
|
||||
model_usage_config: ModelUsageArgConfigItem = config[1]
|
||||
remain_try = (
|
||||
model_usage_config.max_retry or self.req_conf.max_retry
|
||||
) + 1 # 初始化:剩余尝试次数 = 最大重试次数 + 1
|
||||
|
||||
while remain_try:
|
||||
try:
|
||||
return await client.get_embedding(
|
||||
model_info=model_info,
|
||||
embedding_input=embedding_input,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(e)
|
||||
remain_try -= 1 # 剩余尝试次数减1
|
||||
|
||||
# 处理异常
|
||||
handle_res = default_exception_handler(
|
||||
e,
|
||||
self.task_name,
|
||||
model_info.name,
|
||||
remain_try,
|
||||
retry_interval=self.req_conf.retry_interval,
|
||||
)
|
||||
|
||||
if handle_res[0] == -1:
|
||||
# 等待间隔为-1,表示不再请求该模型
|
||||
remain_try = 0
|
||||
elif handle_res[0] != 0:
|
||||
# 等待间隔不为0,表示需要等待
|
||||
await asyncio.sleep(handle_res[0])
|
||||
|
||||
logger.error(f"任务-'{self.task_name}' 请求执行失败,所有模型均不可用")
|
||||
raise RuntimeError("请求失败,所有模型均不可用") # 所有请求尝试均失败
|
||||
@@ -81,10 +81,7 @@ class BaseClient:
|
||||
tuple[APIResponse, tuple[int, int, int]],
|
||||
]
|
||||
| None = None,
|
||||
async_response_parser: Callable[
|
||||
[ChatCompletion], tuple[APIResponse, tuple[int, int, int]]
|
||||
]
|
||||
| None = None,
|
||||
async_response_parser: Callable[[ChatCompletion], tuple[APIResponse, tuple[int, int, int]]] | None = None,
|
||||
interrupt_flag: asyncio.Event | None = None,
|
||||
) -> APIResponse:
|
||||
"""
|
||||
@@ -114,3 +111,37 @@ class BaseClient:
|
||||
:return: 嵌入响应
|
||||
"""
|
||||
raise RuntimeError("This method should be overridden in subclasses")
|
||||
|
||||
|
||||
class ClientRegistry:
|
||||
def __init__(self) -> None:
|
||||
self.client_registry: dict[str, type[BaseClient]] = {}
|
||||
|
||||
def register_client_class(self, client_type: str):
|
||||
"""
|
||||
注册API客户端类
|
||||
:param client_class: API客户端类
|
||||
"""
|
||||
|
||||
def decorator(cls: type[BaseClient]) -> type[BaseClient]:
|
||||
if not issubclass(cls, BaseClient):
|
||||
raise TypeError(f"{cls.__name__} is not a subclass of BaseClient")
|
||||
self.client_registry[client_type] = cls
|
||||
return cls
|
||||
|
||||
return decorator
|
||||
|
||||
def get_client_class(self, client_type: str) -> type[BaseClient]:
|
||||
"""
|
||||
获取注册的API客户端类
|
||||
Args:
|
||||
client_type: 客户端类型
|
||||
Returns:
|
||||
type[BaseClient]: 注册的API客户端类
|
||||
"""
|
||||
if client_type not in self.client_registry:
|
||||
raise KeyError(f"'{client_type}' 类型的 Client 未注册")
|
||||
return self.client_registry[client_type]
|
||||
|
||||
|
||||
client_registry = ClientRegistry()
|
||||
|
||||
@@ -22,7 +22,7 @@ from openai.types.chat.chat_completion_chunk import ChoiceDelta
|
||||
|
||||
from .base_client import APIResponse, UsageRecord
|
||||
from src.config.api_ada_configs import ModelInfo, APIProvider
|
||||
from . import BaseClient
|
||||
from .base_client import BaseClient, client_registry
|
||||
from src.common.logger import get_logger
|
||||
|
||||
from ..exceptions import (
|
||||
@@ -63,9 +63,7 @@ def _convert_messages(messages: list[Message]) -> list[ChatCompletionMessagePara
|
||||
content.append(
|
||||
{
|
||||
"type": "image_url",
|
||||
"image_url": {
|
||||
"url": f"data:image/{item[0].lower()};base64,{item[1]}"
|
||||
},
|
||||
"image_url": {"url": f"data:image/{item[0].lower()};base64,{item[1]}"},
|
||||
}
|
||||
)
|
||||
elif isinstance(item, str):
|
||||
@@ -120,13 +118,8 @@ def _convert_tool_options(tool_options: list[ToolOption]) -> list[dict[str, Any]
|
||||
if tool_option.params:
|
||||
ret["parameters"] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
param.name: _convert_tool_param(param)
|
||||
for param in tool_option.params
|
||||
},
|
||||
"required": [
|
||||
param.name for param in tool_option.params if param.required
|
||||
],
|
||||
"properties": {param.name: _convert_tool_param(param) for param in tool_option.params},
|
||||
"required": [param.name for param in tool_option.params if param.required],
|
||||
}
|
||||
return ret
|
||||
|
||||
@@ -190,9 +183,7 @@ def _process_delta(
|
||||
|
||||
if tool_call_delta.function.arguments:
|
||||
# 如果有工具调用参数,则添加到对应的工具调用的参数串缓冲区中
|
||||
tool_calls_buffer[tool_call_delta.index][2].write(
|
||||
tool_call_delta.function.arguments
|
||||
)
|
||||
tool_calls_buffer[tool_call_delta.index][2].write(tool_call_delta.function.arguments)
|
||||
|
||||
return in_rc_flag
|
||||
|
||||
@@ -225,14 +216,12 @@ def _build_stream_api_resp(
|
||||
if not isinstance(arguments, dict):
|
||||
raise RespParseException(
|
||||
None,
|
||||
"响应解析失败,工具调用参数无法解析为字典类型。工具调用参数原始响应:\n"
|
||||
f"{raw_arg_data}",
|
||||
f"响应解析失败,工具调用参数无法解析为字典类型。工具调用参数原始响应:\n{raw_arg_data}",
|
||||
)
|
||||
except json.JSONDecodeError as e:
|
||||
raise RespParseException(
|
||||
None,
|
||||
"响应解析失败,无法解析工具调用参数。工具调用参数原始响应:"
|
||||
f"{raw_arg_data}",
|
||||
f"响应解析失败,无法解析工具调用参数。工具调用参数原始响应:{raw_arg_data}",
|
||||
) from e
|
||||
else:
|
||||
arguments_buffer.close()
|
||||
@@ -257,9 +246,7 @@ async def _default_stream_response_handler(
|
||||
_in_rc_flag = False # 标记是否在推理内容块中
|
||||
_rc_delta_buffer = io.StringIO() # 推理内容缓冲区,用于存储接收到的推理内容
|
||||
_fc_delta_buffer = io.StringIO() # 正式内容缓冲区,用于存储接收到的正式内容
|
||||
_tool_calls_buffer: list[
|
||||
tuple[str, str, io.StringIO]
|
||||
] = [] # 工具调用缓冲区,用于存储接收到的工具调用
|
||||
_tool_calls_buffer: list[tuple[str, str, io.StringIO]] = [] # 工具调用缓冲区,用于存储接收到的工具调用
|
||||
_usage_record = None # 使用情况记录
|
||||
|
||||
def _insure_buffer_closed():
|
||||
@@ -280,7 +267,7 @@ async def _default_stream_response_handler(
|
||||
|
||||
delta = event.choices[0].delta # 获取当前块的delta内容
|
||||
|
||||
if hasattr(delta, "reasoning_content") and delta.reasoning_content:
|
||||
if hasattr(delta, "reasoning_content") and delta.reasoning_content: # type: ignore
|
||||
# 标记:有独立的推理内容块
|
||||
_has_rc_attr_flag = True
|
||||
|
||||
@@ -334,10 +321,10 @@ def _default_normal_response_parser(
|
||||
raise RespParseException(resp, "响应解析失败,缺失choices字段")
|
||||
message_part = resp.choices[0].message
|
||||
|
||||
if hasattr(message_part, "reasoning_content") and message_part.reasoning_content:
|
||||
if hasattr(message_part, "reasoning_content") and message_part.reasoning_content: # type: ignore
|
||||
# 有有效的推理字段
|
||||
api_response.content = message_part.content
|
||||
api_response.reasoning_content = message_part.reasoning_content
|
||||
api_response.reasoning_content = message_part.reasoning_content # type: ignore
|
||||
elif message_part.content:
|
||||
# 提取推理和内容
|
||||
match = pattern.match(message_part.content)
|
||||
@@ -358,16 +345,10 @@ def _default_normal_response_parser(
|
||||
try:
|
||||
arguments = json.loads(call.function.arguments)
|
||||
if not isinstance(arguments, dict):
|
||||
raise RespParseException(
|
||||
resp, "响应解析失败,工具调用参数无法解析为字典类型"
|
||||
)
|
||||
api_response.tool_calls.append(
|
||||
ToolCall(call.id, call.function.name, arguments)
|
||||
)
|
||||
raise RespParseException(resp, "响应解析失败,工具调用参数无法解析为字典类型")
|
||||
api_response.tool_calls.append(ToolCall(call.id, call.function.name, arguments))
|
||||
except json.JSONDecodeError as e:
|
||||
raise RespParseException(
|
||||
resp, "响应解析失败,无法解析工具调用参数"
|
||||
) from e
|
||||
raise RespParseException(resp, "响应解析失败,无法解析工具调用参数") from e
|
||||
|
||||
# 提取Usage信息
|
||||
if resp.usage:
|
||||
@@ -385,63 +366,15 @@ def _default_normal_response_parser(
|
||||
return api_response, _usage_record
|
||||
|
||||
|
||||
@client_registry.register_client_class("openai")
|
||||
class OpenaiClient(BaseClient):
|
||||
def __init__(self, api_provider: APIProvider):
|
||||
super().__init__(api_provider)
|
||||
# 不再在初始化时创建固定的client,而是在请求时动态创建
|
||||
self._clients_cache = {} # API Key -> AsyncOpenAI client 的缓存
|
||||
|
||||
def _get_client(self, api_key: str = None) -> AsyncOpenAI:
|
||||
"""获取或创建对应API Key的客户端"""
|
||||
if api_key is None:
|
||||
api_key = self.api_provider.get_current_api_key()
|
||||
|
||||
if not api_key:
|
||||
raise ValueError(f"API Provider '{self.api_provider.name}' 没有可用的API Key")
|
||||
|
||||
# 使用缓存避免重复创建客户端
|
||||
if api_key not in self._clients_cache:
|
||||
self._clients_cache[api_key] = AsyncOpenAI(
|
||||
base_url=self.api_provider.base_url,
|
||||
api_key=api_key,
|
||||
max_retries=0,
|
||||
)
|
||||
|
||||
return self._clients_cache[api_key]
|
||||
|
||||
async def _execute_with_fallback(self, func, *args, **kwargs):
|
||||
"""执行请求并在失败时切换API Key"""
|
||||
current_api_key = self.api_provider.get_current_api_key()
|
||||
max_attempts = len(self.api_provider.api_keys) if self.api_provider.api_keys else 1
|
||||
|
||||
for attempt in range(max_attempts):
|
||||
try:
|
||||
client = self._get_client(current_api_key)
|
||||
result = await func(client, *args, **kwargs)
|
||||
# 成功时重置失败计数
|
||||
self.api_provider.reset_key_failures(current_api_key)
|
||||
return result
|
||||
|
||||
except (APIStatusError, APIConnectionError) as e:
|
||||
# 记录失败并尝试下一个API Key
|
||||
logger.warning(f"API Key失败 (尝试 {attempt + 1}/{max_attempts}): {str(e)}")
|
||||
|
||||
if attempt < max_attempts - 1: # 还有重试机会
|
||||
next_api_key = self.api_provider.mark_key_failed(current_api_key)
|
||||
if next_api_key and next_api_key != current_api_key:
|
||||
current_api_key = next_api_key
|
||||
logger.info(f"切换到下一个API Key: {current_api_key[:8]}***{current_api_key[-4:]}")
|
||||
continue
|
||||
|
||||
# 所有API Key都失败了,重新抛出异常
|
||||
if isinstance(e, APIStatusError):
|
||||
raise RespNotOkException(e.status_code, e.message) from e
|
||||
elif isinstance(e, APIConnectionError):
|
||||
raise NetworkConnectionError(str(e)) from e
|
||||
|
||||
except Exception as e:
|
||||
# 其他异常直接抛出
|
||||
raise e
|
||||
self.client: AsyncOpenAI = AsyncOpenAI(
|
||||
base_url=api_provider.base_url,
|
||||
api_key=api_provider.api_key,
|
||||
max_retries=0,
|
||||
)
|
||||
|
||||
async def get_response(
|
||||
self,
|
||||
@@ -456,10 +389,7 @@ class OpenaiClient(BaseClient):
|
||||
tuple[APIResponse, tuple[int, int, int]],
|
||||
]
|
||||
| None = None,
|
||||
async_response_parser: Callable[
|
||||
[ChatCompletion], tuple[APIResponse, tuple[int, int, int]]
|
||||
]
|
||||
| None = None,
|
||||
async_response_parser: Callable[[ChatCompletion], tuple[APIResponse, tuple[int, int, int]]] | None = None,
|
||||
interrupt_flag: asyncio.Event | None = None,
|
||||
) -> APIResponse:
|
||||
"""
|
||||
@@ -475,40 +405,6 @@ class OpenaiClient(BaseClient):
|
||||
:param interrupt_flag: 中断信号量(可选,默认为None)
|
||||
:return: (响应文本, 推理文本, 工具调用, 其他数据)
|
||||
"""
|
||||
return await self._execute_with_fallback(
|
||||
self._get_response_internal,
|
||||
model_info,
|
||||
message_list,
|
||||
tool_options,
|
||||
max_tokens,
|
||||
temperature,
|
||||
response_format,
|
||||
stream_response_handler,
|
||||
async_response_parser,
|
||||
interrupt_flag,
|
||||
)
|
||||
|
||||
async def _get_response_internal(
|
||||
self,
|
||||
client: AsyncOpenAI,
|
||||
model_info: ModelInfo,
|
||||
message_list: list[Message],
|
||||
tool_options: list[ToolOption] | None = None,
|
||||
max_tokens: int = 1024,
|
||||
temperature: float = 0.7,
|
||||
response_format: RespFormat | None = None,
|
||||
stream_response_handler: Callable[
|
||||
[AsyncStream[ChatCompletionChunk], asyncio.Event | None],
|
||||
tuple[APIResponse, tuple[int, int, int]],
|
||||
]
|
||||
| None = None,
|
||||
async_response_parser: Callable[
|
||||
[ChatCompletion], tuple[APIResponse, tuple[int, int, int]]
|
||||
]
|
||||
| None = None,
|
||||
interrupt_flag: asyncio.Event | None = None,
|
||||
) -> APIResponse:
|
||||
"""内部方法:执行实际的API调用"""
|
||||
if stream_response_handler is None:
|
||||
stream_response_handler = _default_stream_response_handler
|
||||
|
||||
@@ -518,23 +414,19 @@ class OpenaiClient(BaseClient):
|
||||
# 将messages构造为OpenAI API所需的格式
|
||||
messages: Iterable[ChatCompletionMessageParam] = _convert_messages(message_list)
|
||||
# 将tool_options转换为OpenAI API所需的格式
|
||||
tools: Iterable[ChatCompletionToolParam] = (
|
||||
_convert_tool_options(tool_options) if tool_options else NOT_GIVEN
|
||||
)
|
||||
tools: Iterable[ChatCompletionToolParam] = _convert_tool_options(tool_options) if tool_options else NOT_GIVEN
|
||||
|
||||
try:
|
||||
if model_info.force_stream_mode:
|
||||
req_task = asyncio.create_task(
|
||||
client.chat.completions.create(
|
||||
self.client.chat.completions.create(
|
||||
model=model_info.model_identifier,
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
temperature=temperature,
|
||||
max_tokens=max_tokens,
|
||||
stream=True,
|
||||
response_format=response_format.to_dict()
|
||||
if response_format
|
||||
else NOT_GIVEN,
|
||||
response_format=response_format.to_dict() if response_format else NOT_GIVEN,
|
||||
)
|
||||
)
|
||||
while not req_task.done():
|
||||
@@ -544,22 +436,18 @@ class OpenaiClient(BaseClient):
|
||||
raise ReqAbortException("请求被外部信号中断")
|
||||
await asyncio.sleep(0.1) # 等待0.1秒后再次检查任务&中断信号量状态
|
||||
|
||||
resp, usage_record = await stream_response_handler(
|
||||
req_task.result(), interrupt_flag
|
||||
)
|
||||
resp, usage_record = await stream_response_handler(req_task.result(), interrupt_flag)
|
||||
else:
|
||||
# 发送请求并获取响应
|
||||
req_task = asyncio.create_task(
|
||||
client.chat.completions.create(
|
||||
self.client.chat.completions.create(
|
||||
model=model_info.model_identifier,
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
temperature=temperature,
|
||||
max_tokens=max_tokens,
|
||||
stream=False,
|
||||
response_format=response_format.to_dict()
|
||||
if response_format
|
||||
else NOT_GIVEN,
|
||||
response_format=response_format.to_dict() if response_format else NOT_GIVEN,
|
||||
)
|
||||
)
|
||||
while not req_task.done():
|
||||
@@ -599,21 +487,8 @@ class OpenaiClient(BaseClient):
|
||||
:param embedding_input: 嵌入输入文本
|
||||
:return: 嵌入响应
|
||||
"""
|
||||
return await self._execute_with_fallback(
|
||||
self._get_embedding_internal,
|
||||
model_info,
|
||||
embedding_input,
|
||||
)
|
||||
|
||||
async def _get_embedding_internal(
|
||||
self,
|
||||
client: AsyncOpenAI,
|
||||
model_info: ModelInfo,
|
||||
embedding_input: str,
|
||||
) -> APIResponse:
|
||||
"""内部方法:执行实际的嵌入API调用"""
|
||||
try:
|
||||
raw_response = await client.embeddings.create(
|
||||
raw_response = await self.client.embeddings.create(
|
||||
model=model_info.model_identifier,
|
||||
input=embedding_input,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user