feat:add tool
This commit is contained in:
59
src/do_tool/tool_can_use/compare_numbers_tool.py
Normal file
59
src/do_tool/tool_can_use/compare_numbers_tool.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from src.do_tool.tool_can_use.base_tool import BaseTool, register_tool
|
||||
from src.common.logger import get_module_logger
|
||||
from typing import Dict, Any
|
||||
|
||||
logger = get_module_logger("compare_numbers_tool")
|
||||
|
||||
class CompareNumbersTool(BaseTool):
|
||||
"""比较两个数大小的工具"""
|
||||
name = "compare_numbers"
|
||||
description = "比较两个数的大小,返回较大的数"
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"num1": {
|
||||
"type": "number",
|
||||
"description": "第一个数字"
|
||||
},
|
||||
"num2": {
|
||||
"type": "number",
|
||||
"description": "第二个数字"
|
||||
}
|
||||
},
|
||||
"required": ["num1", "num2"]
|
||||
}
|
||||
|
||||
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> Dict[str, Any]:
|
||||
"""执行比较两个数的大小
|
||||
|
||||
Args:
|
||||
function_args: 工具参数
|
||||
message_txt: 原始消息文本
|
||||
|
||||
Returns:
|
||||
Dict: 工具执行结果
|
||||
"""
|
||||
try:
|
||||
num1 = function_args.get("num1")
|
||||
num2 = function_args.get("num2")
|
||||
|
||||
if num1 > num2:
|
||||
result = f"{num1} 大于 {num2}"
|
||||
elif num1 < num2:
|
||||
result = f"{num1} 小于 {num2}"
|
||||
else:
|
||||
result = f"{num1} 等于 {num2}"
|
||||
|
||||
return {
|
||||
"name": self.name,
|
||||
"content": result
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"比较数字失败: {str(e)}")
|
||||
return {
|
||||
"name": self.name,
|
||||
"content": f"比较数字失败: {str(e)}"
|
||||
}
|
||||
|
||||
# 注册工具
|
||||
register_tool(CompareNumbersTool)
|
||||
@@ -43,7 +43,7 @@ class ToolUser:
|
||||
prompt += "你正在思考如何回复群里的消息。\n"
|
||||
prompt += f"你注意到{sender_name}刚刚说:{message_txt}\n"
|
||||
prompt += f"注意你就是{bot_name},{bot_name}指的就是你。"
|
||||
prompt += "你现在需要对群里的聊天内容进行回复,现在请你思考,你是否需要额外的信息,或者一些工具来帮你回复,比如回忆或者搜寻已有的知识,或者了解你现在正在做什么,请输出你需要的工具,或者你需要的额外信息。"
|
||||
prompt += "你现在需要对群里的聊天内容进行回复,现在请你思考,你是否需要额外的信息,或者一些工具来帮你回复,不要使用危险功能(比如文件操作或者系统操作爬虫),比如回忆或者搜寻已有的知识,或者了解你现在正在做什么,请输出你需要的工具,或者你需要的额外信息。"
|
||||
|
||||
return prompt
|
||||
|
||||
@@ -106,6 +106,7 @@ class ToolUser:
|
||||
|
||||
# 定义可用工具
|
||||
tools = self._define_tools()
|
||||
print(tools)
|
||||
|
||||
# 使用llm_model_tool发送带工具定义的请求
|
||||
payload = {
|
||||
|
||||
Reference in New Issue
Block a user