In this video, we're going to take a look at how to add long-term memories for our AI agents. In the previous video, talked about how to build a WhatsApp AI assistant that can take audio, images, and text. But there was a one big problem.
It has no memory. Every time when a user start a new conversation, the chatbot forgots everything from before. And that's not how a real conversation work.
So, in this video, I'm going to show you how to fix that. And we're going to add long-term conversational memory to our WhatsApp chat bots so that I can be able to remember the key facts across multiple chats. Now you might be wondering why can't we just use short-term memories or something fancy like rag retrieval augmented generations.
Well, here's the thing. Short-term memories in tools like unin resets every session. That means that the AI agents will forget as soon as the workflow ends.
Or let's say we want to make a change in the workflow. Then the short-term memories will also reset as well. Now regg on the other hand pulling info from documents or knowledge base but not for tracking personalized or user specific data like hey my name's Eric.
And when it comes to storing this data longterm, we had a few options. Now, Air Table is great, right? It's great for a simple visual database, but it has API limitations and is also less scalable for growing data sets.
Usually, people use this product is suitable for manual data review or rather than just automated the memory storage. Now, we also have MongoDB on the other hand where is excellent for flexible JSON data. We choose SQL database for structure, faster for query, easy to integrate tools like an end.
So, we're going to use that instead. And by the end of this video, your AI agents will be able to remember the important details from the conversation and respond the way how you want it. Now, if you find this video helpful, please drop a like or comment.
And with that being said, let's get started. All right. So, the first thing we're going to do is to go to Subbase and click on start your project.
And here I'm just going to choose sign up for a new account. And here I'm just going to create a new organization in Subase. Okay.
So, once you created your organization, it's time for us to create a project and a password for the database. So once you define those, we're just going to go ahead and click on the create new project. So now you can see that we have our project created.
Then what we can do is to go to database and then click on create new table. So here we don't have any table. We're just going to create one.
And once we create a new table, we're just going to give it a name. And here I'm just going to call it conversation memory. And if you were to scroll down, you can see that we can have the option to define the columns.
So to do so, basically we're just going to add a few columns here. So first we're going to define the message. In this case, the message here is just going to be a text.
So, we're just going to choose text. And then we're also going to add another column here just to define the recipient. And then we also need to define the sender.
So, who send the message, right? So, that's also going to be a text. Now, here we're just going to put the number the phone number for the recipient, phone number for the sender, and as well as the message, right?
And then we also have the created at which is the time that when it was sent. So if everything looks good, we can just click on save and it's going to create these five columns and a table called conversation memory. Okay.
So once that's created, then what we can do is to basically integrate this onto our project. Okay. So back to the workflow, then what we can do is to add a superbase for the tool.
So in that case, we're just going to click on tools and we're going to search for superbase. And once we search for superbase, we're just going to select superbase tool. Now we have the operation here to add records.
In this case, create operation. So first we're going to set up the credentials and here we have to input the host and the service role secrets. So to basically get the host and the service role secrets we just go directly to the project settings and then click on the API and here in this case we're just going to click on a data API here and here we're just going to copy the URL which is what we see here and that's going to be the host for our Nan.
So come back here I'm just going to paste the host. Now in terms of the service role we're just going to copy the service role from the project API keys and then we come back here and paste it for the service ro secret. And then we're just going to click on save connection test this successfully.
Then we're just going to close this. And then here inside of our suase we have the table name. So here we can be able to select the conversation memory.
And then data to send which is defined below for each column. All right. So now let's test the workflow and we're going to see if we can be able to get a test data that we can work with and I'm just going to send the data here.
So for example, I'm just going to say my name is Eric. Let's see if it triggers the workflow. So it triggers the workflow and then here you can see the workflow has executed successfully and if I come back you can see that we have hello Eric how can I assist you today and now if I were to click on subbase.
So here you can see we have some data from the inputs that we can be able to save into the records. So here I'm just going to say fields to send which is going to be message and I'm just going to put the body for the text body here. Uh that's going to be the message we're going to record.
We're also going to add another field here that's going to be the sender. So who is the sender in this case? So that's going to be the from and then we're also going to add another field here to also add who is the recipient.
So here we also have the recipient and then the test number here just going to be the recipient. All right. So now you can see that we have a tool created for save message.
And now what we can do is to give instructions for tools agents on when to save the message. Okay. So here we're just going to add options.
And here we're just going to add a system message. And I'm just going to expand this. And here we can be able to change the instruction.
And here inside of the instruction, you can see that I basically specify the rule. So you're a AI assistant. And then the rules here is basically when a user sends a message, decide if you want to save this or not into the memory.
And we also have if you're going to use the save memory tool to store this information, do not inform the user. Now this can be optional. If you want to inform the user that you're saving this information, but simply continue the conversation as normal.
And then for the tools here, we have store the important facts shared by the user. summarize this information clearly and pass it to this tool. Okay, that's basically the instruction.
Now, in terms of the definition for the memories, these are the last store facts collected from the user including dates and time. Okay, and most importantly, we want to make sure that we only want to store facts into the account when replying and if the fact already stored in the memory, then we do not want to repeat asking the same question again. Okay.
Now, because in the system message, we have led the AI agents to define what's important facts to store in the memory. So in that case in the save message here we have to let AI to generate the message to let the AI to summarize the message to save. So in that case we're just going to click on save message and here inside of the message here we can actually click on the AI generate and this will basically let the AI to generate what to be saved for the message.
Okay, instead of just passing what's the user sent, we can be able to let AI to summarize it and be able to extract the most important information and save it here. So I'm just going to click on this defined automatically by the model and we're going to use that to save inside of our database. Okay.
So let's test this out. We're going to save this and I'm just going to click on test workflow and here I'm just going to say my name is Tom and let's see it triggers the workflow and it also saves the records to the memory and sends the message nice to meet you Tom how's your day going? Okay so now if I were to click on the save message we can see that this is the records that we added to the subase.
We have the sender which I blurred this out. And then we also have the recipient which is the test number and then we also have the message. In this case it's basically saying that user's name is Tom.
Okay. And then we also have the created at which is the date of the record insertion. And now if I were to navigate toase for the conversation memory table which here you can see the user's name is Tom.
And then we also have recipient and then we also have sender. Okay. All right.
So now what we can do is once we have our memory saved into our database, we can also be able to retrieve this and pass it to the AI agents to generate the response. So here we're just going to click on a plus sign and we're just going to search for superbase. So here we're just going to say get many rows and here what we do is we're just going to select a table for the conversation memory.
Here we're just going to select return all records. So here we're just going to add a filter for the condition. And what we're going to do is we're going to filter by sender equals to and here we're just going to pass the sender phone number to the field value.
So once we pass it, this is going to be the value that we're going to find for all the messages that are sent by this person. Okay. So then what we can do is we can be able to click on test step and see what it generates.
Okay. So here you can see we have one item being retrieved. Let's also add additional messages here.
I'm just going to say I'm currently 5 years old. And this should be also saved to the database. And if we were to look at our table, you can see that we have another records that the current user is currently 5 years old.
Okay. So now what we can do is we can click on the game memory here and we should be able to see two messages. All right.
So now you can see I click on test step and here you can see we have two items showing. One is the user's name is Tom and the other one is user currently 5 years old. Awesome.
So then what we can do is we can be able to pass this data onto the AI agent to be able to generate the response. But before we do so, I also want to make sure that the data that we have passed is only the messages. Right?
In that case, what we can do is be able to use a aggregate function that we can be able to only select the message and when the message was sent and pass those two important informations to the AI agents for each of the conversation memory messages. So in that case for the aggregate, what we're going to do is we're going to choose all items data into a single list. And then in terms of the output field name, we're just going to call it the conversation memories.
And then in terms of the include we're just going to say specific fields. So we will only want to include these fields and that's going to be the message and created at. Okay.
So if I were to click on test step and you can see that this is the data that we have. So we have the conversation memories and we have when the message was sent and then we also have the message content. Okay.
So then what we can do is back to the AI agents. We can then pass the past memories as well as the new message from the WhatsApp trigger to the AI agents. So in that case we have the prompt which is the user message and in terms of the memories I'm just going to pass the memories here.
So the memories is going to be the conversation memory. So we're just going to pass the conversation memory here. And notice here that it just returns just the object.
So in that case we need to convert the JSON objects here into a string right into text. So in that case we're going to say to JSON string and you can see that we convert the objects into string format or text format. So we have the message date as well as the message.
So once we have this, let's close this and let's try to test it out again. So I'm just going to click on test workflow. Come back here.
I'm just going to ask what is my name. It's going to trigger the workflow and we have a new message sent. And then you can see the workflow has executed successfully.
And now if I were to come back to WhatsApp, you can see that we have a response which is your name is Tom. So let me just turn the workflow to active. Now let's ask more questions to test this.
For example, I'm just going to say am I allowed to drive a car? Now because I have already mentioned in the past conversation that the age is five, right? So in that case we should get a response from the AI that we're not allowed to drive a car because we set the age to 5 years old or in in this case the past conversation mentioned that the current person or the current user is 5 years old.
So let's try to trigger this workflow and let's see what the response look like. So here you can see we got a response and it says since you're five years old you won't be able to drive a car yet. Typically, people need to be at least 18 years old.
So, at least you can see that we have long-term memory stored and be able to retrieve the long-term memory from the uh sumase to be able to generate a response by the AI agents. All right. So, then I did a bit more testing and I mentioned what food do I like and I like pizza.
And then here you can see that is able to capture this data, save it into the memory and then I asked another question what food do I like then is able to retrieve the memory conversation and be able to answer this question. So you it says you you mentioned you like pizza and it asks another question what type of pizza you like right so you can see that it's able to retrieve the data from the conversation memory and be able to answer the question that the user asked right and here I'm just going to ask the same question what is my name again and this should be able to retrieve that information from the memory and be able to answer so here it says your name is Tom okay now because you can see that we have several questions for example what food do I like and uh what's my name again and also can I allow to drive a car. You can see that those are additional message that we sent but really the most important information from this conversation is my name is Tom.
I'm 5 years old and I like pizza. Right? Those are the three important information.
But if we were to check the database you can see that that's the only three information that's being captured in the conversation memory. So if I were to refresh this and you can see that after refresh uh we still able to see that these are the only three data being captured from the conversation memory because we have told the AI agents to only capture the most important informations into the conversation memory. Okay, and that's what we have.
So you can see that this workflow is very powerful. We're able to save the most important informations from the conversation into the memory and we're also able to retrieve the memory along with the user prompt to ask the questions to the AI agents to respond.