Skip to main content
You can also check this cookbook in colab here Star us on Github, join our Discord or follow our X

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:

🔑 Setting Up API Keys

You’ll need to set up your API keys for OpenAI.
Alternatively, if running on Colab, you could save your API keys and tokens as Colab Secrets, and use them across notebooks. To do so, comment out the above manual API key prompt code block(s), and uncomment the following codeblock. ⚠️ Don’t forget granting access to the API key you would be using to the current notebook.

Usage

To use the Memory module in your agent:
  1. Choose an appropriate AgentMemory implementation (ChatHistoryMemory, VectorDBMemory, or LongtermAgentMemory).
  2. Initialize the memory with a context creator and any necessary parameters.
  3. Use write_records() to add new information to the memory.
  4. Use retrieve() to get relevant context for the agent’s next action.
  5. Use get_context() to obtain the formatted context for the agent.

Setting LongtermAgentMemory:

Import required modules

Adding LongtermAgentMemory to your ChatAgent:

Advanced Topics

Customizing Context Creator

You can create custom context creators by subclassing BaseContextCreator:

Customizing Vector Database Block

For VectorDBBlock, you can customize it by adjusting the embedding models or vector storages:

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.