1
0

01_MemoryTool_Basic_Operations.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 代码示例 01: MemoryTool基础操作
  5. 展示MemoryTool的核心execute方法和基本操作
  6. """
  7. from dotenv import load_dotenv
  8. load_dotenv()
  9. from datetime import datetime
  10. from typing import List
  11. from hello_agents.tools import MemoryTool
  12. def memory_tool_execute_demo():
  13. """MemoryTool execute方法演示"""
  14. print("🧠 MemoryTool基础操作演示")
  15. print("=" * 50)
  16. # 初始化MemoryTool
  17. memory_tool = MemoryTool(
  18. user_id="demo_user",
  19. memory_types=["working", "episodic", "semantic", "perceptual"]
  20. )
  21. print("✅ MemoryTool初始化完成")
  22. print(f"📋 支持的操作: add, search, summary, stats, update, remove, forget, consolidate, clear_all")
  23. return memory_tool
  24. def add_memory_demo(memory_tool):
  25. """添加记忆演示 - 模拟人类记忆编码过程"""
  26. print("\n📝 添加记忆演示")
  27. print("-" * 30)
  28. # 添加工作记忆
  29. result = memory_tool.run({
  30. "action":"add",
  31. "content":"正在学习HelloAgents框架的记忆系统",
  32. "memory_type":"working",
  33. "importance":0.7,
  34. "task_type":"learning"
  35. })
  36. print(f"工作记忆: {result}")
  37. # 添加情景记忆
  38. result = memory_tool.run({
  39. "action":"add",
  40. "content":"2024年开始深入研究AI Agent技术",
  41. "memory_type":"episodic",
  42. "importance":0.8,
  43. "event_type":"milestone",
  44. "location":"研发中心"
  45. })
  46. print(f"情景记忆: {result}")
  47. # 添加语义记忆
  48. result = memory_tool.run({
  49. "action":"add",
  50. "content":"记忆系统包括工作记忆、情景记忆、语义记忆和感知记忆四种类型",
  51. "memory_type":"semantic",
  52. "importance":0.9,
  53. "concept":"memory_types",
  54. "domain":"cognitive_science"
  55. })
  56. print(f"语义记忆: {result}")
  57. # 添加感知记忆
  58. result = memory_tool.run({
  59. "action":"add",
  60. "content":"查看了记忆系统的架构图和实现代码",
  61. "memory_type":"perceptual",
  62. "importance":0.6,
  63. "modality":"document",
  64. "source":"technical_documentation"
  65. })
  66. print(f"感知记忆: {result}")
  67. def search_memory_demo(memory_tool):
  68. """搜索记忆演示 - 实现语义理解的检索"""
  69. print("\n🔍 搜索记忆演示")
  70. print("-" * 30)
  71. # 基础搜索
  72. print("基础搜索 - '记忆系统':")
  73. result = memory_tool.run({"action":"search", "query":"记忆系统", "limit":3})
  74. print(result)
  75. # 按类型搜索
  76. print("\n按类型搜索 - 语义记忆中的'记忆':")
  77. result = memory_tool.run({
  78. "action":"search",
  79. "query":"记忆",
  80. "memory_type":"semantic",
  81. "limit":2
  82. })
  83. print(result)
  84. # 设置重要性阈值
  85. print("\n高重要性记忆搜索:")
  86. result = memory_tool.run({
  87. "action":"search",
  88. "query":"AI Agent",
  89. "min_importance":0.7,
  90. "limit":3
  91. })
  92. print(result)
  93. def memory_summary_demo(memory_tool):
  94. """记忆摘要演示 - 提供系统全貌"""
  95. print("\n📋 记忆摘要演示")
  96. print("-" * 30)
  97. # 获取记忆摘要
  98. result = memory_tool.run({"action":"summary", "limit":5})
  99. print("记忆摘要:")
  100. print(result)
  101. # 获取统计信息
  102. print("\n📊 统计信息:")
  103. result = memory_tool.run({"action": "stats"})
  104. print(result)
  105. def memory_management_demo(memory_tool):
  106. """记忆管理演示 - 遗忘和整合"""
  107. print("\n⚙️ 记忆管理演示")
  108. print("-" * 30)
  109. # 添加一个低重要性记忆用于遗忘测试
  110. memory_tool.run({
  111. "action":"add",
  112. "content":"这是一个临时的测试记忆,重要性很低",
  113. "memory_type":"working",
  114. "importance":0.1
  115. })
  116. # 基于重要性的遗忘
  117. print("基于重要性的遗忘 (阈值=0.2):")
  118. result = memory_tool.run({
  119. "action":"forget",
  120. "strategy":"importance_based",
  121. "threshold":0.2
  122. })
  123. print(result)
  124. # 记忆整合 - 将重要的工作记忆转为情景记忆
  125. print("\n记忆整合 (working → episodic):")
  126. result = memory_tool.run({
  127. "action":"consolidate",
  128. "from_type":"working",
  129. "to_type":"episodic",
  130. "importance_threshold":0.6
  131. })
  132. print(result)
  133. def main():
  134. """主函数"""
  135. print("🚀 MemoryTool基础操作完整演示")
  136. print("展示记忆系统的核心功能和操作方法")
  137. print("=" * 60)
  138. try:
  139. # 1. 初始化MemoryTool
  140. memory_tool = memory_tool_execute_demo()
  141. # 2. 添加记忆演示
  142. add_memory_demo(memory_tool)
  143. # 3. 搜索记忆演示
  144. search_memory_demo(memory_tool)
  145. # 4. 记忆摘要演示
  146. memory_summary_demo(memory_tool)
  147. # 5. 记忆管理演示
  148. memory_management_demo(memory_tool)
  149. print("\n" + "=" * 60)
  150. print("🎉 MemoryTool基础操作演示完成!")
  151. print("=" * 60)
  152. print("\n✨ 演示的核心功能:")
  153. print("1. 🧠 四种记忆类型的添加和管理")
  154. print("2. 🔍 智能语义搜索和过滤")
  155. print("3. 📋 记忆摘要和统计分析")
  156. print("4. ⚙️ 记忆整合和选择性遗忘")
  157. print("\n🎯 设计特点:")
  158. print("• 统一的execute接口,操作简洁一致")
  159. print("• 丰富的元数据支持,便于分类和检索")
  160. print("• 智能的重要性评估和时间衰减机制")
  161. print("• 模拟人类认知的记忆管理策略")
  162. except Exception as e:
  163. print(f"\n❌ 演示过程中发生错误: {e}")
  164. import traceback
  165. traceback.print_exc()
  166. if __name__ == "__main__":
  167. main()