refine agent loop and working memory tools
This commit is contained in:
@@ -73,5 +73,9 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
|
|||||||
datastr = json.dumps(outcome.data, ensure_ascii=False, default=json_default) if type(outcome.data) in [dict, list] else str(outcome.data)
|
datastr = json.dumps(outcome.data, ensure_ascii=False, default=json_default) if type(outcome.data) in [dict, list] else str(outcome.data)
|
||||||
next_prompt += f"<tool_result>\n{datastr}\n</tool_result>\n\n"
|
next_prompt += f"<tool_result>\n{datastr}\n</tool_result>\n\n"
|
||||||
next_prompt += outcome.next_prompt
|
next_prompt += outcome.next_prompt
|
||||||
|
if (turn+1) % 5 == 0:
|
||||||
|
next_prompt += f"\n\n[DANGER] 已连续执行第 {turn+1} 轮。禁止无效重试。若无有效进展,必须切换策略:1. 探测物理边界 2. 请求用户协助。"
|
||||||
|
if turn == 23:
|
||||||
|
next_prompt += f"\n\n[DANGER] 已连续执行第 {turn+1} 轮。你必须总结情况进行ask_user,不允许继续重试。"
|
||||||
messages = [{"role": "user", "content": next_prompt}]
|
messages = [{"role": "user", "content": next_prompt}]
|
||||||
return {'result': 'MAX_TURNS_EXCEEDED'}
|
return {'result': 'MAX_TURNS_EXCEEDED'}
|
||||||
@@ -47,11 +47,11 @@
|
|||||||
"save_to_file": {"type": "string", "description": "可选。将 JS 执行结果(js_return)保存到的文件路径。注意:该功能不支持 await 等异步结果。"}}, "required": ["script"]}
|
"save_to_file": {"type": "string", "description": "可选。将 JS 执行结果(js_return)保存到的文件路径。注意:该功能不支持 await 等异步结果。"}}, "required": ["script"]}
|
||||||
}},
|
}},
|
||||||
{"type": "function", "function": {
|
{"type": "function", "function": {
|
||||||
"name": "update_sop_plan",
|
"name": "update_working_mem",
|
||||||
"description": "【在读取SOP后需要调用】准备开始执行此SOP。提取容易遗忘但关键的内容,生成极简检查清单,100tokens以内,后续每轮自动注入context。特别是靠后的内容。",
|
"description": "更新当前任务的工作记忆。当任务目标细化、阶段切换或发现关键物理约束(坑)时调用,内容将作为物理锚点在后续每轮自动注入,强行锁定推理方向,防止长程任务逻辑漂移。",
|
||||||
"parameters": {"type": "object", "properties": {
|
"parameters": {"type": "object", "properties": {
|
||||||
"keys": {"type": "string", "description": "每轮自动注入的必须注意的文本,可以是约束规则/执行步骤/需要注意的坑"},
|
"key_info": {"type": "string", "description": "替换掉当前的极简关键信息(200tokens以内),包含当前进度和目标、已获取的关键信息及下一步必须绕过的坑,已经解决或完成的步骤可以去掉"},
|
||||||
"sop_path": {"type": "string", "description": "sop路径,必要时可以再读"}}}
|
"related_sop": {"type": "string", "description": "相关sop名称,可以多个,必要时需要再读"}}}
|
||||||
}},
|
}},
|
||||||
{"type": "function", "function": {
|
{"type": "function", "function": {
|
||||||
"name": "ask_user",
|
"name": "ask_user",
|
||||||
@@ -62,7 +62,7 @@
|
|||||||
}},
|
}},
|
||||||
{"type": "function", "function": {
|
{"type": "function", "function": {
|
||||||
"name": "trigger_memory_update",
|
"name": "trigger_memory_update",
|
||||||
"description": "用户的任务完成后,若发现值得长期记忆的信息(环境事实/用户偏好/避坑经验),调用此工具触发记忆提炼。不允许在记忆更新或自主流程内调用。",
|
"description": "若发现值得长期记忆的信息(环境事实/用户偏好/避坑经验),调用此工具触发记忆提炼。一次用户对话只允许调用一次,严禁在记忆更新或自主流程内调用。",
|
||||||
"parameters": {"type": "object", "properties": {}}}
|
"parameters": {"type": "object", "properties": {}}}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
26
ga.py
26
ga.py
@@ -241,8 +241,8 @@ class GenericAgentHandler(BaseHandler):
|
|||||||
'''
|
'''
|
||||||
def __init__(self, parent, last_history=None, cwd='./'):
|
def __init__(self, parent, last_history=None, cwd='./'):
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.sop_keys = ""
|
self.key_info = ""
|
||||||
self.sop_path = ""
|
self.related_sop = ""
|
||||||
self.cwd = cwd
|
self.cwd = cwd
|
||||||
self.history_info = last_history if last_history else []
|
self.history_info = last_history if last_history else []
|
||||||
self.code_stop_signal = []
|
self.code_stop_signal = []
|
||||||
@@ -382,19 +382,19 @@ class GenericAgentHandler(BaseHandler):
|
|||||||
result = tips + result
|
result = tips + result
|
||||||
next_prompt = self._get_anchor_prompt()
|
next_prompt = self._get_anchor_prompt()
|
||||||
if 'memory' in path or 'sop' in path:
|
if 'memory' in path or 'sop' in path:
|
||||||
next_prompt += "\nPROTOCOL: 你正在读取记忆或SOP文件,若决定按sop执行请先调用准备执行相关工具,提取sop中的重点内容(特别是靠后的)进入工作记忆。"
|
next_prompt += "\nPROTOCOL: 你正在读取记忆或SOP文件,若决定按sop执行请先调用相关工具提取sop中的关键点(特别是靠后的)进入工作记忆。"
|
||||||
return StepOutcome(result, next_prompt=next_prompt)
|
return StepOutcome(result, next_prompt=next_prompt)
|
||||||
|
|
||||||
def do_update_sop_plan(self, args, response):
|
def do_update_working_mem(self, args, response):
|
||||||
'''读取完sop后,为整个任务设定后续需要临时记忆的重点。
|
'''读取完sop后,为整个任务设定后续需要临时记忆的重点。
|
||||||
'''
|
'''
|
||||||
sop_keys = args.get("keys", "")
|
key_info = args.get("key_info", "")
|
||||||
sop_path = args.get("sop_path", "")
|
related_sop = args.get("related_sop", "")
|
||||||
if sop_keys: self.sop_keys = sop_keys
|
if key_info: self.key_info = key_info
|
||||||
if sop_path: self.sop_path = sop_path
|
if related_sop: self.related_sop = related_sop
|
||||||
yield f"[Info] Updated sop_keys and sop_path.\n"
|
yield f"[Info] Updated key_info and related_sop.\n"
|
||||||
yield f"sop_keys:\n{self.sop_keys}\n\n"
|
yield f"key_info:\n{self.key_info}\n\n"
|
||||||
yield f"sop_path:\n{self.sop_path}\n\n"
|
yield f"related_sop:\n{self.related_sop}\n\n"
|
||||||
next_prompt = self._get_anchor_prompt()
|
next_prompt = self._get_anchor_prompt()
|
||||||
return StepOutcome({"status": "success"}, next_prompt=next_prompt)
|
return StepOutcome({"status": "success"}, next_prompt=next_prompt)
|
||||||
|
|
||||||
@@ -458,8 +458,8 @@ class GenericAgentHandler(BaseHandler):
|
|||||||
def _get_anchor_prompt(self):
|
def _get_anchor_prompt(self):
|
||||||
h_str = "\n".join(self.history_info[-20:])
|
h_str = "\n".join(self.history_info[-20:])
|
||||||
prompt = f"\n### [WORKING MEMORY]\n<history>\n{h_str}\n</history>"
|
prompt = f"\n### [WORKING MEMORY]\n<history>\n{h_str}\n</history>"
|
||||||
if self.sop_keys: prompt += f"\n<sop_essentials>{self.sop_keys}</sop_essentials>"
|
if self.key_info: prompt += f"\n<sop_essentials>{self.key_info}</sop_essentials>"
|
||||||
if self.sop_path: prompt += f"\n有不清晰的地方请再次读取{self.sop_path}"
|
if self.related_sop: prompt += f"\n有不清晰的地方请再次读取{self.related_sop}"
|
||||||
print(prompt)
|
print(prompt)
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user