I’m working on a project where I need to get specific info from customer messages using AI in n8n. I want the AI to pick out things like the customer’s name and where they live. Then I need it to give me the answer in a neat JSON format.
Here’s what I’m hoping to get:
{
"ai_response": "Hello! What should I call you?",
"extracted_info": [
{
"item": "Location",
"data": [{ "text": "Miami" }]
},
{
"item": "Customer",
"data": [{ "text": "Sam" }]
}
]
}
What’s the best way to set this up in n8n? How do I tell the AI what to look for and how to format its answer? I really need the output to be consistent and easy for a computer to read. Any advice would be great!
hey, i’ve used n8n too. using the openai node with a strict prompt worked for me. i rephrased instructons to ‘extract customer name and locaton’ and then parsed the json output. might need minor tweaks!
To achieve this in n8n, you’ll want to use the OpenAI node with a custom prompt. Structure your prompt to clearly instruct the AI on what information to extract and how to format the response. Here’s a basic approach:
Set up an OpenAI node with a system message that defines the task and output format.
In the user message, include the customer’s input.
Use JSON parsing nodes to process the AI’s response.
For the prompt, try something like:
‘Extract the customer’s name and location from the following message. Respond in this exact JSON format: {"ai_response": "Your response here", "extracted_info": [{"item": "Location", "data": [{"text": "location"}]}, {"item": "Customer", "data": [{"text": "name"}]}]}’
This should guide the AI to produce the structured output you need. You may need to fine-tune the prompt based on your specific use case and the variety of customer inputs you receive.
I’ve tackled a similar challenge in my work, and here’s what worked well for me:
Using the OpenAI node in n8n is definitely the way to go. The key is crafting a precise prompt that instructs the AI exactly what to extract and how to format it.
I found success with a two-step approach. First, I used a ‘system’ message to set the context and output format. Then, I passed the customer’s message as the ‘user’ input.
For the system message, I used something like this:
‘You are an AI assistant tasked with extracting customer information. Always respond in the specified JSON format, including an AI response and the extracted info.’
This setup consistently produced the structured JSON output I needed. It took some trial and error to get the prompt just right, but once dialed in, it worked like a charm.
Remember to validate the JSON output in n8n to catch any formatting issues before passing it to the next steps in your workflow.