01_MemoryTool_Basic_Operations.py 5.6 KB

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