|
@@ -889,47 +889,53 @@ $$
|
|
|
Reflection 的核心在于迭代,而迭代的前提是能够记住之前的尝试和获得的反馈。因此,一个“短期记忆”模块是实现该范式的必需品。这个记忆模块将负责存储每一次“执行-反思”循环的完整轨迹。
|
|
Reflection 的核心在于迭代,而迭代的前提是能够记住之前的尝试和获得的反馈。因此,一个“短期记忆”模块是实现该范式的必需品。这个记忆模块将负责存储每一次“执行-反思”循环的完整轨迹。
|
|
|
|
|
|
|
|
```python
|
|
```python
|
|
|
-from typing import List, Dict, Any
|
|
|
|
|
|
|
+from typing import List, Dict, Any, Optional
|
|
|
|
|
|
|
|
class Memory:
|
|
class Memory:
|
|
|
-"""
|
|
|
|
|
-一个简单的短期记忆模块,用于存储智能体的行动与反思轨迹。
|
|
|
|
|
-"""
|
|
|
|
|
-def __init__(self):
|
|
|
|
|
-# 初始化一个空列表来存储所有记录
|
|
|
|
|
-self.records: List[Dict[str, Any]] = []
|
|
|
|
|
|
|
+ """
|
|
|
|
|
+ 一个简单的短期记忆模块,用于存储智能体的行动与反思轨迹。
|
|
|
|
|
+ """
|
|
|
|
|
|
|
|
-def add_record(self, record_type: str, content: str):
|
|
|
|
|
-"""
|
|
|
|
|
-向记忆中添加一条新记录。
|
|
|
|
|
|
|
+ def __init__(self):
|
|
|
|
|
+ """
|
|
|
|
|
+ 初始化一个空列表来存储所有记录。
|
|
|
|
|
+ """
|
|
|
|
|
+ self.records: List[Dict[str, Any]] = []
|
|
|
|
|
|
|
|
-参数:
|
|
|
|
|
-- record_type (str): 记录的类型 ('execution' 或 'reflection')。
|
|
|
|
|
-- content (str): 记录的具体内容 (例如,生成的代码或反思的反馈)。
|
|
|
|
|
-"""
|
|
|
|
|
-self.records.append({"type": record_type, "content": content})
|
|
|
|
|
-print(f"📝 记忆已更新,新增一条 '{record_type}' 记录。")
|
|
|
|
|
|
|
+ def add_record(self, record_type: str, content: str):
|
|
|
|
|
+ """
|
|
|
|
|
+ 向记忆中添加一条新记录。
|
|
|
|
|
|
|
|
-def get_trajectory(self) -> str:
|
|
|
|
|
-"""
|
|
|
|
|
-将所有记忆记录格式化为一个连贯的字符串文本,用于构建提示词。
|
|
|
|
|
-"""
|
|
|
|
|
-trajectory = ""
|
|
|
|
|
-for record in self.records:
|
|
|
|
|
-if record['type'] == 'execution':
|
|
|
|
|
-trajectory += f"--- 上一轮尝试 (代码) ---\n{record['content']}\n\n"
|
|
|
|
|
-elif record['type'] == 'reflection':
|
|
|
|
|
-trajectory += f"--- 评审员反馈 ---\n{record['content']}\n\n"
|
|
|
|
|
-return trajectory.strip()
|
|
|
|
|
-
|
|
|
|
|
-def get_last_execution(self) -> str:
|
|
|
|
|
-"""
|
|
|
|
|
-获取最近一次的执行结果 (例如,最新生成的代码)。
|
|
|
|
|
-"""
|
|
|
|
|
-for record in reversed(self.records):
|
|
|
|
|
-if record['type'] == 'execution':
|
|
|
|
|
-return record['content']
|
|
|
|
|
-return None
|
|
|
|
|
|
|
+ 参数:
|
|
|
|
|
+ - record_type (str): 记录的类型 ('execution' 或 'reflection')。
|
|
|
|
|
+ - content (str): 记录的具体内容 (例如,生成的代码或反思的反馈)。
|
|
|
|
|
+ """
|
|
|
|
|
+ record = {"type": record_type, "content": content}
|
|
|
|
|
+ self.records.append(record)
|
|
|
|
|
+ print(f"📝 记忆已更新,新增一条 '{record_type}' 记录。")
|
|
|
|
|
+
|
|
|
|
|
+ def get_trajectory(self) -> str:
|
|
|
|
|
+ """
|
|
|
|
|
+ 将所有记忆记录格式化为一个连贯的字符串文本,用于构建提示词。
|
|
|
|
|
+ """
|
|
|
|
|
+ trajectory_parts = []
|
|
|
|
|
+ for record in self.records:
|
|
|
|
|
+ if record['type'] == 'execution':
|
|
|
|
|
+ trajectory_parts.append(f"--- 上一轮尝试 (代码) ---\n{record['content']}")
|
|
|
|
|
+ elif record['type'] == 'reflection':
|
|
|
|
|
+ trajectory_parts.append(f"--- 评审员反馈 ---\n{record['content']}")
|
|
|
|
|
+
|
|
|
|
|
+ return "\n\n".join(trajectory_parts)
|
|
|
|
|
+
|
|
|
|
|
+ def get_last_execution(self) -> Optional[str]:
|
|
|
|
|
+ """
|
|
|
|
|
+ 获取最近一次的执行结果 (例如,最新生成的代码)。
|
|
|
|
|
+ 如果不存在,则返回 None。
|
|
|
|
|
+ """
|
|
|
|
|
+ for record in reversed(self.records):
|
|
|
|
|
+ if record['type'] == 'execution':
|
|
|
|
|
+ return record['content']
|
|
|
|
|
+ return None
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
这个 `Memory` 类的设计比较简洁,主体是这样的:
|
|
这个 `Memory` 类的设计比较简洁,主体是这样的:
|