Skip to main content

Memory Modules

In this section, we'll cover:

💭 What is a Memory Module?

Memory modules enable agent modules on Naptha to store, retrieve, and manage their experiences over time. These modules are crucial for maintaining context and learning from past interactions.

You can create modules for different types of memories such as:

  • Cognitive Memory: Store reflections and learned insights
  • Episodic Memory: Record sequential experiences
  • Semantic Memory: Store knowledge and facts

Naptha Nodes support the deployment of Memory modules. The state of these modules is stored in a local database (postgres) and file system on the Naptha Node.

📝 Memory Configurations

You can configure a memory module by specifying:

  • An LLM Configuration - The language model that the memory uses to generate responses

The configuration of a memory module can be specified using the MemoryConfig class:

#naptha_sdk/schemas.py
class MemoryConfig(BaseModel):
config_name: Optional[str] = None
llm_config: Optional[LLMConfig] = None
storage_config: Optional[StorageConfig] = None
info

More details on the StorageConfig schema can be found in the Storage Provider section.

🐋 Memory Deployments

Memory deployments allow you to specify the node that the memory will run on, the module that the memory will use, and the config that the memory will use. The configuration of a memory deployment can be specified using the MemoryDeployment class:

#naptha_sdk/schemas.py
class MemoryDeployment(BaseModel):
node: Union[NodeConfig, NodeConfigUser, Dict]
name: Optional[str] = None
module: Optional[Dict] = None
config: Optional[MemoryConfig] = None

🚀 Running a Memory Module

Prerequisites

Install the Naptha SDK using the instructions here.

Example

The Cognitive Memory module is a simple example of a Memory module. It is intended to demonstrate how agents can interact with a Memory module that allows them to store and retrieve cognitive steps such as reflections. You can create a memory table using:

The configuration of a memory module is specified in the deployment.json file in the configs folder of the module.

# MemoryConfig in configs/deployment.json file 
[
{
...
"config": {
"storage_config": {
"storage_type": "db",
"path": "cognitive_memory",
"storage_schema": {
"memory_id": {"type": "INTEGER", "primary_key": true},
"cognitive_step": {"type": "TEXT"},
"content": {"type": "TEXT"},
"created_at": {"type": "TEXT"},
"metadata": {"type": "jsonb"}
},
"options": {
"query_col": "title",
"answer_col": "text"
}
}
}
}
]

You can deploy the memory (without running) using:

# usage: naptha create <memory_name>
naptha create memory:cognitive_memory

There is a CognitiveMemory class in the run.py file that has a number of methods. You can think of these methods as endpoints of the Memory, which will be called using the run command below. For example, you can initialize the table in Memory using:

naptha run memory:cognitive_memory -p "func_name='init'"

You can add to the memory table using:

naptha run memory:cognitive_memory -p '{
"func_name": "store_cognitive_item",
"func_input_data": {
"cognitive_step": "reflection",
"content": "I am reflecting."
}
}'

You can query the memory table using:

naptha run memory:cognitive_memory -p '{
"func_name": "get_cognitive_items",
"func_input_data": {
"cognitive_step": "reflection"
}
}'

You can delete a row in the memory table using:

naptha run memory:cognitive_memory -p '{
"func_name": "delete_cognitive_items",
"func_input_data": {
"condition": {"cognitive_step": "reflection"}
}
}'

Examples

Check out these memory implementations:

Need Help?

  • Join our Community and post in the #support channel
  • Submit issues on GitHub

Next Steps