🤖 自动格式化代码 [skip ci]

This commit is contained in:
github-actions[bot]
2025-04-14 04:01:29 +00:00
parent 38d7d9e014
commit 9c82f1322e
7 changed files with 69 additions and 84 deletions

View File

@@ -18,12 +18,11 @@ class ChangeMoodTool(BaseTool):
"type": "object",
"properties": {
"text": {"type": "string", "description": "引起你改变心情的文本"},
"response_set": {"type": "list", "description": "你对文本的回复"}
"response_set": {"type": "list", "description": "你对文本的回复"},
},
"required": ["text", "response_set"],
}
async def execute(self, function_args: Dict[str, Any], message_txt: str) -> Dict[str, Any]:
"""执行心情改变

View File

@@ -6,28 +6,19 @@ from src.do_tool.tool_can_use.base_tool import BaseTool
logger = get_module_logger("relationship_tool")
class RelationshipTool(BaseTool):
name = "change_relationship"
description = "根据收到的文本和回复内容,修改与特定用户的关系值,当你回复了别人的消息,你可以使用这个工具"
parameters = {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "收到的文本"
"text": {"type": "string", "description": "收到的文本"},
"changed_value": {"type": "number", "description": "变更值"},
"reason": {"type": "string", "description": "变更原因"},
},
"changed_value": {
"type": "number",
"description": "变更值"
},
"reason": {
"type": "string",
"description": "变更原因"
"required": ["text", "changed_value", "reason"],
}
},
"required": ["text", "changed_value", "reason"]
}
async def execute(self, args: dict, message_txt: str) -> dict:
"""执行工具功能

View File

