加上tools的enum属性
This commit is contained in:
@@ -192,17 +192,6 @@ def _build_stream_api_resp(
|
||||
return resp
|
||||
|
||||
|
||||
async def _to_async_iterable(iterable: Iterable[T]) -> AsyncIterator[T]:
|
||||
"""
|
||||
将迭代器转换为异步迭代器
|
||||
:param iterable: 迭代器对象
|
||||
:return: 异步迭代器对象
|
||||
"""
|
||||
for item in iterable:
|
||||
await asyncio.sleep(0)
|
||||
yield item
|
||||
|
||||
|
||||
async def _default_stream_response_handler(
|
||||
resp_stream: AsyncIterator[GenerateContentResponse],
|
||||
interrupt_flag: asyncio.Event | None,
|
||||
|
||||
@@ -94,16 +94,19 @@ def _convert_tool_options(tool_options: list[ToolOption]) -> list[dict[str, Any]
|
||||
:return: 转换后的工具选项列表
|
||||
"""
|
||||
|
||||
def _convert_tool_param(tool_option_param: ToolParam) -> dict[str, str]:
|
||||
def _convert_tool_param(tool_option_param: ToolParam) -> dict[str, Any]:
|
||||
"""
|
||||
转换单个工具参数格式
|
||||
:param tool_option_param: 工具参数对象
|
||||
:return: 转换后的工具参数字典
|
||||
"""
|
||||
return {
|
||||
return_dict: dict[str, Any] = {
|
||||
"type": tool_option_param.param_type.value,
|
||||
"description": tool_option_param.description,
|
||||
}
|
||||
if tool_option_param.enum_values:
|
||||
return_dict["enum"] = tool_option_param.enum_values
|
||||
return return_dict
|
||||
|
||||
def _convert_tool_option_item(tool_option: ToolOption) -> dict[str, Any]:
|
||||
"""
|
||||
|
||||
@@ -6,10 +6,10 @@ class ToolParamType(Enum):
|
||||
工具调用参数类型
|
||||
"""
|
||||
|
||||
String = "string" # 字符串
|
||||
Int = "integer" # 整型
|
||||
Float = "float" # 浮点型
|
||||
Boolean = "bool" # 布尔型
|
||||
STRING = "string" # 字符串
|
||||
INTEGER = "integer" # 整型
|
||||
FLOAT = "float" # 浮点型
|
||||
BOOLEAN = "bool" # 布尔型
|
||||
|
||||
|
||||
class ToolParam:
|
||||
@@ -18,7 +18,12 @@ class ToolParam:
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, name: str, param_type: ToolParamType, description: str, required: bool
|
||||
self,
|
||||
name: str,
|
||||
param_type: ToolParamType,
|
||||
description: str,
|
||||
required: bool,
|
||||
enum_values: list[str] | None = None,
|
||||
):
|
||||
"""
|
||||
初始化工具调用参数
|
||||
@@ -32,6 +37,7 @@ class ToolParam:
|
||||
self.param_type: ToolParamType = param_type
|
||||
self.description: str = description
|
||||
self.required: bool = required
|
||||
self.enum_values: list[str] | None = enum_values
|
||||
|
||||
|
||||
class ToolOption:
|
||||
@@ -95,6 +101,7 @@ class ToolOptionBuilder:
|
||||
param_type: ToolParamType,
|
||||
description: str,
|
||||
required: bool = False,
|
||||
enum_values: list[str] | None = None,
|
||||
) -> "ToolOptionBuilder":
|
||||
"""
|
||||
添加工具参数
|
||||
@@ -113,6 +120,7 @@ class ToolOptionBuilder:
|
||||
param_type=param_type,
|
||||
description=description,
|
||||
required=required,
|
||||
enum_values=enum_values,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -77,7 +77,9 @@ class LLMRequest:
|
||||
# 请求体构建
|
||||
message_builder = MessageBuilder()
|
||||
message_builder.add_text_content(prompt)
|
||||
message_builder.add_image_content(image_base64=image_base64, image_format=image_format, support_formats=client.get_support_image_formats())
|
||||
message_builder.add_image_content(
|
||||
image_base64=image_base64, image_format=image_format, support_formats=client.get_support_image_formats()
|
||||
)
|
||||
messages = [message_builder.build()]
|
||||
|
||||
# 请求并处理返回值
|
||||
@@ -458,6 +460,7 @@ class LLMRequest:
|
||||
return -1, None
|
||||
|
||||
def _build_tool_options(self, tools: Optional[List[Dict[str, Any]]]) -> Optional[List[ToolOption]]:
|
||||
# sourcery skip: extract-method
|
||||
"""构建工具选项列表"""
|
||||
if not tools:
|
||||
return None
|
||||
@@ -467,18 +470,25 @@ class LLMRequest:
|
||||
tool_options_builder = ToolOptionBuilder()
|
||||
tool_options_builder.set_name(tool.get("name", ""))
|
||||
tool_options_builder.set_description(tool.get("description", ""))
|
||||
parameters: List[Tuple[str, str, str, bool]] = tool.get("parameters", [])
|
||||
parameters: List[Tuple[str, str, str, bool, List[str] | None]] = tool.get("parameters", [])
|
||||
for param in parameters:
|
||||
try:
|
||||
assert isinstance(param, tuple) and len(param) == 5, "参数必须是包含5个元素的元组"
|
||||
assert isinstance(param[0], str), "参数名称必须是字符串"
|
||||
assert isinstance(param[1], ToolParamType), "参数类型必须是ToolParamType枚举"
|
||||
assert isinstance(param[2], str), "参数描述必须是字符串"
|
||||
assert isinstance(param[3], bool), "参数是否必填必须是布尔值"
|
||||
assert isinstance(param[4], list) or param[4] is None, "参数枚举值必须是列表或None"
|
||||
tool_options_builder.add_param(
|
||||
name=param[0],
|
||||
param_type=ToolParamType(param[1]),
|
||||
param_type=param[1],
|
||||
description=param[2],
|
||||
required=param[3],
|
||||
enum_values=param[4],
|
||||
)
|
||||
except ValueError as ve:
|
||||
except AssertionError as ae:
|
||||
tool_legal = False
|
||||
logger.error(f"{param[1]} 参数类型错误: {str(ve)}")
|
||||
logger.error(f"{param[0]} 参数定义错误: {str(ae)}")
|
||||
except Exception as e:
|
||||
tool_legal = False
|
||||
logger.error(f"构建工具参数失败: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user