Dynamically injecting context into AI agent conversation

The problem/error/question:

I am ready to deploy my first n8n built AI assistant. This assistant will be embedded into a CRM application and I need to be able to pass context from the CRM contact record into the AI agent so that when my user is communicating with the AI agent, the AI agent already has an understanding as to which record the user is communicating about, and the agent can use the data fields from that record to make decisions

Is there a way to do this?

Information on your n8n setup

  • n8n version: 1.123.4
  • Database (default: SQLite): Postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: Ubuntu

Hey @Michael_Barnett !

Hmm… I am thinking at this 2 ways:

If your workflow is trigggered from the CRM (for exampe via webhook or API call) and you already havve the contact record, fetch some data about that contact(email, name etc)before the Ai Agnt node.

In the AI Agents human/system prompt, you could add context like :

You are helping the user with this CRM contact:

ID: {{ $json.contactId }}
Name: {{ $json.name }}
Email: {{ $json.email }}
Status: {{ $json.status }}

Use this information when answering questions and making decisions.
User message: {{ $json.userMessage }}

Or if using the Chat Trigger node (embebdded chat) you can:

Chat triggger thenuse a node (HTTP Request / DB / CRM node) to load the contact reccord based on some identiier passsed from your CRm and add feed this to prompts …

Cheers!

Thank you for the feedback @Parintele_Damaskin. Since we will be embedding the chat panel directly into the CRM record, I was looking to pass the CRM record data either as a URL parameter to the chat widget URL or pass some metadata to the workflow with each chat.

This can be done with N8nChatUI, but we want to test with the standard n8n UI.

I found the solution (clearly I cannot read today). the solution is in the chat options.

There is a metadata key that can be used to pass stringified data to the context.

https://www.npmjs.com/package/@n8n/chat

2 Likes

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