长期记忆包含有关智能体当前情况的上下文信息,而长期记忆则存储智能体过去的行为和想法,可以根据当前事件进行检索。

长期记忆类似外部向量存储,智能体可以根据需要快速查询和检索。

在DB-GPT中,长期记忆默认存储在向量数据库中。

使用长期记忆

为使用长期记忆,你需要提供向量存储

准备Embedding模型

首先,需要准备一个Embedding模型,用于将文本转化为向量。 可以通过如下的文档进行准备: 准备Embedding模型

下面是使用OpenAI Embedding模型的样例

  1. import os
  2. from dbgpt.rag.embedding import DefaultEmbeddingFactory
  3. api_url = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1") + "/embeddings"
  4. api_key = os.getenv("OPENAI_API_KEY")
  5. embeddings = DefaultEmbeddingFactory.openai(api_url=api_url, api_key=api_key)

准备向量存储

你需要准备向量存储,我们使用ChromaStore来作为样例,通过如下命令安装chroma

  1. pip install chromadb
  1. import shutil
  2. from dbgpt.storage.vector_store.chroma_store import ChromaVectorConfig, ChromaStore
  3. # Delete old vector store directory(/tmp/tmp_ltm_vector_stor)
  4. shutil.rmtree("/tmp/tmp_ltm_vector_store", ignore_errors=True)
  5. vector_store = ChromaStore(
  6. ChromaVectorConfig(
  7. embedding_fn=embeddings,
  8. vector_store_config=ChromaVectorConfig(
  9. name="ltm_vector_store",
  10. persist_path="/tmp/tmp_ltm_vector_store",
  11. ),
  12. )
  13. )

使用长记忆

  1. from concurrent.futures import ThreadPoolExecutor
  2. from dbgpt.agent import AgentMemory, LongTermMemory
  3. # Create an agent memory, which contains a long-term memory
  4. memory = LongTermMemory(
  5. executor=ThreadPoolExecutor(), vector_store=vector_store, _default_importance=0.5
  6. )
  7. agent_memory: AgentMemory = AgentMemory(memory=memory)

在上述代码中,_default_importance表示一个内存片段的默认重要性,因为我们直接使用LongTermMemory,所以需要设置默认的重要性。