@@ -26,13 +26,15 @@ def __init__(
2626 model : LLM ,
2727 max_chat_turns : int = 30 , # 单个agent最大对话轮次
2828 user_output : UserOutput = None ,
29+ max_memory : int = 20 , # 最大记忆轮次
2930 ) -> None :
3031 self .task_id = task_id
3132 self .model = model
3233 self .chat_history : list [dict ] = [] # 存储对话历史
3334 self .max_chat_turns = max_chat_turns # 最大对话轮次
3435 self .current_chat_turns = 0 # 当前对话轮次计数器
3536 self .user_output = user_output
37+ self .max_memory = max_memory # 最大记忆轮次
3638
3739 async def run (self , prompt : str , system_prompt : str , sub_title : str ) -> str :
3840 """
@@ -68,11 +70,20 @@ async def run(self, prompt: str, system_prompt: str, sub_title: str) -> str:
6870 return error_msg
6971
7072 def append_chat_history (self , msg : dict ) -> None :
73+ self .clear_memory ()
7174 self .chat_history .append (msg )
7275 # self.user_output.data_recorder.append_chat_history(
7376 # msg, agent_name=self.__class__.__name__
7477 # )
7578
79+ def clear_memory (self ):
80+ logger .info (f"{ self .__class__ .__name__ } :清除记忆" )
81+ if len (self .chat_history ) <= self .max_memory :
82+ return
83+
84+ # 使用切片保留第一条和最后两条消息
85+ self .chat_history = self .chat_history [:2 ] + self .chat_history [- 5 :]
86+
7687
7788class ModelerAgent (Agent ): # 继承自Agent类而不是BaseModel
7889 def __init__ (
@@ -207,18 +218,18 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
207218 error_message ,
208219 ) = await self .code_interpreter .execute_code (code )
209220
210- # 记录执行结果
211-
212221 # 添加工具执行结果
213- self .append_chat_history (
214- {
215- "role" : "tool" ,
216- "content" : text_to_gpt ,
217- "tool_call_id" : tool_id ,
218- }
219- )
220- # 代码执行错误
221222 if error_occurred :
223+ # 即使发生错误也要添加tool响应
224+ self .append_chat_history (
225+ {
226+ "role" : "tool" ,
227+ "content" : error_message ,
228+ "tool_call_id" : tool_id ,
229+ "name" : "execute_code" ,
230+ }
231+ )
232+
222233 logger .warning (f"代码执行错误: { error_message } " )
223234 retry_count += 1
224235 logger .info (f"当前尝试次:{ retry_count } / { self .max_retries } " )
@@ -227,14 +238,24 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
227238
228239 await redis_manager .publish_message (
229240 self .task_id ,
230- SystemMessage (content = "代码手反思错误 " , type = "error" ),
241+ SystemMessage (content = "代码手反思纠正错误 " , type = "error" ),
231242 )
232243
233244 self .append_chat_history (
234245 {"role" : "user" , "content" : reflection_prompt }
235246 )
236247 # 如果代码出错,返回重新开始
237248 continue
249+ else :
250+ # 成功执行的tool响应
251+ self .append_chat_history (
252+ {
253+ "role" : "tool" ,
254+ "content" : text_to_gpt ,
255+ "tool_call_id" : tool_id ,
256+ "name" : "execute_code" ,
257+ }
258+ )
238259
239260 # 检查任务完成情况时也计入对话轮次
240261 self .current_chat_turns += 1
@@ -326,7 +347,28 @@ async def run(
326347 image_prompt = f"\n 可用的图片链接列表:\n { image_list } \n 请在写作时适当引用这些图片链接。"
327348 prompt = prompt + image_prompt
328349
329- return await super ().run (prompt , self .system_prompt , sub_title )
350+ try :
351+ logger .info (f"{ self .__class__ .__name__ } :开始:执行对话" )
352+ self .current_chat_turns = 0 # 重置对话轮次计数器
353+
354+ # 更新对话历史
355+ self .append_chat_history ({"role" : "system" , "content" : self .system_prompt })
356+ self .append_chat_history ({"role" : "user" , "content" : prompt })
357+
358+ # 获取历史消息用于本次对话
359+ response = await self .model .chat (
360+ history = self .chat_history ,
361+ agent_name = self .__class__ .__name__ ,
362+ sub_title = sub_title ,
363+ )
364+ response_content = response .choices [0 ].message .content
365+ self .chat_history .append ({"role" : "assistant" , "content" : response_content })
366+ logger .info (f"{ self .__class__ .__name__ } :完成:执行对话" )
367+ return response_content
368+ except Exception as e :
369+ error_msg = f"执行过程中遇到错误: { str (e )} "
370+ logger .error (f"Agent执行失败: { str (e )} " )
371+ return error_msg
330372
331373 async def summarize (self ) -> str :
332374 """
0 commit comments