ChatMem#
Overview#
ChatMem is a high-level memory capability provided by the Agent framework, which can be directly integrated into Agent nodes as conversation memory, allowing you to write customized prompts to guide the memory to focus on specific content.
Characteristics#
As a memory module: ChatMem inherits from
MemBase, a high-level memory module that can understand context information from a specific perspective based on user promptsHigh flexibility: The system provides a directly runnable ChatMem implementation, wherein the memory perspective, memory extraction, and memory update strategies can all be customized through incoming prompts.
Context Messages Concatenation Scheme: You can inherit
_select_messagesand_message_to_textto implement a custom method for converting an agent message list into content to be processed and stored.
Best Practices#
Build a ChatMem, and its usage
from evofabric.core.vectorstore import ChromaDB
from evofabric.core.mem import ChatMem, FEAT_DEFINE_PROMPT_ZH
from evofabric.core.typing import UserMessage, LLMChatResponse
from evofabric.core.clients import OpenAIChatClient, SentenceTransformerEmbed
import os
# Define a Chat client
chat_client = OpenAIChatClient(
model=os.getenv("MODEL_NAME"),
stream=False,
client_kwargs={
"api_key": os.getenv("OPENAI_API_KEY"),
"base_url": os.getenv("OPENAI_BASE_URL"),
},
)
# Define a vector database
embed_client = SentenceTransformerEmbed(
device="cpu",
model="your-model-path",
)
vectorstore = ChromaDB(
collection_name="chroma_db",
persist_directory="./chroma_test",
embedding=embed_client,
top_k=2
)
# Define your ChatMem
chat_mem = ChatMem(
zh_mode=False,
vectorstore=vectorstore,
chat_client=chat_client,
feat_define_prompt=FEAT_DEFINE_PROMPT_ZH,
user_extract_prompt="Your memory extraction prompt, or use default",
user_update_prompt="Your memory update prompt, or use default",
)
# Add messages to memory
state_messages = [
UserMessage(content="Ate a cake, feeling very happy")
]
await chat_mem.add_messages(state_messages)
state_messages = [
UserMessage(content="On the way home, fell down, feeling sad")
]
await chat_mem.add_messages(state_messages)
# Use ChatMem for retrieval
retrieval_messages = [
UserMessage(content="How am I feeling today?")
]
retrieved_messages = await chat_mem.retrieval_update(retrieval_messages)