I’m attaching my workflow scenario for context. I want to achieve a setup where the entire chat history is passed to the AI when generating responses.
Currently, every time I send a new message through the WhatsApp module, it starts a new conversation, ignoring the previous context of the chat. I want to change that so the AI can access and respond based on the full chat history. Any tips or guidance would be greatly appreciated!
Thanks!
Here is the workflow. Only the Chat Memory is important.
n8n version: 1.69.2
Database (default: SQLite): QLite
n8n EXECUTIONS_PROCESS setting (default: own, main): own, main
Running n8n via (Docker, npm, n8n cloud, desktop app): self-hosted in google cloud
You should be able to change your postgress chat memory to do this.
Change it from Session ID: Connected Chat Trigger Node to Session ID: Define below
Then have some logic between the trigger and agent to find/generate an id key.
Something to note here is it wouldn’t grab anything before you implimented that logic
If possible, you could also fetch the previous messages with the WhatsApp api between the trigger and agent and pass that in, but an not sure if that operation is available.
@liam Well ultimately what I want to do is store the entire chat that has come from one number (not per session meaning as soon as some time passes or what not) just like within a ChatGPT chat where the entire chat is given as context until the token window is maxed out.
I am not familiar with the WhatsApp api, but look and see if they have an api method for fetching an entire conversation and use the http node to do that.
Otherwise, you will need to store it as the conversation happens one message at a time with your own logic whenever you receive a message or send a message
The Postgres Chat Memory node by default expects sessionId to contain the ID. You can either switch to “Define below” option and point to the WhatsApp property that has the number (no idea if it does as I never used WhatsApp API either) or use Set node in between WhatsApp and AI Agent insuring sessionId is present there and contains the phone number in additions to another property that holds the text to be passed to AI Agent.
I now selected define below and used json.id and after each of the 3 blocks used a set variables block to define the id. The first image is from the chat input within N8N. THe last 2 are from the schedule trigger - I need that one otherwise if I don’t use the Supabase account for 2 weeks it deactivates on the free plan - this in theory is a cool work around.
I only fear that there will be issues since it waits for the inputs of multiple paths here or is this not a problem? Has been in the future.