Added Citation System To My n8n RAG Agent (Now Tells Me Which Document Every Answer Came From) ๐Ÿ”ฅ
RAG agent gave great answers. Client asked: 'Which contract did that come from?' Agent couldn't say. Added citation tracking. Now every answer includes source document and page number.
THE PROBLEM WITH BASIC RAG:
Agent answers questions. Doesn't track source documents. Can't verify answers. Can't cite sources. Legal teams need sources for every claim.
THE SOLUTION:
Build citation tracking into RAG pipeline. Capture document metadata. Pass through embeddings. Return with answers.
THE EXTENSION:
ORIGINAL RAG FLOW:
Document โ†’ Parse โ†’ Split โ†’ Embed โ†’ Store โ†’ Query โ†’ Answer
EXTENDED RAG FLOW:
Document โ†’ Parse โ†’ Add Metadata โ†’ Split (preserve metadata) โ†’ Embed (with metadata) โ†’ Store with source โ†’ Query โ†’ Retrieve with source โ†’ Answer with citation
THE NEW METADATA STRUCTURE:
document_id, document_name, document_type, page_number, section, last_updated, owner
STORED WITH EACH CHUNK:
Every text chunk in vector store includes metadata. When agent retrieves chunks, metadata comes with it.
AGENT TOOL ENHANCEMENT:
Modified 'Search Documents' tool to return text PLUS source information.
Agent receives: Relevant text, source document name, page numbers, confidence score.
Agent includes in answer: 'According to the Service Agreement (page 7), the cancellation policy is...'
THE IMPLEMENTATION:
NODE CHANGES:
1. After Document Parser: SET node creates metadata object
2. Text Splitter: Configure to preserve metadata in chunks
3. Vector Store Insert: Include metadata with embeddings
4. Vector Store Query: Return metadata with results
5. Agent Tool: Format metadata into citation
CITATION FORMAT OPTIONS:
Simple: (Source: filename.pdf)
Detailed: (Source: filename.pdf, page 7, section 3.2)
Legal: (Service Agreement v2.1, executed 2024-01-15, section 3.2, page 7)
THE RESULTS:
- Base RAG: Answers without sources
- Extended RAG: Every answer cited
- Legal team: Approved (had rejected basic RAG)
- Audit compliance: 100% traceable
- User trust: Significantly increased
REAL EXAMPLE:
Question: 'What is our refund policy for enterprise customers?'
Basic RAG: 'Enterprise customers can request refunds within 30 days with manager approval.'
Citation-enabled RAG: 'Enterprise customers can request refunds within 30 days with manager approval. (Source: Enterprise Terms of Service v3.2, page 12, section 4.3, last updated 2024-03-01)'
Can verify. Can audit. Can trust.
CONFIGURATION:
Metadata schema: Define in SET node. Consistent across documents.
Vector store: Qdrant metadata filtering. Query specific document types or date ranges.
THE LESSON:
RAG without citations is demo. RAG with citations is production. Legal and compliance require source tracking.
TEMPLATE:
Complete citation-enabled RAG system. Metadata schema, vector store config, agent tool with citations.
How do you handle source tracking in RAG?
18
18 comments
Duy Bui
7
Added Citation System To My n8n RAG Agent (Now Tells Me Which Document Every Answer Came From) ๐Ÿ”ฅ
AI Automation Society
skool.com/ai-automation-society
Learn to get paid for AI solutions, regardless of your background.
Leaderboard (30-day)
1
+7867
2
+7601
4
+4287
5
+4052
Powered by