长期记忆包含有关智能体当前情况的上下文信息,而长期记忆则存储智能体过去的行为和想法,可以根据当前事件进行检索。
长期记忆类似外部向量存储,智能体可以根据需要快速查询和检索。
在DB-GPT中,长期记忆默认存储在向量数据库中。
使用长期记忆
为使用长期记忆,你需要提供向量存储
准备Embedding模型
首先,需要准备一个Embedding模型,用于将文本转化为向量。 可以通过如下的文档进行准备: 准备Embedding模型
下面是使用OpenAI Embedding模型的样例
import os
from dbgpt.rag.embedding import DefaultEmbeddingFactory
api_url = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1") + "/embeddings"
api_key = os.getenv("OPENAI_API_KEY")
embeddings = DefaultEmbeddingFactory.openai(api_url=api_url, api_key=api_key)
准备向量存储
你需要准备向量存储,我们使用ChromaStore
来作为样例,通过如下命令安装chroma
pip install chromadb
import shutil
from dbgpt.storage.vector_store.chroma_store import ChromaVectorConfig, ChromaStore
# Delete old vector store directory(/tmp/tmp_ltm_vector_stor)
shutil.rmtree("/tmp/tmp_ltm_vector_store", ignore_errors=True)
vector_store = ChromaStore(
ChromaVectorConfig(
embedding_fn=embeddings,
vector_store_config=ChromaVectorConfig(
name="ltm_vector_store",
persist_path="/tmp/tmp_ltm_vector_store",
),
)
)
使用长记忆
from concurrent.futures import ThreadPoolExecutor
from dbgpt.agent import AgentMemory, LongTermMemory
# Create an agent memory, which contains a long-term memory
memory = LongTermMemory(
executor=ThreadPoolExecutor(), vector_store=vector_store, _default_importance=0.5
)
agent_memory: AgentMemory = AgentMemory(memory=memory)
在上述代码中,_default_importance
表示一个内存片段的默认重要性,因为我们直接使用LongTermMemory
,所以需要设置默认的重要性。