refactor: 将多个方法修改为静态方法以提高代码可读性和一致性

This commit is contained in:
春河晴
2025-04-17 15:39:49 +09:00
parent 73da67fce8
commit dc96e26ca5
37 changed files with 248 additions and 174 deletions

View File

@@ -23,7 +23,7 @@ class ChangeMoodTool(BaseTool):
"required": ["text", "response_set"],
}
async def execute(self, function_args: Dict[str, Any], message_txt: str) -> Dict[str, Any]:
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> Dict[str, Any]:
"""执行心情改变
Args:

View File

@@ -1,4 +1,6 @@
# from src.plugins.person_info.relationship_manager import relationship_manager
from typing import Dict, Any
from src.common.logger import get_module_logger
from src.do_tool.tool_can_use.base_tool import BaseTool
# from src.plugins.chat_module.think_flow_chat.think_flow_generator import ResponseGenerator
@@ -20,11 +22,11 @@ class RelationshipTool(BaseTool):
"required": ["text", "changed_value", "reason"],
}
async def execute(self, args: dict, message_txt: str) -> dict:
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> dict:
"""执行工具功能
Args:
args: 包含工具参数的字典
function_args: 包含工具参数的字典
text: 原始消息文本
changed_value: 变更值
reason: 变更原因
@@ -33,9 +35,9 @@ class RelationshipTool(BaseTool):
dict: 包含执行结果的字典
"""
try:
text = args.get("text")
changed_value = args.get("changed_value")
reason = args.get("reason")
text = function_args.get("text")
changed_value = function_args.get("changed_value")
reason = function_args.get("reason")
return {"content": f"因为你刚刚因为{reason},所以你和发[{text}]这条消息的人的关系值变化为{changed_value}"}

View File

@@ -49,8 +49,9 @@ class SearchKnowledgeTool(BaseTool):
logger.error(f"知识库搜索工具执行失败: {str(e)}")
return {"name": "search_knowledge", "content": f"知识库搜索失败: {str(e)}"}
@staticmethod
def get_info_from_db(
self, query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
) -> Union[str, list]:
"""从数据库中获取相关信息

View File

@@ -17,7 +17,7 @@ class SendEmojiTool(BaseTool):
"required": ["text"],
}
async def execute(self, function_args: Dict[str, Any], message_txt: str) -> Dict[str, Any]:
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> Dict[str, Any]:
text = function_args.get("text", message_txt)
return {
"name": "send_emoji",

View File

@@ -24,8 +24,9 @@ class ToolUser:
model=global_config.llm_tool_use, temperature=0.2, max_tokens=1000, request_type="tool_use"
)
@staticmethod
async def _build_tool_prompt(
self, message_txt: str, sender_name: str, chat_stream: ChatStream, subheartflow: SubHeartflow = None
message_txt: str, sender_name: str, chat_stream: ChatStream, subheartflow: SubHeartflow = None
):
"""构建工具使用的提示词
@@ -69,7 +70,8 @@ class ToolUser:
prompt += "你现在需要对群里的聊天内容进行回复,现在选择工具来对消息和你的回复进行处理,你是否需要额外的信息,比如回忆或者搜寻已有的知识,改变关系和情感,或者了解你现在正在做什么。"
return prompt
def _define_tools(self):
@staticmethod
def _define_tools():
"""获取所有已注册工具的定义
Returns:
@@ -77,7 +79,8 @@ class ToolUser:
"""
return get_all_tool_definitions()
async def _execute_tool_call(self, tool_call, message_txt: str):
@staticmethod
async def _execute_tool_call(tool_call, message_txt: str):
"""执行特定的工具调用
Args: