正如人类的感知一样,感知记忆是记录感知输入的,它会接收来自环境的观察结果,一些感知记忆会被转移到短期记忆中。

绝大多数情况下,应该直接使用SensoryMemory, 它被设计来接收来自环境的观察结果,只有一部分感觉i一会被转移到短期记忆中。

感知记忆示例

首先,你需要创建SensoryMemory,然后可以使用它来存储感知结果。

  1. from dbgpt.agent import AgentMemory, SensoryMemory
  2. # Create an agent memory, which contains a sensory memory
  3. memory = SensoryMemory(buffer_size=2)
  4. agent_memory: AgentMemory = AgentMemory(memory=memory)

然后,让我们来创建一些用户消息用于测试

  1. import os
  2. from dbgpt.agent import AgentContext
  3. from dbgpt.model.proxy import OpenAILLMClient
  4. llm_client = OpenAILLMClient(
  5. model_alias="gpt-4o",
  6. api_base=os.getenv("OPENAI_API_BASE"),
  7. api_key=os.getenv("OPENAI_API_KEY"),
  8. )
  9. context: AgentContext = AgentContext(
  10. conv_id="test123",
  11. language="en",
  12. temperature=0.9,
  13. max_new_tokens=2048,
  14. )
  15. messages = [
  16. "When I was 4 years old, I went to primary school for the first time, please tell me a joke",
  17. "When I was 10 years old, I went to middle school for the first time, please tell me a joke",
  18. "When I was 16 years old, I went to high school for the first time, please tell me a joke",
  19. "When I was 18 years old, I went to college for the first time, please tell me a joke",
  20. ]

校验记住

  1. import asyncio
  2. from dbgpt.agent import (
  3. ConversableAgent,
  4. ProfileConfig,
  5. LLMConfig,
  6. BlankAction,
  7. UserProxyAgent,
  8. )
  9. async def verify_remember():
  10. joy = (
  11. await ConversableAgent(profile=ProfileConfig(name="Joy", role="Comedians"))
  12. .bind(context)
  13. .bind(LLMConfig(llm_client=llm_client))
  14. .bind(agent_memory)
  15. .bind(BlankAction)
  16. .build()
  17. )
  18. user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
  19. # The turns not more than 2, make sure the agent remembers the previous conversation
  20. for message in messages[:2]:
  21. await user_proxy.initiate_chat(
  22. recipient=joy,
  23. reviewer=user_proxy,
  24. message=message,
  25. )
  26. await user_proxy.initiate_chat(
  27. recipient=joy,
  28. reviewer=user_proxy,
  29. message="How old was I when I went to primary school?"
  30. )
  31. if __name__ == "__main__":
  32. asyncio.run(verify_remember())

输出

  1. --------------------------------------------------------------------------------
  2. User (to Joy)-[]:
  3. "When I was 4 years old, I went to primary school for the first time, please tell me a joke"
  4. --------------------------------------------------------------------------------
  5. un_stream ai response: Sure, here's a fun joke for you:
  6. Why did the kid bring a ladder to school?
  7. Because he wanted to go to high school!
  8. --------------------------------------------------------------------------------
  9. Joy (to User)-[gpt-4o]:
  10. "Sure, here's a fun joke for you:\n\nWhy did the kid bring a ladder to school?\n\nBecause he wanted to go to high school!"
  11. >>>>>>>>Joy Review info:
  12. Pass(None)
  13. >>>>>>>>Joy Action report:
  14. execution succeeded,
  15. Sure, here's a fun joke for you:
  16. Why did the kid bring a ladder to school?
  17. Because he wanted to go to high school!
  18. --------------------------------------------------------------------------------
  19. --------------------------------------------------------------------------------
  20. User (to Joy)-[]:
  21. "When I was 10 years old, I went to middle school for the first time, please tell me a joke"
  22. --------------------------------------------------------------------------------
  23. un_stream ai response: Sure, here's a joke for you:
  24. Why did the student eat his homework?
  25. Because the teacher said it was a piece of cake!
  26. --------------------------------------------------------------------------------
  27. Joy (to User)-[gpt-4o]:
  28. "Sure, here's a joke for you:\n\nWhy did the student eat his homework?\n\nBecause the teacher said it was a piece of cake!"
  29. >>>>>>>>Joy Review info:
  30. Pass(None)
  31. >>>>>>>>Joy Action report:
  32. execution succeeded,
  33. Sure, here's a joke for you:
  34. Why did the student eat his homework?
  35. Because the teacher said it was a piece of cake!
  36. --------------------------------------------------------------------------------
  37. --------------------------------------------------------------------------------
  38. User (to Joy)-[]:
  39. "How old was I when I went to primary school?"
  40. --------------------------------------------------------------------------------
  41. un_stream ai response: Based on your previous statements, you went to primary school for the first time when you were 4 years old.
  42. --------------------------------------------------------------------------------
  43. Joy (to User)-[gpt-4o]:
  44. "Based on your previous statements, you went to primary school for the first time when you were 4 years old."
  45. >>>>>>>>Joy Review info:
  46. Pass(None)
  47. >>>>>>>>Joy Action report:
  48. execution succeeded,
  49. Based on your previous statements, you went to primary school for the first time when you were 4 years old.

在上述示例中,Agent记得先前的对话并且可以基于先前的对话回答问题, 我们设置了SensoryMemorybuffer_size=2, 所以智能体可以回答先前两轮的对话。

