AI Document Q&A Agent with OpenAI, Pinecone & Langchain
Integrates with:
Overview
Unlock Instant, Cited Answers from Your Documents with this AI Agent
This n8n workflow transforms your static documents into an interactive knowledge base. It acts as an AI Agent with powerful document understanding and retrieval capabilities, often referred to as a Retrieval Augmented Generation (RAG) system.
How it Works:
- Ingestion: The first part of the workflow, triggered manually, fetches a document (e.g., from Google Drive). It then uses Langchain components to split the document into manageable chunks, generates semantic embeddings for each chunk using OpenAI, and stores these chunks and their embeddings in a Pinecone vector database. This creates a searchable index of your document's content.
- Question Answering: The second part is triggered by a chat input. When you ask a question, the AI Agent:
- Generates an embedding for your question using OpenAI.
- Queries the Pinecone vector store to find the most relevant document chunks based on semantic similarity.
- Feeds these relevant chunks as context to an OpenAI chat model (e.g., GPT-3.5-turbo or GPT-4).
- The LLM generates an answer based only on the provided document context.
- Finally, it returns the answer along with citations (e.g., file name, line numbers) indicating where in the original document the information was found.
This AI-driven automation is ideal for solopreneurs, founders, and technical teams needing to quickly extract insights, verify information, or build internal Q&A systems without laborious manual searching.
Key Features
- Automated Document Ingestion: Fetches and processes documents from sources like Google Drive.
- Advanced Text Splitting: Utilizes Langchain's
RecursiveCharacterTextSplitter
for optimal document chunking. - Semantic Embeddings: Leverages OpenAI's state-of-the-art models for generating meaningful document embeddings.
- Vector Storage & Retrieval: Integrates with Pinecone for efficient storage and fast similarity search of document chunks.
- Conversational Q&A (RAG): Employs OpenAI's chat models for natural language interaction and context-aware answers.
- Source Citations: Provides references to the original document sections, ensuring answer verifiability and trust.
- Langchain Powered: Built using robust Langchain nodes within n8n for a sophisticated AI pipeline.
- Customizable Knowledge Base: Easily adapt to different documents to create your own specialized Q&A systems.
Use Cases
- Building an AI to query internal knowledge bases (e.g., company wikis, technical manuals).
- Creating a chat interface for research papers or long-form articles to extract key information with sources.
- Developing customer support bots that answer questions based on product documentation.
- Analyzing legal documents or contracts for specific clauses and getting cited answers.
- Quickly finding specific information within large PDF or text-based files.
Prerequisites
- An n8n instance (Cloud or self-hosted).
- OpenAI API Key with access to an embedding model (e.g.,
text-embedding-ada-002
) and a chat model (e.g.,gpt-3.5-turbo
orgpt-4
). - Pinecone API Key and an existing Pinecone index. The index must be configured with 1536 dimensions to match OpenAI's
text-embedding-ada-002
model output. - Google Drive credentials configured in n8n for document fetching.
Setup Instructions
- Download the n8n workflow JSON file.
- Import the workflow into your n8n instance.
- Configure Credentials:
- OpenAI: In the 'Embeddings OpenAI', 'Embeddings OpenAI2', and 'OpenAI Chat Model' nodes, select or create your OpenAI credentials.
- Pinecone: In the 'Add to Pinecone vector store' and 'Get top chunks matching query' nodes, select or create your Pinecone credentials. Also, ensure you select your pre-configured Pinecone index name in both nodes.
- Google Drive: In the 'Download file' node, select or create your Google Drive credentials.
- Set Document Source: In the 'Set file URL in Google Drive' node, update the
value
field with the direct shareable URL of the Google Drive file you want to process. - Initial Document Ingestion (Setup Part 1):
- Manually execute the workflow once by clicking the 'Execute Workflow' button (or Test Workflow). This will trigger the first part of the flow: downloading the specified file, chunking it, creating embeddings, and inserting it into your Pinecone index.
- Note: Running this ingestion part multiple times with the same document might result in duplicate entries in Pinecone unless your Pinecone setup handles upserts or you clear the index beforehand.
- Chat with Your Document (AI Agent Part 2):
- The 'Chat Trigger' node provides a webhook URL and an interactive chat interface when you test or activate the workflow. Use this to ask questions to your indexed document.
- You can adjust the number of retrieved chunks in the 'Set max chunks to send to model' node (default is 4).
- Activate the workflow to make the Chat Trigger continuously available.
Want your own unique AI agent?
Talk to us - we know how to build custom AI agents for your specific needs.
Schedule a Consultation