@@ -21,7 +21,9 @@ class ToolUser:
model=global_config.llm_heartflow, temperature=0.2, max_tokens=1000, request_type="tool_use"
)
async def _build_tool_prompt(self, message_txt: str, sender_name: str, chat_stream: ChatStream, reply_message:str = ""):
async def _build_tool_prompt(
self, message_txt: str, sender_name: str, chat_stream: ChatStream, reply_message: str = ""
):
"""构建工具使用的提示词
Args:
@@ -166,7 +168,7 @@ class ToolUser:
"knowledge": [],
"change_relationship": [],
"change_mood": [],
"other": []
"other": [],
}
# 执行所有工具调用
@@ -175,18 +177,12 @@ class ToolUser:
if result:
tool_results.append(result)
# 将工具结果添加到对应类型的列表中
structured_info[result["type"]].append({
"name": result["name"],
"content": result["content"]
})
structured_info[result["type"]].append({"name": result["name"], "content": result["content"]})
# 如果有工具结果,返回结构化的信息
if any(structured_info.values()):
logger.info(f"工具调用收集到结构化信息: {json.dumps(structured_info, ensure_ascii=False)}")
return {
"used_tools": True,
"structured_info": structured_info
}
return {"used_tools": True, "structured_info": structured_info}
else:
# 没有工具调用
content, reasoning_content = response

View File

@@ -97,8 +97,6 @@ class SubHeartflow:
self.bot_name = global_config.BOT_NICKNAME
def add_observation(self, observation: Observation):
"""添加一个新的observation对象到列表中如果已存在相同id的observation则不添加"""
# 查找是否存在相同id的observation
@@ -151,15 +149,15 @@ class SubHeartflow:
observation = self.observations[0]
await observation.observe()
async def do_thinking_before_reply(self, message_txt: str, sender_name: str, chat_stream: ChatStream, extra_info: str):
async def do_thinking_before_reply(
self, message_txt: str, sender_name: str, chat_stream: ChatStream, extra_info: str
):
current_thinking_info = self.current_mind
mood_info = self.current_state.mood
# mood_info = "你很生气,很愤怒"
observation = self.observations[0]
chat_observe_info = observation.observe_info
# 开始构建prompt
prompt_personality = f"你的名字是{self.bot_name},你"
# person

View File

@@ -136,8 +136,6 @@ class ThinkFlowChat:
message_manager.add_message(bot_message)
async def _update_relationship(self, message: MessageRecv, response_set):
"""更新关系情绪"""
ori_response = ",".join(response_set)
@@ -265,11 +263,12 @@ class ThinkFlowChat:
update_relationship = ""
try:
with Timer("思考前使用工具", timing_results):
tool_result = await self.tool_user.use_tool(message.processed_plain_text, message.message_info.user_info.user_nickname, chat)
tool_result = await self.tool_user.use_tool(
message.processed_plain_text, message.message_info.user_info.user_nickname, chat
)
# 如果工具被使用且获得了结果,将收集到的信息合并到思考中
collected_info = ""
if tool_result.get("used_tools", False):
# 如果有收集到的结构化信息,将其格式化后添加到当前思考中
if "structured_info" in tool_result:
info = tool_result["structured_info"]
@@ -315,17 +314,17 @@ class ThinkFlowChat:
logger.error(f"思考前工具调用失败: {e}")
logger.error(traceback.format_exc())
if update_relationship:
# ori_response = ",".join(response_set)
# print("22222222222222222222222222222")
stance, emotion = await self.gpt._get_emotion_tags_with_reason("你还没有回复", message.processed_plain_text,update_relationship)
stance, emotion = await self.gpt._get_emotion_tags_with_reason(
"你还没有回复", message.processed_plain_text, update_relationship
)
await relationship_manager.calculate_update_relationship_value(
chat_stream=message.chat_stream, label=emotion, stance=stance
)
print("33333333333333333333333333333")
# 思考前脑内状态
try:
with Timer("思考前脑内状态", timing_results):
@@ -335,7 +334,7 @@ class ThinkFlowChat:
message_txt=message.processed_plain_text,
sender_name=message.message_info.user_info.user_nickname,
chat_stream=chat,
extra_info=collected_info
extra_info=collected_info,
)
except Exception as e:
logger.error(f"心流思考前脑内状态失败: {e}")
@@ -370,15 +369,15 @@ class ThinkFlowChat:
except Exception as e:
logger.error(f"心流处理表情包失败: {e}")
# 思考后使用工具
try:
with Timer("思考后使用工具", timing_results):
tool_result = await self.tool_user.use_tool(message.processed_plain_text, message.message_info.user_info.user_nickname, chat)
tool_result = await self.tool_user.use_tool(
message.processed_plain_text, message.message_info.user_info.user_nickname, chat
)
# 如果工具被使用且获得了结果,将收集到的信息合并到思考中
collected_info = ""
if tool_result.get("used_tools", False):
# 如果有收集到的结构化信息,将其格式化后添加到当前思考中
if "structured_info" in tool_result:
info = tool_result["structured_info"]
@@ -424,13 +423,13 @@ class ThinkFlowChat:
# 更新关系
if info["change_relationship"]:
ori_response = ",".join(response_set)
stance, emotion = await self.gpt._get_emotion_tags(ori_response, message.processed_plain_text,info["change_relationship"]["content"])
stance, emotion = await self.gpt._get_emotion_tags(
ori_response, message.processed_plain_text, info["change_relationship"]["content"]
)
await relationship_manager.calculate_update_relationship_value(
chat_stream=message.chat_stream, label=emotion, stance=stance
)
try:
with Timer("思考后脑内状态更新", timing_results):
stream_id = message.chat_stream.stream_id
@@ -440,11 +439,12 @@ class ThinkFlowChat:
stream_id, limit=global_config.MAX_CONTEXT_SIZE, combine=True
)
await heartflow.get_subheartflow(stream_id).do_thinking_after_reply(response_set, chat_talking_prompt,collected_info)
await heartflow.get_subheartflow(stream_id).do_thinking_after_reply(
response_set, chat_talking_prompt, collected_info
)
except Exception as e:
logger.error(f"心流思考后脑内状态更新失败: {e}")
# 回复后处理
await willing_manager.after_generate_reply_handle(message.message_info.message_id)

View File

@@ -226,7 +226,6 @@ class ResponseGenerator:
logger.debug(f"获取情感标签时出错: {e}")
return "中立", "平静" # 出错时返回默认值
async def _get_emotion_tags_with_reason(self, content: str, processed_plain_text: str, reason: str):
"""提取情感标签,结合立场和情绪"""
try:

View File

@@ -162,7 +162,9 @@ class RelationshipManager:
return chat_stream.user_info.user_nickname, value, relationship_level[level_num]
async def calculate_update_relationship_value_with_reason(self, chat_stream: ChatStream, label: str, stance: str, reason: str) -> tuple:
async def calculate_update_relationship_value_with_reason(
self, chat_stream: ChatStream, label: str, stance: str, reason: str
) -> tuple:
"""计算并变更关系值
新的关系值变更计算方式:
将关系值限定在-1000到1000