Animated data flow diagram

AI Agent: OpenAI RAG Citation Formatter & Retriever

Version: 1.0.0 | Last Updated: 2025-05-16

Integrates with:

OpenAI

Overview

Unlock Verifiable, AI-Generated Content with Formatted Citations

This n8n AI agent works with your OpenAI Assistant that's set up for Retrieval Augmented Generation (RAG) using your own documents in a vector store. When your Assistant responds using information from these files, this workflow kicks in to properly handle and display the citations.

It meticulously extracts the raw citation data provided by the OpenAI Assistant, fetches the corresponding original filenames, and then intelligently replaces the generic citation placeholders in the AI's output with clear, formatted references (e.g., replacing 【1†source】 with _(sourceA.pdf)_). This makes your AI-generated content more transparent and trustworthy by directly linking assertions to their sources. You can easily customize the citation format in the final step, for example, to create clickable Markdown links or convert to HTML.

Key Features & Benefits

  • Seamless OpenAI Assistant RAG Integration: Works directly with OpenAI Assistants that use file retrieval from vector stores.
  • Automated Citation Processing: Automatically extracts and processes complex citation data from the Assistant's responses.
  • Accurate Source Linking: Retrieves original filenames for each citation, ensuring references are precise.
  • Inline Formatted Citations: Embeds readable, formatted citations directly into the AI-generated text.
  • Customizable Output: Default formatting uses Markdown (e.g., _(filename.pdf)_). Easily adapt the code to change styles or use the optional node to convert to HTML.
  • Enhanced Trust & Verifiability: Provides clear traceability for AI-generated claims back to your source documents.
  • Handles API Nuances: Includes steps to fetch full thread details from OpenAI to ensure all citation information is captured correctly.

Use Cases

  • Generating research summaries or reports with verifiable source links from your document corpus.
  • Creating internal documentation or FAQs where answers are directly backed by specific source files.
  • Automating content creation for blog posts or articles, ensuring proper attribution to materials within your vector store.
  • Building Q&A bots that cite the exact documents used to formulate their answers, enhancing user trust.
  • Streamlining the process of adding references to any AI-generated text based on uploaded files.

Prerequisites

  • An n8n instance (Cloud or self-hosted).
  • OpenAI API Key with access to Assistants API (v2 recommended).
  • An existing OpenAI Assistant configured with File Retrieval (RAG) enabled and a populated Vector Store containing your source files.

Setup Instructions

  1. Download the n8n workflow JSON file and import it into your n8n instance.
  2. Configure the 'OpenAI Assistant with Vector Store' node: select or create your OpenAI credentials and choose your pre-configured OpenAI Assistant ID. Ensure this Assistant has File Retrieval enabled and a vector store with files.
  3. Verify OpenAI Credentials: Ensure your OpenAI credentials are also selected in the 'Get ALL Thread Content' HTTP Request node and the 'Retrieve file name from a file ID' HTTP Request node.
  4. Customize Citation Format (Optional): In the 'Finnaly format the output' Code node, you can modify the JavaScript code (saida = saida.replaceAll(i.text, " _("+ i.filename+")_ ");) to change how citations appear. For example, to make it a Markdown link: "[source: " + i.filename + "](#)" (you'd need a way to make the link functional, or just for display).
  5. HTML Output (Optional): If you need HTML, enable the 'Optional Markdown to HTML' node and ensure its input ({{ $json.output }}) is correct.
  6. Test the Agent: Activate the workflow. Use the 'Create a simple Trigger to have the Chat button within N8N' (Chat Trigger node) to send a query to your Assistant that should trigger file retrieval and citations.
  7. Observe Output: The final output of the 'Finnaly format the output' node (or 'Optional Markdown to HTML' if enabled) will contain the Assistant's response with formatted citations.

Tags:

AI AgentOpenAIRAGCitation GenerationContent AutomationVector StoreDeveloper ToolKnowledge ManagementLLM

Want your own unique AI agent?

Talk to us - we know how to build custom AI agents for your specific needs.

Schedule a Consultation