Autogen Memory Example
Autogen is a library created by Microsoft for building multi-agent applications. This example demonstrates using Zep for Autogen long-term memory.
NOTE This example does not include all code required to run the Autogen Agents.
A complete Notebook example of using Zep for Autogen long-term memory may be found in the Zep Python SDK Repository.
This example walks through how to build an Autogen Agent with long-term memory. Zep builds a knowledge graph from user interactions with the agent, enabling the agent to recall relevant facts from previous conversations or user interactions.
In this example we will:
- Create an Autogen Agent class that extends
ConversableAgent
by adding long-term memory - Create a Mental Health Assistant Agent, CareBot, that acts as a counselor and coach.
- Create a user Agent, Cathy, who stands in for our expected user.
- Demonstrate preloading chat history into Zep.
- Demonstrate the agents in conversation, with CareBot recalling facts from previous conversations with Cathy.
- Inspect Facts within Zep, and demonstrate how to use Zep’s Fact Ratings to improve the quality of returned facts.
Install dependencies
Import Autogen and configure define a config_list
initiualize the Zep Client
You can sign up for a Zep account here: https://www.getzep.com/
ZepConversableAgent
The ZepConversableAgent
is a custom implementation of the Autogen ConversableAgent
that integrates with Zep for long-term memory management. This class extends the functionality of the base ConversableAgent
by adding Zep-specific features for persisting and retrieving facts from long-term memory.
Zep User and Session Management
Zep User
A Zep User represents an individual interacting with your application. Each User can have multiple Sessions associated with them, allowing you to track and manage interactions over time. The unique identifier for each user is their UserID
, which can be any string value (e.g., username, email address, or UUID).
Zep Session
A Zep Session represents a conversation and can be associated with Users in a one-to-many relationship. Chat messages are added to Sessions, with each session having many messages.
Fact Rating
Fact Rating is a feature in Zep that allows you to rate the importance or relevance of facts extracted from conversations. This helps in prioritizing and filtering information when retrieving memory artifacts. Here, we rate facts based on poignancy. We provide a definition of poignancy and several examples of highly poignant and low-poignancy facts. When retrieving memory, you can use the min_rating
parameter to filter facts based on their importance.
Fact Rating helps ensure the most relevant information, especially in long or complex conversations, is used to ground the agent.
Preload a prior conversation into Zep
We’ll load a prior conversation into long-term memory. We’ll use facts derived from this conversation when Cathy restarts the conversation with CareBot, ensuring Carebot has context.
Review all facts in Zep
We query all session facts for this user session. Only facts that meet the MIN_FACT_RATING
threshold are returned.
Create the Autogen agent, CareBot, an instance of ZepConversableAgent
We pass in the current session_id
into the CareBot agent which allows it to retrieve relevant facts related to the conversation with Cathy.
Create the Autogen agent, Cathy
Cathy is a stand-in for a human. When building a production application, you’d replace Cathy with a human-in-the-loop pattern.
Note that we’re instructing Cathy to start the conversation with CareBit by asking about her previous session. This is an opportunity for us to test whether fact retrieval from Zep’s long-term memory is working.
Start the conversation
We use Autogen’s a_initiate_chat
method to get the two agents conversing. CareBot is the primary agent.
NOTE how Carebot is able to recall the past conversation about Cathy’s mother in detail, having had relevant facts from Zep added to its system prompt.
Review current facts in Zep
Let’s see how the facts have evolved as the conversation has progressed.
Search over Facts in Zep’s long-term memory
In addition to the memory.get
method which uses the current conversation to retrieve facts, we can also search Zep with our own keywords. Here, we retrieve facts using a query. Again, we use fact ratings to limit the returned facts to only those with a high poignancy rating.
The memory.search_sessions
API may be used as an Agent tool, enabling an agent to search across user memory for relevant facts.