OpenAI Chat: remember conversation to continue string without resetting

Hello, I set up a basic workflow where an SMS is sent to my system, and my N8N automation sends the person’s SMS to ChatGPT and ChatGPT sends back an auto-response.

I was hoping that N8N would have a memory feature like Zapier, but I can’t seem to get this part to work.

I would love to be able to have this automation have a seamless conversation with my contacts, and remember the previous messages to feed into the new messages.

Hi @Michael_Rapino - welcome to the community :tada:

Thanks for going into details about what you’re looking for - at the moment, n8n wouldn’t have this type of built-in storage that can be easily accessed from a workflow. You would currently need to have this persist in an external data store like a database.

I’m moving this over to the feature request section of our forums, which our product team looks at regularly. Don’t forget to also vote on your own thread up at the top near your first post :slight_smile:

Thanks for the quick reply. So, perhaps and Airtable databas since N8N also integrates with that.

As far as the memory key, is that something I’d have to recreate somehow or do you think everything would run properly without it?

1 Like

Are you self-hosting n8n? If so, there’s some community nodes like this one that might make things a little easier for you :slight_smile:

1 Like

Thanks so much! Yes, I am self-hosting. I’ll definitely look into this particular solution. The key factor for me if whether I would need long-term storage of the conversations, so if a contact reached out again, I had a reference-point. However, after the first interaction with a virtual chat, we want to get a contact connected with a human ASAP, so virtual key-value storage might be all I need.

1 Like

So, I tried figuring out the community node KV, but decided to self-install NocoDB instead. I got really far with my workflow, but got stuck and was hoping for a nudge. I’m not sure there is a node for what I need, or if I have to custom code something.

Here is my workflow so far:

The flow is:

  1. Webhook receives user message
  2. NocoDB pulls all previous message from the same user. I am filtering by a user id column. This all works so far.
  3. Since the NocoDB node returns objects, and I need all previous messages to be a single variable to use in the ChatGPT node, I tried using the “combine all messages” node, but when I use the “data” merge tag, it returns objects that can’t be read by the ChatGPT node.

This is where I am stuck. I have all past messages being pulled, but I need to add a text block into the prompt so that ChatGPT can reference them for context, so the conversation seems natural to the contact.

If anyone is interested, this code will create a master field by concatenating all previous data:

// Loop over input items and add conversation history
for (const item of $input.all()) {
  let fullConversationHistory = '';
  
  for (let i = 0; i < item.json.data.length; i++) {
    const userMessage = item.json.data[i]["User Message"];
    const aiResponse = item.json.data[i]["AI Response"];
    const conversationEntry = `Message # ${i}: User Message: ${userMessage}\nAI Response: ${aiResponse}\n\n`;
    
    fullConversationHistory += conversationEntry;
    
    // Add conversation history to the current item
    item.json.data[i].conversationHistory = fullConversationHistory;
    
  }
  
  // Add the masterConversationHistory to the top level of the item
  item.json.masterConversationHistory = fullConversationHistory;

}

// Return the modified JSON array
return $input.all();

Thanks a lot for sharing @Michael_Rapino !

Just FYI, we are currently working on adding property support to build applications that are powered by LLMs by adding LangChain support. That will make the above much simpler. Here the feature request:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.