校验忘记

  1. async def verify_forget():
  2. joy = (
  3. await ConversableAgent(profile=ProfileConfig(name="Joy", role="Comedians"))
  4. .bind(context)
  5. .bind(LLMConfig(llm_client=llm_client))
  6. .bind(agent_memory)
  7. .bind(BlankAction)
  8. .build()
  9. )
  10. user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
  11. for message in messages:
  12. await user_proxy.initiate_chat(
  13. recipient=joy,
  14. reviewer=user_proxy,
  15. message=message,
  16. )
  17. await user_proxy.initiate_chat(
  18. recipient=joy,
  19. reviewer=user_proxy,
  20. message="How old was I when I went to primary school?",
  21. )
  22. if __name__ == "__main__":
  23. asyncio.run(verify_forget())

输出结果如下:

  1. --------------------------------------------------------------------------------
  2. User (to Joy)-[]:
  3. "When I was 4 years old, I went to primary school for the first time, please tell me a joke"
  4. --------------------------------------------------------------------------------
  5. un_stream ai response: Sure, here's a joke for you:
  6. Why did the scarecrow become a successful student?
  7. Because he was outstanding in his field!
  8. --------------------------------------------------------------------------------
  9. Joy (to User)-[gpt-4o]:
  10. "Sure, here's a joke for you:\n\nWhy did the scarecrow become a successful student?\n\nBecause he was outstanding in his field!"
  11. >>>>>>>>Joy Review info:
  12. Pass(None)
  13. >>>>>>>>Joy Action report:
  14. execution succeeded,
  15. Sure, here's a joke for you:
  16. Why did the scarecrow become a successful student?
  17. Because he was outstanding in his field!
  18. --------------------------------------------------------------------------------
  19. --------------------------------------------------------------------------------
  20. User (to Joy)-[]:
  21. "When I was 10 years old, I went to middle school for the first time, please tell me a joke"
  22. --------------------------------------------------------------------------------
  23. un_stream ai response: Of course! Here's a joke for you:
  24. Why was the math book sad when it started middle school?
  25. Because it had too many problems!
  26. --------------------------------------------------------------------------------
  27. Joy (to User)-[gpt-4o]:
  28. "Of course! Here's a joke for you:\n\nWhy was the math book sad when it started middle school?\n\nBecause it had too many problems!"
  29. >>>>>>>>Joy Review info:
  30. Pass(None)
  31. >>>>>>>>Joy Action report:
  32. execution succeeded,
  33. Of course! Here's a joke for you:
  34. Why was the math book sad when it started middle school?
  35. Because it had too many problems!
  36. --------------------------------------------------------------------------------
  37. --------------------------------------------------------------------------------
  38. User (to Joy)-[]:
  39. "When I was 16 years old, I went to high school for the first time, please tell me a joke"
  40. --------------------------------------------------------------------------------
  41. un_stream ai response: Sure, here's a joke for you:
  42. Why did the geometry teacher go to the beach?
  43. Because she needed to find a new angle!
  44. --------------------------------------------------------------------------------
  45. Joy (to User)-[gpt-4o]:
  46. "Sure, here's a joke for you:\n\nWhy did the geometry teacher go to the beach?\n\nBecause she needed to find a new angle!"
  47. >>>>>>>>Joy Review info:
  48. Pass(None)
  49. >>>>>>>>Joy Action report:
  50. execution succeeded,
  51. Sure, here's a joke for you:
  52. Why did the geometry teacher go to the beach?
  53. Because she needed to find a new angle!
  54. --------------------------------------------------------------------------------
  55. --------------------------------------------------------------------------------
  56. User (to Joy)-[]:
  57. "When I was 18 years old, I went to college for the first time, please tell me a joke"
  58. --------------------------------------------------------------------------------
  59. un_stream ai response: Sure, here’s a college-themed joke for you:
  60. Why did the scarecrow become a successful college student?
  61. Because he was outstanding in his field! 🌾🎓😄
  62. --------------------------------------------------------------------------------
  63. Joy (to User)-[gpt-4o]:
  64. "Sure, heres a college-themed joke for you:\n\nWhy did the scarecrow become a successful college student?\n\nBecause he was outstanding in his field! 🌾🎓😄"
  65. >>>>>>>>Joy Review info:
  66. Pass(None)
  67. >>>>>>>>Joy Action report:
  68. execution succeeded,
  69. Sure, here’s a college-themed joke for you:
  70. Why did the scarecrow become a successful college student?
  71. Because he was outstanding in his field! 🌾🎓😄
  72. --------------------------------------------------------------------------------
  73. --------------------------------------------------------------------------------
  74. User (to Joy)-[]:
  75. "How old was I when I went to primary school?"
  76. --------------------------------------------------------------------------------
  77. un_stream ai response: Most people typically start primary school around 5 or 6 years old. But if you'd like a joke on that topic, here it goes:
  78. Why did the math book look so sad on its first day of primary school?
  79. Because it had too many problems! 📚😄
  80. --------------------------------------------------------------------------------
  81. Joy (to User)-[gpt-4o]:
  82. "Most people typically start primary school around 5 or 6 years old. But if you'd like a joke on that topic, here it goes:\n\nWhy did the math book look so sad on its first day of primary school?\n\nBecause it had too many problems! 📚😄"
  83. >>>>>>>>Joy Review info:
  84. Pass(None)
  85. >>>>>>>>Joy Action report:
  86. execution succeeded,
  87. Most people typically start primary school around 5 or 6 years old. But if you'd like a joke on that topic, here it goes:
  88. Why did the math book look so sad on its first day of primary school?
  89. Because it had too many problems! 📚😄

在上述例子中,智能体忘记了之前的对话,无法根据之前的对话回答问题,这是因为这块内存中的buffer_size=2, 当缓冲区满时,它会丢弃所有现有的内存,这是SensoryMemory的特殊功能,不像普通的缓冲存储器(FIFO,保留最新的buffer_size存储器)