Przeglądaj źródła

fix chapter9 about memory tool use execute function

jjyaoao 5 miesięcy temu
rodzic
commit
79776d0cbb

+ 13 - 13
code/chapter9/01_context_builder_basic.py

@@ -51,19 +51,19 @@ def main():
 
     # 4. 添加一些记忆
     print("4. 添加记忆...")
-    # memory_tool.execute(
-    #     "add",
-    #     content="用户正在开发数据分析工具,使用Python和Pandas",
-    #     memory_type="semantic",
-    #     importance=0.8
-    # )
-
-    # memory_tool.execute(
-    #     "add",
-    #     content="已完成CSV读取模块的开发",
-    #     memory_type="episodic",
-    #     importance=0.7
-    # )
+    # memory_tool.run({
+    #     "action": "add",
+    #     "content": "用户正在开发数据分析工具,使用Python和Pandas",
+    #     "memory_type": "semantic",
+    #     "importance": 0.8
+    # })
+
+    # memory_tool.run({
+    #     "action": "add",
+    #     "content": "已完成CSV读取模块的开发",
+    #     "memory_type": "episodic",
+    #     "importance": 0.7
+    # })
 
     # 5. 构建上下文
     print("5. 构建上下文...\n")

+ 6 - 6
code/chapter9/02_context_builder_with_agent.py

@@ -61,12 +61,12 @@ class ContextAwareAgent(SimpleAgent):
         )
 
         # 4. 将重要交互记录到记忆系统
-        # self.memory_tool.execute(
-        #     "add",
-        #     content=f"Q: {user_input}\nA: {response[:200]}...",  # 摘要
-        #     memory_type="episodic",
-        #     importance=0.6
-        # )
+        # self.memory_tool.run({
+        #     "action": "add",
+        #     "content": f"Q: {user_input}\nA: {response[:200]}...",  # 摘要
+        #     "memory_type": "episodic",
+        #     "importance": 0.6
+        # })
 
         return response
 

+ 37 - 37
docs/chapter9/Chapter9-Context-Engineering.md

