Simple Memory
A simple example of how to store and derive facts about individual users
This guide shows how to implement a simple user memory system that derives and stores facts about users that are then referenced later on.
A fully working example can be found on GitHub. It’s setup as a discord bot so view our Discord guide for more details on how to set that up.
Initial Setup
First we’ll have some setup code to initialize our LLM, Honcho client, and prompts.
The 3 steps to this project are:
- Derive facts about the user.
- Store those facts in a per-user vector db (
Collection
). - Retrieve those facts later on to improve our response.
Step 1 - Deriving Facts
For this part we will be leveraging LangChain and GPT-4 to derivce facts on the user based on their recent input.
Breaking down this function we start by using LCEL to make a chain that will take the user_input
as a variable. The
prompt used here is below:
We then invoke the chain and parse the output to get our list of facts. We take advantage of one of LangChain’s built in output parsers for this.
Step 2 - Storing Facts
This is where Honcho comes into play. With Honcho we can initialize
Collections
for each user and can store facts as vector embeddings. You can
use multiple collections if you want to segment different types of facts or
data, but for now we just need one.
That’s it. We just make the collection if it doesn’t already exist for the user and add the facts.
Step 3 - Retrieving Facts
This is where we actually make user of the facts. To do this we need to first determine a query to the collection. This query is natural language that is compared to other facts in the collection using cosine similarity.
The trick we use below is to have the LLM determine the query and then use it in a later step.
In the the introspect
method we are using the user’s response as input to derive the query. The prompt used is below:
Then we use the question that it generated as the query in our collection.query()
method. the top_k
parameter allows you to control
how many retrieved facts to return with a max of 50.
The prompt use there is below as well:
This is a very simple method of using Honcho to hold user context. For further reading on the limits read about violation of expectation.