better:优化工作记忆,给reply action加入额外信息
This commit is contained in:
@@ -137,7 +137,8 @@ class ActionPlanner(BasePlanner):
|
||||
self_info = info.get_processed_info()
|
||||
elif isinstance(info, StructuredInfo):
|
||||
structured_info = info.get_processed_info()
|
||||
# print(f"structured_info: {structured_info}")
|
||||
else:
|
||||
extra_info.append(info.get_processed_info())
|
||||
# elif not isinstance(info, ActionInfo): # 跳过已处理的ActionInfo
|
||||
# extra_info.append(info.get_processed_info())
|
||||
|
||||
@@ -218,6 +219,15 @@ class ActionPlanner(BasePlanner):
|
||||
action_data[key] = value
|
||||
|
||||
action_data["identity"] = self_info
|
||||
|
||||
extra_info_block = "\n".join(extra_info)
|
||||
extra_info_block += f"\n{structured_info}"
|
||||
if extra_info or structured_info:
|
||||
extra_info_block = f"以下是一些额外的信息,现在请你阅读以下内容,进行决策\n{extra_info_block}\n以上是一些额外的信息,现在请你阅读以下内容,进行决策"
|
||||
else:
|
||||
extra_info_block = ""
|
||||
|
||||
action_data["extra_info_block"] = extra_info_block
|
||||
|
||||
# 对于reply动作不需要额外处理,因为相关字段已经在上面的循环中添加到action_data
|
||||
|
||||
@@ -257,9 +267,13 @@ class ActionPlanner(BasePlanner):
|
||||
)
|
||||
|
||||
action_result = {"action_type": action, "action_data": action_data, "reasoning": reasoning}
|
||||
|
||||
|
||||
|
||||
|
||||
plan_result = {
|
||||
"action_result": action_result,
|
||||
# "extra_info_block": extra_info_block,
|
||||
"current_mind": current_mind,
|
||||
"observed_messages": observed_messages,
|
||||
"action_prompt": prompt,
|
||||
@@ -288,7 +302,7 @@ class ActionPlanner(BasePlanner):
|
||||
if running_memorys:
|
||||
memory_str = "以下是当前在聊天中,你回忆起的记忆:\n"
|
||||
for running_memory in running_memorys:
|
||||
memory_str += f"{running_memory['topic']}: {running_memory['content']}\n"
|
||||
memory_str += f"{running_memory['content']}\n"
|
||||
|
||||
chat_context_description = "你现在正在一个群聊中"
|
||||
chat_target_name = None # Only relevant for private
|
||||
|
||||
@@ -30,8 +30,7 @@ logger = get_logger("expressor")
|
||||
def init_prompt():
|
||||
Prompt(
|
||||
"""
|
||||
你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中:
|
||||
{style_habbits}
|
||||
{extra_info_block}
|
||||
|
||||
{time_block}
|
||||
你现在正在群里聊天,以下是群里正在进行的聊天内容:
|
||||
@@ -42,6 +41,8 @@ def init_prompt():
|
||||
{chat_target}
|
||||
{identity},在这聊天中,"{target_message}"引起了你的注意,你想要在群里发言或者回复这条消息。
|
||||
你需要使用合适的语法和句法,参考聊天内容,组织一条日常且口语化的回复。注意不要复读你说过的话。
|
||||
你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中:
|
||||
{style_habbits}
|
||||
请你根据情景使用以下句法:
|
||||
{grammar_habbits}
|
||||
{config_expression_style},请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容。
|
||||
@@ -55,8 +56,7 @@ def init_prompt():
|
||||
|
||||
Prompt(
|
||||
"""
|
||||
你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中:
|
||||
{style_habbits}
|
||||
{extra_info_block}
|
||||
|
||||
{time_block}
|
||||
你现在正在聊天,以下是你和对方正在进行的聊天内容:
|
||||
@@ -67,8 +67,10 @@ def init_prompt():
|
||||
{chat_target}
|
||||
{identity},在这聊天中,"{target_message}"引起了你的注意,你想要发言或者回复这条消息。
|
||||
你需要使用合适的语法和句法,参考聊天内容,组织一条日常且口语化的回复。注意不要复读你说过的话。
|
||||
请你根据情景使用以下句法:
|
||||
你可以参考以下的语言习惯和句法,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中:
|
||||
{style_habbits}
|
||||
{grammar_habbits}
|
||||
|
||||
{config_expression_style},请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容。
|
||||
{keywords_reaction_prompt}
|
||||
请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。
|
||||
@@ -267,6 +269,7 @@ class DefaultReplyer:
|
||||
|
||||
target_message = action_data.get("target", "")
|
||||
identity = action_data.get("identity", "")
|
||||
extra_info_block = action_data.get("extra_info_block", "")
|
||||
|
||||
# 3. 构建 Prompt
|
||||
with Timer("构建Prompt", {}): # 内部计时器,可选保留
|
||||
@@ -274,6 +277,7 @@ class DefaultReplyer:
|
||||
chat_stream=self.chat_stream, # Pass the stream object
|
||||
# in_mind_reply=in_mind_reply,
|
||||
identity=identity,
|
||||
extra_info_block=extra_info_block,
|
||||
reason=reason,
|
||||
sender_name=sender_name_for_prompt, # Pass determined name
|
||||
target_message=target_message,
|
||||
@@ -334,6 +338,7 @@ class DefaultReplyer:
|
||||
chat_stream,
|
||||
sender_name,
|
||||
# in_mind_reply,
|
||||
extra_info_block,
|
||||
identity,
|
||||
target_message,
|
||||
config_expression_style,
|
||||
@@ -429,6 +434,7 @@ class DefaultReplyer:
|
||||
grammar_habbits=grammar_habbits_str,
|
||||
chat_target=chat_target_1,
|
||||
chat_info=chat_talking_prompt,
|
||||
extra_info_block=extra_info_block,
|
||||
time_block=time_block,
|
||||
# bot_name=global_config.bot.nickname,
|
||||
# prompt_personality="",
|
||||
@@ -448,6 +454,7 @@ class DefaultReplyer:
|
||||
grammar_habbits=grammar_habbits_str,
|
||||
chat_target=chat_target_1,
|
||||
chat_info=chat_talking_prompt,
|
||||
extra_info_block=extra_info_block,
|
||||
time_block=time_block,
|
||||
# bot_name=global_config.bot.nickname,
|
||||
# prompt_personality="",
|
||||
|
||||
@@ -226,25 +226,24 @@ class MemoryManager:
|
||||
"""
|
||||
prompt = f"""请对以下内容进行总结,总结成记忆,输出两部分:
|
||||
1. 记忆内容主题(精简,20字以内):让用户可以一眼看出记忆内容是什么
|
||||
2. key_points:多条,包含关键的概念、事件,每条都要包含解释或描述,谁在什么时候干了什么
|
||||
2. content:一到三条,包含关键的概念、事件,每条都要包含解释或描述,谁在什么时候干了什么
|
||||
|
||||
内容:
|
||||
{content}
|
||||
|
||||
请按以下JSON格式输出:
|
||||
{{
|
||||
"brief": "记忆内容主题(20字以内)",
|
||||
"key_points": [
|
||||
"要点1:解释或描述",
|
||||
"要点2:解释或描述",
|
||||
...
|
||||
"brief": "记忆内容主题",
|
||||
"content": [
|
||||
"内容",
|
||||
"内容"
|
||||
]
|
||||
}}
|
||||
请确保输出是有效的JSON格式,不要添加任何额外的说明或解释。
|
||||
"""
|
||||
default_summary = {
|
||||
"brief": "主题未知的记忆",
|
||||
"key_points": ["未知的要点"],
|
||||
"content": ["未知的要点"],
|
||||
}
|
||||
|
||||
try:
|
||||
@@ -277,13 +276,13 @@ class MemoryManager:
|
||||
json_result["brief"] = "主题未知的记忆"
|
||||
|
||||
# 处理关键要点
|
||||
if "key_points" not in json_result or not isinstance(json_result["key_points"], list):
|
||||
json_result["key_points"] = ["未知的要点"]
|
||||
if "content" not in json_result or not isinstance(json_result["content"], list):
|
||||
json_result["content"] = ["未知的要点"]
|
||||
else:
|
||||
# 确保key_points中的每个项目都是字符串
|
||||
json_result["key_points"] = [str(point) for point in json_result["key_points"] if point is not None]
|
||||
if not json_result["key_points"]:
|
||||
json_result["key_points"] = ["未知的要点"]
|
||||
# 确保content中的每个项目都是字符串
|
||||
json_result["content"] = [str(point) for point in json_result["content"] if point is not None]
|
||||
if not json_result["content"]:
|
||||
json_result["content"] = ["未知的要点"]
|
||||
|
||||
return json_result
|
||||
|
||||
@@ -328,13 +327,13 @@ class MemoryManager:
|
||||
目前主题:{summary["brief"]}
|
||||
|
||||
目前关键要点:
|
||||
{chr(10).join([f"- {point}" for point in summary.get("key_points", [])])}
|
||||
{chr(10).join([f"- {point}" for point in summary.get("content", [])])}
|
||||
|
||||
请生成修改后的主题和关键要点,遵循以下格式:
|
||||
```json
|
||||
{{
|
||||
"brief": "修改后的主题(20字以内)",
|
||||
"key_points": [
|
||||
"content": [
|
||||
"修改后的要点1:解释或描述",
|
||||
"修改后的要点2:解释或描述"
|
||||
]
|
||||
@@ -345,7 +344,7 @@ class MemoryManager:
|
||||
# 定义默认的精简结果
|
||||
default_refined = {
|
||||
"brief": summary["brief"],
|
||||
"key_points": summary.get("key_points", ["未知的要点"])[:1], # 默认只保留第一个要点
|
||||
"content": summary.get("content", ["未知的要点"])[:1], # 默认只保留第一个要点
|
||||
}
|
||||
|
||||
try:
|
||||
@@ -377,13 +376,13 @@ class MemoryManager:
|
||||
summary["brief"] = refined_data.get("brief", "主题未知的记忆")
|
||||
|
||||
# 更新关键要点
|
||||
key_points = refined_data.get("key_points", [])
|
||||
if isinstance(key_points, list) and key_points:
|
||||
content = refined_data.get("content", [])
|
||||
if isinstance(content, list) and content:
|
||||
# 确保所有要点都是字符串
|
||||
summary["key_points"] = [str(point) for point in key_points if point is not None]
|
||||
summary["content"] = [str(point) for point in content if point is not None]
|
||||
else:
|
||||
# 如果key_points不是列表或为空,使用默认值
|
||||
summary["key_points"] = ["主要要点已遗忘"]
|
||||
# 如果content不是列表或为空,使用默认值
|
||||
summary["content"] = ["主要要点已遗忘"]
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"精简记忆出错: {str(e)}")
|
||||
@@ -391,7 +390,7 @@ class MemoryManager:
|
||||
|
||||
# 出错时使用简化的默认精简
|
||||
summary["brief"] = summary["brief"] + " (已简化)"
|
||||
summary["key_points"] = summary.get("key_points", ["未知的要点"])[:1]
|
||||
summary["content"] = summary.get("content", ["未知的要点"])[:1]
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"精简记忆调用LLM出错: {str(e)}")
|
||||
@@ -510,11 +509,11 @@ class MemoryManager:
|
||||
# 如果有摘要信息,添加到提示中
|
||||
if summary1:
|
||||
prompt += f"记忆1主题:{summary1['brief']}\n"
|
||||
prompt += "记忆1关键要点:\n" + "\n".join([f"- {point}" for point in summary1.get("key_points", [])]) + "\n\n"
|
||||
prompt += "记忆1关键要点:\n" + "\n".join([f"- {point}" for point in summary1.get("content", [])]) + "\n\n"
|
||||
|
||||
if summary2:
|
||||
prompt += f"记忆2主题:{summary2['brief']}\n"
|
||||
prompt += "记忆2关键要点:\n" + "\n".join([f"- {point}" for point in summary2.get("key_points", [])]) + "\n\n"
|
||||
prompt += "记忆2关键要点:\n" + "\n".join([f"- {point}" for point in summary2.get("content", [])]) + "\n\n"
|
||||
|
||||
# 添加记忆原始内容
|
||||
prompt += f"""
|
||||
@@ -529,7 +528,7 @@ class MemoryManager:
|
||||
{{
|
||||
"content": "合并后的记忆内容文本(尽可能保留原信息,但去除重复)",
|
||||
"brief": "合并后的主题(20字以内)",
|
||||
"key_points": [
|
||||
"content": [
|
||||
"合并后的要点1:解释或描述",
|
||||
"合并后的要点2:解释或描述",
|
||||
"合并后的要点3:解释或描述"
|
||||
@@ -543,18 +542,18 @@ class MemoryManager:
|
||||
default_merged = {
|
||||
"content": f"{content1}\n\n{content2}",
|
||||
"brief": f"合并:{summary1['brief']} + {summary2['brief']}",
|
||||
"key_points": [],
|
||||
"content": [],
|
||||
}
|
||||
|
||||
# 合并key_points
|
||||
if "key_points" in summary1:
|
||||
default_merged["key_points"].extend(summary1["key_points"])
|
||||
if "key_points" in summary2:
|
||||
default_merged["key_points"].extend(summary2["key_points"])
|
||||
# 合并content
|
||||
if "content" in summary1:
|
||||
default_merged["content"].extend(summary1["content"])
|
||||
if "content" in summary2:
|
||||
default_merged["content"].extend(summary2["content"])
|
||||
|
||||
# 确保列表不为空
|
||||
if not default_merged["key_points"]:
|
||||
default_merged["key_points"] = ["合并的要点"]
|
||||
if not default_merged["content"]:
|
||||
default_merged["content"] = ["合并的要点"]
|
||||
|
||||
try:
|
||||
# 调用LLM合并记忆
|
||||
@@ -589,13 +588,13 @@ class MemoryManager:
|
||||
merged_data["brief"] = default_merged["brief"]
|
||||
|
||||
# 处理关键要点
|
||||
if "key_points" not in merged_data or not isinstance(merged_data["key_points"], list):
|
||||
merged_data["key_points"] = default_merged["key_points"]
|
||||
if "content" not in merged_data or not isinstance(merged_data["content"], list):
|
||||
merged_data["content"] = default_merged["content"]
|
||||
else:
|
||||
# 确保key_points中的每个项目都是字符串
|
||||
merged_data["key_points"] = [str(point) for point in merged_data["key_points"] if point is not None]
|
||||
if not merged_data["key_points"]:
|
||||
merged_data["key_points"] = ["合并的要点"]
|
||||
# 确保content中的每个项目都是字符串
|
||||
merged_data["content"] = [str(point) for point in merged_data["content"] if point is not None]
|
||||
if not merged_data["content"]:
|
||||
merged_data["content"] = ["合并的要点"]
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"合并记忆时处理JSON出错: {str(e)}")
|
||||
@@ -623,7 +622,7 @@ class MemoryManager:
|
||||
# 设置合并后的摘要
|
||||
summary = {
|
||||
"brief": merged_data["brief"],
|
||||
"key_points": merged_data["key_points"],
|
||||
"content": merged_data["content"],
|
||||
}
|
||||
merged_memory.set_summary(summary)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user