Hybrid Memory
This structure explicitly models the human short-term and long-term memories. The short-term memory temporarily buffers recent perceptions, while long-term memory consolidates important information over time.
For example, the short-term memory contains the context information about the agent current situations, while the long-term memory stores the agent past behaviors and thoughts, which can be retrieved according to the current events.
Creating A Hybrid Memory
Method 1: Creating A Hybrid Memory with Default Values
It will use OpenAI Embedding API and ChromaStore as the default values.
import shutil
from dbgpt.agent import HybridMemory, AgentMemory
# Delete old vector store directory(/tmp/tmp_ltm_vector_stor)
shutil.rmtree("/tmp/tmp_ltm_vector_store", ignore_errors=True)
hybrid_memory = HybridMemory.from_chroma(
vstore_name="agent_memory", vstore_path="/tmp/tmp_ltm_vector_store"
)
agent_memory: AgentMemory = AgentMemory(memory=hybrid_memory)
Method 2: Creating A Hybrid Memory With Custom Values
The hybrid memory requires sensory memory, short-term memory, and long-term memory to be created.
Prepare Embedding Model
You can prepare the embedding model according Prepare Embedding Model.
Here we use the OpenAI Embedding API as an example:
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)
Prepare Vector Store
You need to prepare a vector store, here we use the ChromaStore
as an example:
import shutil
from dbgpt_ext.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(
persist_path="/tmp/tmp_ltm_vector_store",
),
name="ltm_vector_store",
embedding_fn=embeddings
)
Create Hybrid Memory
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from dbgpt.agent import (
SensoryMemory,
EnhancedShortTermMemory,
LongTermMemory,
HybridMemory,
AgentMemory,
)
executor = ThreadPoolExecutor()
sensor_memory = SensoryMemory(buffer_size=2)
short_term_memory = EnhancedShortTermMemory(
embeddings=embeddings,
buffer_size=2,
enhance_similarity_threshold=0.7,
enhance_threshold=3,
executor=executor,
)
long_term_memory = LongTermMemory(
executor=ThreadPoolExecutor(), vector_store=vector_store, _default_importance=0.5
)
hybrid_memory = HybridMemory(
now=datetime.now(),
sensory_memory=sensor_memory,
short_term_memory=short_term_memory,
long_term_memory=long_term_memory,
)
agent_memory: AgentMemory = AgentMemory(memory=hybrid_memory)