@@ -252,12 +252,12 @@ def _gather(
     # 2. Retrieve relevant memories from memory system
     if self.memory_tool:
         try:
-            memory_results = self.memory_tool.execute(
-                "search",
-                query=user_query,
-                limit=10,
-                min_importance=0.3
-            )
+            memory_results = self.memory_tool.run({
+                "action": "search",
+                "query": user_query,
+                "limit": 10,
+                "min_importance": 0.3
+            })
             # Parse memory results and convert to ContextPacket
             memory_packets = self._parse_memory_results(memory_results, user_query)
             packets.extend(memory_packets)
@@ -267,12 +267,12 @@ def _gather(
     # 3. Retrieve relevant knowledge from RAG system
     if self.rag_tool:
         try:
-            rag_results = self.rag_tool.execute(
-                "search",
-                query=user_query,
-                limit=5,
-                min_score=0.3
-            )
+            rag_results = self.rag_tool.run({
+                "action": "search",
+                "query": user_query,
+                "limit": 5,
+                "min_score": 0.3
+            })
             # Parse RAG results and convert to ContextPacket
             rag_packets = self._parse_rag_results(rag_results, user_query)
             packets.extend(rag_packets)
@@ -614,19 +614,19 @@ conversation_history = [
 ]
 
 # 4. Add some memories
-memory_tool.execute(
-    "add",
-    content="User is developing a data analysis tool using Python and Pandas",
-    memory_type="semantic",
-    importance=0.8
-)
+memory_tool.run({
+    "action": "add",
+    "content": "User is developing a data analysis tool using Python and Pandas",
+    "memory_type": "semantic",
+    "importance": 0.8
+})
 
-memory_tool.execute(
-    "add",
-    content="Completed development of CSV reading module",
-    memory_type="episodic",
-    importance=0.7
-)
+memory_tool.run({
+    "action": "add",
+    "content": "Completed development of CSV reading module",
+    "memory_type": "episodic",
+    "importance": 0.7
+})
 
 # 5. Build context
 context = builder.build(
@@ -741,12 +741,12 @@ class ContextAwareAgent(SimpleAgent):
         )
 
         # 4. Record important interactions to memory system
-        self.memory_tool.execute(
-            "add",
-            content=f"Q: {user_input}\nA: {response[:200]}...",  # Summary
-            memory_type="episodic",
-            importance=0.6
-        )
+        self.memory_tool.run({
+            "action": "add",
+            "content": f"Q: {user_input}\nA: {response[:200]}...",  # Summary
+            "memory_type": "episodic",
+            "importance": 0.6
+        })
 
         return response
 
@@ -1957,13 +1957,13 @@ Information discovered by TerminalTool can be stored in the memory system:
 structure = terminal.run({"command": "tree -L 2 src"})
 
 # Store in semantic memory
-memory_tool.execute(
-    "add",
-    content=f"Project structure:\n{structure}",
-    memory_type="semantic",
-    importance=0.8,
-    metadata={"type": "project_structure"}
-)
+memory_tool.run({
+    "action": "add",
+    "content": f"Project structure:\n{structure}",
+    "memory_type": "semantic",
+    "importance": 0.8,
+    "metadata": {"type": "project_structure"}
+})
 ```
 
 (2) Collaboration with NoteTool

+ 37 - 37
docs/chapter9/第九章 上下文工程.md

@@ -256,12 +256,12 @@ def _gather(
     # 2. 从记忆系统检索相关记忆
     if self.memory_tool:
         try:
-            memory_results = self.memory_tool.execute(
-                "search",
-                query=user_query,
-                limit=10,
-                min_importance=0.3
-            )
+            memory_results = self.memory_tool.run({
+                "action": "search",
+                "query": user_query,
+                "limit": 10,
+                "min_importance": 0.3
+            })
             # 解析记忆结果并转换为 ContextPacket
             memory_packets = self._parse_memory_results(memory_results, user_query)
             packets.extend(memory_packets)
@@ -271,12 +271,12 @@ def _gather(
     # 3. 从 RAG 系统检索相关知识
     if self.rag_tool:
         try:
-            rag_results = self.rag_tool.execute(
-                "search",
-                query=user_query,
-                limit=5,
-                min_score=0.3
-            )
+            rag_results = self.rag_tool.run({
+                "action": "search",
+                "query": user_query,
+                "limit": 5,
+                "min_score": 0.3
+            })
             # 解析 RAG 结果并转换为 ContextPacket
             rag_packets = self._parse_rag_results(rag_results, user_query)
             packets.extend(rag_packets)
@@ -618,19 +618,19 @@ conversation_history = [
 ]
 
 # 4. 添加一些记忆
-memory_tool.execute(
-    "add",
-    content="用户正在开发数据分析工具,使用Python和Pandas",
-    memory_type="semantic",
-    importance=0.8
-)
+memory_tool.run({
+    "action": "add",
+    "content": "用户正在开发数据分析工具,使用Python和Pandas",
+    "memory_type": "semantic",
+    "importance": 0.8
+})
 
-memory_tool.execute(
-    "add",
-    content="已完成CSV读取模块的开发",
-    memory_type="episodic",
-    importance=0.7
-)
+memory_tool.run({
+    "action": "add",
+    "content": "已完成CSV读取模块的开发",
+    "memory_type": "episodic",
+    "importance": 0.7
+})
 
 # 5. 构建上下文
 context = builder.build(
@@ -745,12 +745,12 @@ class ContextAwareAgent(SimpleAgent):
         )
 
         # 4. 将重要交互记录到记忆系统
-        self.memory_tool.execute(
-            "add",
-            content=f"Q: {user_input}\nA: {response[:200]}...",  # 摘要
-            memory_type="episodic",
-            importance=0.6
-        )
+        self.memory_tool.run({
+            "action": "add",
+            "content": f"Q: {user_input}\nA: {response[:200]}...",  # 摘要
+            "memory_type": "episodic",
+            "importance": 0.6
+        })
 
         return response
 
@@ -1963,13 +1963,13 @@ TerminalTool 发现的信息可以存储到记忆系统中:
 structure = terminal.run({"command": "tree -L 2 src"})
 
 # 存储到语义记忆
-memory_tool.execute(
-    "add",
-    content=f"项目结构:\n{structure}",
-    memory_type="semantic",
-    importance=0.8,
-    metadata={"type": "project_structure"}
-)
+memory_tool.run({
+    "action": "add",
+    "content": f"项目结构:\n{structure}",
+    "memory_type": "semantic",
+    "importance": 0.8,
+    "metadata": {"type": "project_structure"}
+})
 ```
 
 (2)与 NoteTool 协同