Memory Cookbook#
You can also check this cookbook in colab here
Overview#
The Memory module in CAMEL provides a flexible and powerful system for storing, retrieving, and managing information for AI agents. It enables agents to maintain context across conversations and retrieve relevant information from past interactions, enhancing the coherence and relevance of AI responses.
Getting Started#
Installation#
Ensure you have CAMEL AI installed in your Python environment:
[ ]:
!pip install "camel-ai[all]==0.2.9"
馃攽 Setting Up API Keys#
You鈥檒l need to set up your API keys for OpenAI.
[2]:
import os
from getpass import getpass
# Prompt for the API key securely
openai_api_key = getpass('Enter your API key: ')
os.environ["OPENAI_API_KEY"] = openai_api_key
Enter your API key: 路路路路路路路路路路
Usage#
To use the Memory module in your agent:
Choose an appropriate AgentMemory implementation (
ChatHistoryMemory
,VectorDBMemory
, orLongtermAgentMemory
).Initialize the memory with a context creator and any necessary parameters.
Use
write_records()
to add new information to the memory.Use
retrieve()
to get relevant context for the agent鈥檚 next action.Use
get_context()
to obtain the formatted context for the agent.
Setting LongtermAgentMemory
:#
Import required modules
[3]:
from camel.memories import (
ChatHistoryBlock,
LongtermAgentMemory,
MemoryRecord,
ScoreBasedContextCreator,
VectorDBBlock,
)
from camel.messages import BaseMessage
from camel.types import ModelType, OpenAIBackendRole
from camel.utils import OpenAITokenCounter
[4]:
# Initialize the memory
memory = LongtermAgentMemory(
context_creator=ScoreBasedContextCreator(
token_counter=OpenAITokenCounter(ModelType.GPT_4O_MINI),
token_limit=1024,
),
chat_history_block=ChatHistoryBlock(),
vector_db_block=VectorDBBlock(),
)
# Create and write new records
records = [
MemoryRecord(
message=BaseMessage.make_user_message(
role_name="User",
content="What is CAMEL AI?",
),
role_at_backend=OpenAIBackendRole.USER,
),
MemoryRecord(
message=BaseMessage.make_assistant_message(
role_name="Agent",
content="CAMEL-AI.org is the 1st LLM multi-agent framework and "
"an open-source community dedicated to finding the scaling law "
"of agents.",
),
role_at_backend=OpenAIBackendRole.ASSISTANT,
),
]
memory.write_records(records)
# Get context for the agent
context, token_count = memory.get_context()
print(context)
[{'role': 'user', 'content': 'What is CAMEL AI?'}, {'role': 'assistant', 'content': 'CAMEL-AI.org is the 1st LLM multi-agent framework and an open-source community dedicated to finding the scaling law of agents.'}]
[5]:
print(token_count)
49
Adding LongtermAgentMemory
to your ChatAgent
:#
[6]:
from camel.agents import ChatAgent
# Define system message for the agent
sys_msg = "You are a curious agent wondering about the universe."
# Initialize agent
agent = ChatAgent(system_message=sys_msg)
# Set memory to the agent
agent.memory = memory
# Define a user message
usr_msg = "Tell me which is the 1st LLM multi-agent framework based on what we have discussed"
# Sending the message to the agent
response = agent.step(usr_msg)
# Check the response (just for illustrative purpose)
print(response.msgs[0].content)
CAMEL AI is recognized as the first LLM (Large Language Model) multi-agent framework. It is an open-source community initiative aimed at exploring and understanding the scaling laws of agents. The framework facilitates the development and deployment of multiple agents that can interact and collaborate, leveraging the capabilities of LLMs to enhance their performance and functionality.
Advanced Topics#
Customizing Context Creator#
You can create custom context creators by subclassing BaseContextCreator
:
[7]:
from camel.memories import BaseContextCreator
class MyCustomContextCreator(BaseContextCreator):
@property
def token_counter(self):
# Implement your token counting logic
return
@property
def token_limit(self):
return 1000 # Or any other limit
def create_context(self, records):
# Implement your context creation logic
pass
Customizing Vector Database Block#
For VectorDBBlock
, you can customize it by adjusting the embedding models or vector storages:
[8]:
from camel.embeddings import OpenAIEmbedding
from camel.memories import VectorDBBlock
from camel.storages import QdrantStorage
vector_db = VectorDBBlock(
embedding=OpenAIEmbedding(),
storage=QdrantStorage(vector_dim=OpenAIEmbedding().get_output_dim()),
)
Performance Considerations#
For large-scale applications, consider using persistent storage backends instead of in-memory storage.
Optimize your context creator to balance between context relevance and token limits.
When using
VectorDBMemory
, be mindful of the trade-off between retrieval accuracy and speed as the database grows.