Retrieval-Augmented Generation (RAG) pipelines are revolutionizing automated financial analysis by combining the retrieval of relevant data with the generative power of large language models (LLMs). RAG enables analysts and developers to automate complex workflows—like extracting insights from earnings reports, generating summaries, or answering compliance queries—with accuracy, traceability, and explainability.
As we covered in our Ultimate Guide to RAG Pipelines, this technology is transforming how organizations interact with their data. In this deep dive, we’ll focus specifically on how to build and deploy RAG pipelines for automated financial analysis, complete with practical templates and code you can adapt to your own projects.
This tutorial is for developers, data scientists, and financial analysts looking to automate document-centric financial tasks using RAG. We’ll cover the full workflow—from ingestion and embedding to retrieval, generation, and evaluation.
Prerequisites
- Python 3.10+ installed
- Pip for package management
- Haystack v2.x (for pipeline orchestration)
- FAISS (for vector search, or alternative supported by Haystack)
- OpenAI API key (or Hugging Face key for open-source models)
- Basic knowledge of Python and REST APIs
- Sample financial documents (e.g., PDFs, 10-K filings, earnings calls transcripts)
- Optional: Docker for isolated deployment
1. Set Up Your Development Environment
-
Create a new project directory:
mkdir rag-financial-analysis && cd rag-financial-analysis
-
Create and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activate
-
Install Haystack, FAISS, and dependencies:
pip install farm-haystack[faiss] openai python-dotenv
Note: If you want to use open-source LLMs, install
transformersandtorchas well:pip install transformers torch
-
Set your API keys as environment variables (recommended):
echo "OPENAI_API_KEY=sk-..." > .envLoad these in your Python code with
python-dotenv.
2. Prepare and Ingest Financial Documents
-
Gather your source documents:
- Example: Download 10-K filings from the SEC EDGAR database or use your own PDFs/CSVs.
-
Convert PDFs to text (if needed):
pip install pdfplumber
import pdfplumber def pdf_to_text(pdf_path): with pdfplumber.open(pdf_path) as pdf: return "\n".join(page.extract_text() for page in pdf.pages if page.extract_text()) text = pdf_to_text("sample_10k.pdf") with open("sample_10k.txt", "w") as f: f.write(text) -
Chunk the text for embedding:
def chunk_text(text, chunk_size=512, overlap=50): words = text.split() chunks = [] for i in range(0, len(words), chunk_size - overlap): chunk = " ".join(words[i:i + chunk_size]) chunks.append(chunk) return chunks chunks = chunk_text(text)Screenshot description: A terminal window showing successful PDF-to-text conversion and chunking output.
3. Embed and Index Documents with Haystack + FAISS
-
Initialize Haystack’s FAISS DocumentStore:
from haystack.document_stores import FAISSDocumentStore document_store = FAISSDocumentStore(embedding_dim=1536, faiss_index_factory_str="Flat")Tip: For a deep comparison of embedding models, see Comparing Embedding Models for Production RAG.
-
Embed the text chunks:
from haystack.nodes import EmbeddingRetriever import os from dotenv import load_dotenv load_dotenv() openai_api_key = os.getenv("OPENAI_API_KEY") retriever = EmbeddingRetriever( document_store=document_store, embedding_model="text-embedding-ada-002", # OpenAI's embedding model api_key=openai_api_key, model_format="openai" ) docs = [{"content": chunk, "meta": {"source": "sample_10k.pdf"}} for chunk in chunks] document_store.write_documents(docs) document_store.update_embeddings(retriever)Screenshot description: Python script output showing successful embedding and FAISS index creation.
4. Build the RAG Pipeline for Financial Q&A
-
Define the pipeline using Haystack’s API:
from haystack.pipelines import Pipeline from haystack.nodes import PromptNode generator = PromptNode( model_name_or_path="gpt-3.5-turbo", # or "gpt-4" api_key=openai_api_key, default_prompt_template="financial_analysis", model_kwargs={"temperature": 0.2} ) financial_prompt_template = """ You are a financial analyst. Answer the following question using only the provided context from financial documents. If the answer is not present, say "Not found in the documents.". Context: {join(documents)} Question: {query} """ generator.prompt_templates["financial_analysis"] = financial_prompt_template pipeline = Pipeline() pipeline.add_node(component=retriever, name="Retriever", inputs=["Query"]) pipeline.add_node(component=generator, name="Generator", inputs=["Retriever"]) -
Query the pipeline:
question = "What was the company's net income in 2022?" result = pipeline.run(query=question, params={"Retriever": {"top_k": 5}}) print(result["answers"][0].answer)Screenshot description: Terminal output showing a generated answer with relevant context snippets.
5. Template: Automated Financial Summary Generation
You can use RAG not just for Q&A, but to generate summaries, risk assessments, or compliance checks. Here’s a template for generating a financial summary from your document set:
summary_prompt_template = """
You are a senior financial analyst. Read the following context from financial documents and generate a concise summary highlighting:
- Revenue, net income, and cash flow for the most recent year
- Notable risks or uncertainties mentioned
- Any changes in accounting policies
Context: {join(documents)}
Summary:
"""
generator.prompt_templates["financial_summary"] = summary_prompt_template
summary = pipeline.run(
query="Generate a financial summary.",
params={
"Retriever": {"top_k": 10},
"Generator": {"prompt_template": "financial_summary"}
}
)
print(summary["answers"][0].answer)
Screenshot description: Output displaying a structured financial summary with bullet points for revenue, risks, and accounting changes.
6. Evaluate and Monitor Your RAG Pipeline
- Manual validation: Compare generated answers/summaries against ground truth data from the documents.
-
Automated evaluation: Use Haystack’s built-in evaluation tools or custom scripts to measure accuracy, relevance, and hallucination rates.
from haystack.evaluation import EvalAnswers eval = EvalAnswers( pipeline=pipeline, queries=["What is the company's total assets in 2022?"], labels=[{"answers": ["$1,234,567,890"]}] ) results = eval.run() print(results)For advanced monitoring approaches, see How to Monitor RAG Systems: Automated Evaluation Techniques.
- Track pipeline performance over time: Log results and errors for ongoing improvement.
Common Issues & Troubleshooting
-
FAISS index errors: Ensure
faiss-cpuis installed. If you seeAttributeError: 'NoneType' object has no attribute 'write', check yourdocument_storeinitialization. -
Embedding API errors: Invalid API key or quota exceeded. Verify your
OPENAI_API_KEYand usage limits. -
Empty or irrelevant answers:
- Check chunk size—too large or too small can hurt retrieval quality.
- Increase
top_kin retriever parameters. - Ensure your prompt templates are clear and instructive.
- Slow performance: For large document sets, consider sharding or caching. See Scaling RAG for 100K+ Documents for advanced tips.
- Prompt injection or hallucination: Always instruct the LLM to answer using only retrieved context. Add explicit instructions in your templates.
Next Steps
- Expand your pipeline: Add more document types (e.g., CSVs, Excel), or integrate real-time data feeds.
- Deploy as an API: Wrap your pipeline with FastAPI or Flask for internal or client-facing applications.
- Experiment with open-source models: Use Hugging Face models for cost control or privacy. For a full walkthrough, see Building a Custom RAG Pipeline: Step-by-Step Tutorial with Haystack v2.
- Automate knowledge base creation: See Automated Knowledge Base Creation with LLMs for enterprise-scale strategies.
- Decide between RAG and pure LLMs: Review Choosing Between RAG and LLMs: A Decision Checklist for Enterprise Architects for guidance.
For a comprehensive overview of RAG, revisit our Ultimate Guide to RAG Pipelines. If you’re interested in applying RAG to internal knowledge management, see AI-Driven Knowledge Management: Building Searchable Internal Wikis with Retrieval-Augmented Generation.
