- Chat History: previous messages from the conversation, we recommend sending the last 3-5 messages to the model.
- User Memories: notes and insights about the user, this helps the model personalize the response to the user.
- Summaries: a summary of the conversation, which is added to the prompt when chat history gets too long.
- Session: Each conversation with an Agent is called a session. Sessions are identified by a
session_id. - Run: Every interaction (i.e. chat) within a session is called a run. Runs are identified by a
run_id. - Messages: are the individual messages sent to and received from the model. They have a
role(system,userorassistant) andcontent.
Built-in Memory
Every Agent comes with built-in memory that can be used to access the historical runs and messages. Access it usingagent.memory
Example
agent_memory.py
Persistent Memory
The built-in memory only lasts while the session is active. To persist memory across sessions, we can store Agent sessions in a database usingAgentStorage.
Storage is a necessary component when building user facing AI products as any production application will require users to be able to “continue” their conversation with the Agent.
Let’s test this out, create a file persistent_memory.py with the following code:
persistent_memory.py
Run the agent
Install dependencies and run the agent:session_id.
Read more in the storage section.
User preferences and conversation summaries
Along with storing chat history and run messages,AgentMemory can be extended to automatically classify and store user preferences and conversation summaries.
To do this, add a db to AgentMemory and set create_user_memories=True and create_session_summary=True
User memories are stored in the AgentMemory whereas session summaries are stored in the AgentStorage table with the rest of the session information.
User preferences and conversation summaries are currently only compatible with
OpenAI and OpenAILike models. While Persistent Memory is compatible with
all model providers.Example
personalized_memories_and_summaries.py