Make OpenAI Citation for File Retrieval RAG n8n Workflow Template

n8n workflow template · 12 nodes · OpenAI

Make OpenAI Citation for File Retrieval RAG is a free n8n workflow template that starts when a chat message is received and works with OpenAI. It has 12 nodes, 3 of them AI nodes (Window Buffer Memory, Chat Trigger and OpenAI). To run it, you add your own credentials for OpenAI API.

At a glance

Trigger
Chat Trigger (Create a simple Trigger to have the Chat button within N8N)
Nodes
12
Apps
OpenAI
AI nodes
3
Credentials
1 type
Category
AI agents and LLM tools

Apps and integrations

  • OpenAI

Node types used

  • Aggregate
  • Window Buffer Memory
  • Chat Trigger
  • OpenAI
  • HTTP Request
  • Split Out
  • Edit Fields (Set)
  • Markdown
  • Code

How it works

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Within N8N, there will be a chat button to test

A file retrieval request contains a lot of information, and we want only the text that will be substituted and the file name.

With the last three splits, we may have many citations and texts to substitute. By doing an aggregation, it will be possible to handle everything as a single request.

This simple code will take all the previous files and citations and alter the original text, formatting the output. In this way, we can use Markdown tags to create links, or if you prefer, we can add an HTML transformation node.

Retrieving all thread content is necessary because the OpenAI tool does not retrieve all citations upon request.

Setup notes

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Make OpenAI Citation for File Retrieval RAG

Use case

In this example, we will ensure that all texts from the OpenAI assistant search for citations and sources in the vector store files. We can also format the output for Markdown or HTML tags.

This is necessary because the assistant sometimes generates strange characters, and we can also use dynamic references such as citations 1, 2, 3, for example.

What this workflow does

In this workflow, we will use an OpenAI assistant created within their interface, equipped with a vector store containing some files for file retrieval.

The assistant will perform the file search within the OpenAI infrastructure and will return the content with citations.

  • We will make an HTTP request to retrieve all the details we need to format the text output.

Setup

Insert an OpenAI Key

How to adjust it to your needs

At the end of the workflow, we have a block of code that will format the output, and there we can add Markdown tags to create links. Optionally, we can transform the Markdown formatting into HTML.

by Davi Saranszky Mesquita

Setup

  • Configure OpenAI Key

In this step, we will use an assistant created within the OpenAI platform that contains a vector store a.k.a file retrieval

Nodes in this workflow

The 12 nodes in file order (sticky notes excluded). Branches and connections determine execution order in n8n:

  1. Aggregate (Aggregate)
  2. Window Buffer Memory (Window Buffer Memory)
  3. Create a simple Trigger to have the Chat button within N8N (Chat Trigger)
  4. OpenAI Assistant with Vector Store (OpenAI)
  5. Get ALL Thread Content (HTTP Request)
  6. Split all message iterations from a thread (Split Out)
  7. Split all content from a single message (Split Out)
  8. Split all citations from a single message (Split Out)
  9. Retrieve file name from a file ID (HTTP Request)
  10. Regularize output (Edit Fields (Set))
  11. Optional Markdown to HTML (Markdown)
  12. Finnaly format the output (Code)

Credentials you need

  • OpenAI API – used by Get ALL Thread Content, OpenAI Assistant with Vector Store and Retrieve file name from a file ID

Configure your own credentials after importing. Create each credential in n8n, then select it in the node.

How to import into n8n

  1. Download the JSON file with the button above.
  2. In n8n, open a new workflow, select the ... menu and choose Import from File. You can also paste the JSON straight into the editor canvas.
  3. Open each node that shows a warning and select or create its credential.
  4. Read the notes on the canvas, test the workflow with sample data, then activate it.

Do you need somewhere to run it? Read our guide to self-hosted n8n.

Need this customised?

We can adapt this workflow to your apps, data and process, then deploy and support it.

Contact Us