In this video, we are going to take a look at how to add long term memory for our WhatsApp agents using
Supabase.
In one of my previous tutorials, I talked about how to build a WhatsApp agent that can take audio,
images and text.
But the problem is it has no memory because every time a user starts a new conversation, the chatbot
forgets everything such as name of the user, the topics you've discussed, important details they mentioned,
basically anything from your previous chats.
So in this video I'm going to show you how to fix that.
We are going to add long term memory to our WhatsApp AI agent so they can actually remember important
details like usernames, preferences, past conversations and use that information to provide personalized
responses across multiple chats.
Now, why can't we just use short memory or something like RAC is because short memory in tools like
N810 receives every session.
That means the AI agent will forget as soon as the workflow ends.
Or let's say you want to make a change in the workflow.
Then the short term memory will also reset.
Now, like on the other hand, is pulling info from documents or knowledge bases, but not for tracking
personalized or user specific data.
Like to remember the user name, preferences and so on.
When it comes to storing this data long term, you have a few options.
In general, Airtable is great, but for simple visual databases like this one.
But it has API limitations, and LMS sometimes struggle to retrieve data from structured tables using
semantic search.
On the other hand, MongoDB is excellent for flexible JSON data, but we choose Supabase, which is
a SQL database that offers a structure, first queries, and easy integration with tools like atom.
So we're going to use Supabase instead.
And by the end of this video, your agents will be able to remember important details from previous
conversations.
So now let's jump into it and create our workflow from scratch.
First thing first we need to add our chicken node.
So let's search for WhatsApp trigger.
We want this workflow to be activated on message.
Now let's set up credentials.
So create credential.
If your WhatsApp account is already connected to N810 feel free to skip this step.
Otherwise, stick with me.
I will guide you through setting up your credentials step by step.
I will also show you a nice trick to extend your access token to 60 days.
So even if your WhatsApp app is not live yet, and your business phone number is not officially verified.
To make your app live, you won't have to regenerate the token every few hours as usual.
In order to connect our AI agents to WhatsApp, we need to set up a meta application first.
So let's head over to developers facebook.com and log in to your account.
Once you are logged in, you will see my apps at the top.
Click on that.
And then hit create app.
On the page that opens up.
Now we need to give our application a name.
I will call mine.
WhatsApp integration tutorial.
But you can name it whatever you like and also add your email.
In the contact field and click continue.
Click next.
Okay, certain terms like WhatsApp are not allowed so I'm going to change it.
And click next.
Okay great.
Next you will see different use case options.
So you want to select other from this list.
So just scroll down.
Select other and hit next.
Now on the following screen shows business since that's what we need for WhatsApp integration and confirm.
Go ahead and click create App to finish this step.
Perfect.
Now you will see a list of available products.
Look for WhatsApp in this list and click setup next to it.
And here is where you might need to create a business portfolio if you don't have one already.
So if the system asks for for this, don't worry, it's very simple to set up.
Just open a new tab and go to business.facebook.com.
Sign in with the same account, then look for the drop down menu on the left side.
Click on that, then click on Create a Business Portfolio.
From there.
It will ask you for basic information like your portfolio, name and contact details.
So fill that out and create your portfolio.
Then come back to our app setup and select that portfolio from the drop down.
And click continue.
And now you should see WhatsApp appear in the left sidebar.
So click on that.
Click on API setup.
And we'll be coming back to this section several times.
The first thing we need to do is add our phone number for testing.
So under this to drop down click on Manage Phone Number list.
And then add phone number.
And follows the simple verification process to add your number, you will get a code.
Alright, great.
Once that's done, select your number from the drop down.
Next we need to generate the access token.
So click on this button.
It might ask you to log in again so just reconnect.
This gives you credentials.
We need to you need to connect.
And they tend to add up.
Great.
Now let's test if everything is working.
So scroll down on this page until you see Set Message and click on this button.
And you should receive a text message on your WhatsApp.
If you got that message perfect, our WhatsApp connection is ready and we can move.
We can move on to building our workflow.
Now there is something important to mention about this access token.
Right now we are working with a temporary token that expires every few hours.
So we need to refresh this token while we are building the agents and automations in N810.
And this is totally fine for testing and development.
But if you want to use this in production, you will need a permanent access token.
And the reason we have this temporary tokens is because we are currently in development mode.
As you can see here.
For live application you would want to upgrade to a permanent solution.
So to get a permanent token you need to verify your business with meta.
I won't go through the full verification process in this tutorial since it involves business documentation,
but this is also a very simple process.
You'll need to go to your business portfolio and complete business verification.
So if you look at your business settings right here.
You'll find.
Under business info.
Business verification status.
Just simply click on View Details.
And this takes you through Metta's verification process where you'll need to provide business information
and documentation.
So just simply click on Start Verification.
And once Mita approves your business verification, you can then generate permanent API credentials
that won't expire.
However, there are two workarounds.
If you don't have a business phone number or a verified business to make your it up fully active.
So the first one is to extend the validity of your access token to 60 days.
So instead of constantly refreshing your access token to connect WhatsApp to N810, you can go to the
Facebook Developer Tools.
So click on the tools tab at the top and use Access Token Debugger.
But first let's copy our access token.
Go to API setup.
Now let's copy the token.
And now we can go to the Tools Access token debugger.
Paste your access token right here.
Press debug.
And here at the bottom Extend access token.
Now, as you can see, this token will expire two months from today.
So this way you get 60 days before you need to refresh it again.
So it's way more convenient than doing it every few hours.
Now the second option is to actually make your app active.
So switching from development mode to live mode.
So when you hover over on it, you will see that in development mode, your app can only request data
from users with an approval.
To request end user data, your app must have advanced access permissions and be set to live mode.
You can also use different Facebook accounts, add it as a test user, and use it that way, as I already
showed you.
But I'm going to show you a different way.
So let's try to switch it to live.
As you can see, you must provide a valid privacy policy URL in order to make your app live.
Go to Basic Settings and make sure it is valid.
So here is what you can do.
You can go to ChatGPT and generate a basic privacy policy and also terms of service.
So two separate documents then go to Google Drive.
Create a new document.
Paste the basic privacy policy here.
Then.
Save.
Now change the access to anyone with the link.
Then copy the link.
Go back to your meta account.
Go to Basic settings.
And paste the Privacy Policy URL right here.
And do exactly the same thing with Terms of service.
So ask ChatGPT to generate it for you.
Pass it to a new document in Google Drive.
Then grab the link and paste it right here.
Then save changes and after that you'll be able to change the app mode from development to life.
So to wrap up this way, even without a verified business, you can still run WhatsApp agents.
And next we need to set up our credentials.
So click on the dropdown Create new credential.
And now as you can see we need to provide client ID and client secret.
So we need to grab our app details from meta Stromata for the connection.
So let me go back to our meta dashboard.
Now under settings.
Click on basic.
So here I can copy the app ID and I will pass this into the client ID field.
I'm going to simply copy it.
And paste it right here.
And the same for App Secret.
So I need to click show.
Then copy that and paste it to the client secret field.
It might ask you to login to your Facebook account again, so just simply reconnect.
Copy up secret.
Go back to LinkedIn and paste it into this field.
Hit save.
Perfect.
I can see the connection successful message, which means our workflow can now receive WhatsApp messages.
So I will close the credential window and test our setup.
So let me click Execute Workflow to start monitoring for incoming messages.
Now I'll switch to.
Now I will switch over to WhatsApp and send a simple message like Let's send a simple message on WhatsApp.
Hello.
And hit save.
All right.
Great.
Now we got some data.
Now let's pin this data.
All right, this data is pinned.
Now let's add our agent.
So click on add node.
Select AI agent.
Now we want to change the source for prompt to define below.
Now for the prompt we want to use the actual message we just sent.
So from the text object let's wrap body and drop it here.
Alright.
Perfect.
Now let's move on.
Now let's add our chat model.
I'm going to use GPT four because it's cheaper and sufficient for this use case.
So let's search for GPT four.
All right great.
Make sure you OpenAI account is connected.
Now let's execute the workflow again.
Now our agent prepared response for us.
So we have output.
We can use the response.
Now we are going to add another WhatsApp node.
But this time to send agents reply back to the user.
So search for WhatsApp.
We want to send message to select that.
All right.
Great.
Now let's expand.
What's a node?
So for the resource we have a message.
We want to send a message.
And now we need to provide the recipient's phone number.
We can find the number in context.
So let's grab it and drop it into this field.
All right.
Perfect.
Now what we want to do we want to provide a text body.
So this is output from agent.
All right.
Perfect.
Now we need to provide different type of credentials to connect our WhatsApp account to an A-10.
Pay attention that credentials are different from the WhatsApp node.
Let's add new credentials.
Create new credential.
And call it.
Call this node.
WhatsApp.
Reply.
Now we need two pieces of information here the access token and the business account ID.
So let me switch back to our dashboard and go to the WhatsApp API configuration.
So in the sidebar click on WhatsApp API setup.
And as you can remember here we can generate access token generate access token.
Now meta might ask you to log in to your Facebook account again.
So just simply reconnect.
Copy the access token.
Go back to N810 and paste it right here for the Business Account ID.
You can find that right here on the same page.
As you can see WhatsApp business account ID, simply copy it.
And paste it into this field.
And hit save.
Perfect.
So if everything is configured correctly, we should see a green connection status.
But if you get an error here, it's likely because the access token expired.
So just regenerate it in meta and update n810.
All right.
Great.
Now we are ready to give our agent long term memory.
So let's create our Supabase database.
So now let's jump into Supabase and create our database.
First thing I'm going to do is head over to Supabase.
Com.
So sign up now.
Provide email and password.
If you don't have an account yet just sign up for a free one.
It takes like 30s.
Once you are in, we need to create a new organization.
You don't need to have a company.
I already have one organization, but I'm going to show you how to create one from scratch.
So click on New organization.
Now give it a name.
Whatever we like.
Type leave as is personal.
And we are using free plan.
Confirm by clicking on this create button.
Cool.
So now that we got our organization setup, we need to create an actual project.
So click on New Project.
We're going to need to pick a name for the project.
So I'm going to say tutorials maybe WhatsApp.
Specific.
Now we need to set password.
And please make a note of this password.
This is really important.
You can leave the region as default because this is the closest one to you for the best performance.
Then click on Create New Project.
So it will take a minute to spin up.
You can see project status here.
Alright, great.
Now we have green light.
We can move on.
So our project is ready.
Now we need to create a place to store our memories for our AI agent.
So click on databases from the sidebar.
Then hit Create New table.
And right now it's empty because we don't have any table yet.
So first let's give it a name.
Let's say chat memory.
Now when you scroll down you will see columns.
Now here is where we set up what data we are actually going to store.
And now we can add new columns.
So first.
Let's add column.
Called message.
And set the type to text.
The next one will be recipient.
Which is also text.
And the last one.
Sender.
Text.
All right.
Perfect.
So you have message because we want to store the actual message content.
You have recipient column.
This is gonna store who is receiving the messages.
And finally the sender.
So this tells us who sent each message.
So basically the phone number of whoever is chatting with our agent.
We already got Createdat which tracks when each message was sent.
And that comes automatically, but I cool.
That all looks good.
Now hit save.
And supervises created our table.
now adding five columns to chart memory.
So to our table.
Let's wait a moment.
And there we go.
On the left hand side we can see our table.
So we've got our chart memory table ready to go.
Now that our database is set up let's connect it to our editing workflow.
So let's go back to our editing dashboard.
Here.
So back in our editing workflow.
Now we need to add Supabase as a tool.
So our agent can actually save memories.
So click on this Add Node button.
And let's search for Supabase.
And we want to use Supabase tool to add, get, delete and update data in a table.
Let's select that.
Now we want to leave the description as is as a resource.
We have a row and we want to as an operation, we want to create because we are adding new records to
our database.
So now we got to set up the credentials.
So let's add credential.
Click on create Credential.
And we need two things here the host URL and the service role secret.
So to grab this go back to your Superbase project.
Go to Project Settings.
Then click on data API under configuration.
Copy this URL.
This is the URL of your project.
Go back 2010 and paste it right here.
Now you need to provide service for secret, so look for API keys in settings.
By the way there is.
They recently moved this section.
So let's open it.
Now let's reveal our service roll secret and let's copy it.
Head back to N810.
And paste it in the service secret field.
It's safe.
All right.
Great.
Now close that pop up.
Now in your super base node.
You will see table name or ID.
So let's select the table we just created.
So check memory.
It should appear right now under data to send we need to map our fields.
But first let's get some test data to work with.
So save your workflow.
And let's test it out.
So I'm gonna send a text message something like.
First I'm gonna execute workflow and send the message.
My name is Damien.
Great to see if it triggers.
Let's see a response.
Nice to meet you, Damien.
How can I assist you today?
And now if I click on the super base node, I can see that we've got the data from the inputs that we
can save.
So now we need to add some fields.
So first let's add message.
So let's select message which is string.
And for the field value let's expand WhatsApp sugar node.
And this is our body.
All right great.
Now let's add another field called sender.
All right.
And that's going to be the from field which is the phone number of whoever sent the message.
So you can find it here within the messages object.
Grab it.
Drop it into this field.
And the last one we want to add.
One more field.
So this will be a recipient.
And that's going to be your test number or or whoever's receiving the message.
You can find it uh in contacts WhatsApp ID.
Alright.
Great.
So now we've got message sender and recipient or mapped correctly.
Alright.
Perfecto.
Now as you can see we've got a tool created for saving messages.
Let's rename it.
To save message to make it clear.
But the problem is we need to tell our agent when to actually save stuff to memory.
Now let's add option and let's use System Message to put our prompt.
Now let's switch over to expression mode.
Let's expand it.
We are gonna add a system message here so we can write our instructions.
So I'm going to paste the prompt I prepared.
So what we've got here we've got role.
You are the assistant.
We are currently chatting with a user on WhatsApp.
We also have rules when the user sends a message check if they shared any important information that
should be saved in memory for later.
If yes, use the save memory tool to save it.
Do not tell the user that you are saving anything, just keep the conversation going.
As usual.
We also have tools, so use this tool to store any important facts shared by the user.
Summarize the information clearly and pass it to this tool.
Now this sentence is optional.
If you want your agent to tell users it's saving info.
You can change that.
Now we'll look at memories in a sec, but we also have important section.
So use this stored info when replying.
If something is already saved, don't ask for it.
Again, keep your responses natural and friendly.
So this is important.
We only want to store new facts, so if something is already in memory, we don't want it to keep us
asking the same questions over and over.
And now here is a cool trick.
So let's go to our super base node.
Since we are letting the AI decide what's important to save, we can also let it summarize the info
instead of just saving the raw message.
So in the super base node In the message field, we can let the model define this parameter.
So click on that.
So this lets the LM automatically generate what to save instead of just outputting the exact user message.
So choose define automatically by the model.
And now the AI will extract and summarize the important information before saving it.
Alright great.
Now let's test this out.
So let's save the workflow.
Now, before we give it a test and check if our Supabase save message node is working, let's disconnect
these two nodes.
So I'm going to remove the connection.
And I'm going to send a simple message on WhatsApp because I didn't pin the data.
So I don't have any data in the flow in this node.
So execute workflow.
Hello.
Send.
Now let's open this node.
I wanted to send the message again because I need to pass the message to our agent.
So now I'm going to.
Connect these two nodes.
Let's open our agent.
Now we need to prepare the reference the WhatsApp trigger node because this agent doesn't receive the
message directly.
So let's remove this JSON prompt.
And now within the messages object from the text object.
Let's grab Body field and drop it here into the prompt field.
Alright, great.
Now let's give it a test.
So save workflow.
Execute workflow.
And now let's send another message.
But this time let's say something like.
My name is Damien.
And before I send the message, just to remind you.
In our chat memory table, there is no information about my my name.
And now, when I send the message.
I can clearly see that the message has been passed to the save message node, but we can also double
check by going to executions.
Let's check the last execution.
Yes, as you can see on the screen.
This execution should save the record to memory.
So let's check our super base table.
Now when I refresh the table.
I can see that my name has been saved.
Alright, perfect.
I can see what got saved.
So send the info so the recipient number and the message which is the I summarized as user's name is
Damon.
Plus we also have the Createdat timestamp.
And now if I hop over to N810.
Go back to editor and open save message node.
I can see the same data.
So created at timestamp message darker message recipient and sender.
All right.
Perfect.
So now we are successfully saving important facts to our database.
Now that we are saving memories to our database, we need to retrieve them and pass them to our agent
so it can actually use that data.
Use that info.
So let's add another node between WhatsApp trigger and agent.
Let's search for spark again.
Select Supabase.
And we want to get many rows because we want our agent to fetch memories from the chat memory table.
And based on that generate response.
So select get many rows.
Make sure you are using the same credentials as for the safe message node.
For the resource you have a row.
Because we want to get rows, we want to get many rows.
Here you can also change it to create delayed getrow update and so on.
So in other words, because you want to read the data, not write it, we are using get operation.
Now here I've got an error because I'm using a wrong credentials.
So I'm going to change it to Supabase.
And now I'm able to select the table I created.
So chat memory.
So select the table.
Now you want to return all records.
And now this is very important because we don't want all records from everyone.
We only want memories for the specific person who is chatting.
We need to add a filter condition.
So add condition.
For the field name.
Select sender as a condition.
Let's choose equals.
And for the fieldvalue, let's pass in the sender's phone number from our WhatsApp note, which you
can find in the contacts object.
So wha.
Alright, perfect.
Now let's test it.
So let's execute step.
And now you should see on the right side that one item has been retrieved.
So the message user's name is Damien.
Now let's send another test message.
So I'm going to save the workflow.
Just moving this note.
I'm going to save it.
Test workflow.
Let's say I like dogs.
Hit send.
There is the new record saying that user likes dogs.
And now when I test the Get Memories tab.
So let's press on it.
And I edit it.
We should see two items.
So two records the same as in the chat memory table in Supabase.
Perfect.
Now we've got a user's name is Damon and user likes dogs.
Cool.
Now, before we pass this to our agent, I want to clean up the data.
So we don't need all those database fields, just the actual messages and when they were sent.
So to do this we need to add an aggregate node after SQL base get memory node.
So first let's rename this node to get memory.
And now let's add aggregate node.
Search for aggregate.
To combine a field from many items into a list in a single item.
And now we want to choose all item data into a single list.
And for the output field name, let's call it chat memory.
40.
Four include select specific fields.
And type just message and Createdat.
Because that are the only fields we need from our table.
Message and Createdat.
We don't need the data from the rest of the columns.
And now when we test this step.
So execute step.
And edit it.
You can see that now we have a nice clean data.
Just the conversation memories and timestamps.
Now let's go back to our agent node.
Because we need to pass both the new user message and the retrieved memories.
So for the prompt.
Let's expand it.
For the memories we are gonna pass in our conversation.
Memories from the aggregate node.
From the aggregate node.
Let's grab chat memory and drop it here.
Now on the right you can see it's returning a JSON object, but our agent needs text.
So click on the chat memory field, type dot two and select JSON string.
So this converts the object into text format as you can see.
So now we've got the message dates and the actual message in the format.
Our agent can understand.
Now let's close this up and test the entire workflow.
All right, let's hit save.
Now I need to unpin data.
So we can send a fresh data into the flow.
So let's open it.
And now let's save execute workflow.
And I'm going to send a simple message.
What's my name.
Let's see the output.
The name is Damien.
How is it going today.
So as you can see it's working.
It remembers my name.
Now let me turn the workflow to active to active mode so we can do more testing.
Confirm.
Let's ask, do I like dogs or cats?
Let's see the executions.
Are great.
Now let's check the response.
You mentioned that you like dogs.
Do you have a favorite breed or any dog related stories to share?
So it remembers that I like dogs.
So as you can see, we've got long term memory working.
It's storing info in Supabase and retrieving it to generate responses.
Let's give it a final test.
I'm going to ask I'm going to say I like sushi.
All right.
This information should be saved in our table.
Use it like sushi.
Let's ask, what food do I like?
Like sushi.
Do you have a favorite type of sushi?
All right.
Perfect.
Now, throughout this conversation, I ask a bunch of questions.
But the only really important info from our conversation was my name.
And also that I like dogs and like sushi.
So all these three facts are saved in our chat memory table.
So the agent was able to filter out.
all the random sentences.
It only stored the actual important facts because that's what we told it to do in our system message.
So in our prompt.
Cool.
Now your agent is able to save important information from conversations into memory and retrieve it
along with a new user's prompts to generate personalized responses.
This really changes how your WhatsApp agent works because it can remember stuff.
For example, if you are building this type of solution for your own business or your client's business,
it can help build a relationship with customers.
It can be super useful if you are trying to build a kind of a second brain and chat with your agent
through WhatsApp.
Of course, you can switch to Telegram or Slack if you want.
Yes, adding long term memory with Supabase makes it way more solid and production ready.
I hope this was helpful and see you in the next tutorial.