Misbah17311/financial-intelligence-agent
Financial Intelligence Agent
A production-grade multi-agent AI system for financial Q&A. Combines structured SQL data (2,100 company records) with hybrid semantic search over 110K+ news articles, protected by a layered guardrail system.
Built with LangGraph, FastAPI, ChromaDB, DuckDB, and a custom HTML/CSS/JS frontend.
Architecture Diagram
flowchart TB
subgraph Frontend["Frontend ยท HTML/CSS/JS"]
UI[Chat Interface]
end
subgraph API["FastAPI Backend"]
direction TB
EP["/api/query endpoint"]
subgraph Guardrails["๐ก๏ธ Input Guardrails"]
G1[Input Length Check]
G2[PII Detection]
G3[SQL Injection Scanner]
G4[Prompt Injection Detector]
G5[Topic Relevance Filter]
end
subgraph AgentPipeline["๐ค LangGraph Agent Pipeline"]
Planner["๐ง Planner Agent"]
Retriever["๐ Retriever Agent"]
Analyst["๐ Analyst Agent"]
Critic["โ
Critic Agent"]
end
subgraph OutputGuardrails["๐ก๏ธ Output Guardrails"]
OG1[Response Validation]
end
end
subgraph DataLayer["Data Layer"]
DuckDB["๐๏ธ DuckDB ยท 2,100 records ยท SQL"]
ChromaDB["๐ฎ ChromaDB ยท 110K chunks ยท Vector"]
BM25["๐ BM25 Index ยท 110K chunks ยท Keyword"]
end
subgraph Retrieval["Hybrid Retrieval"]
VS[Vector Search]
KS[Keyword Search]
RRF[Reciprocal Rank Fusion]
RE[Cross-Encoder Reranker]
end
subgraph LLM["LLM Provider ยท Swappable"]
OpenAI["OpenAI GPT-4o-mini"]
Anthropic["Claude Sonnet"]
Groq["Llama 3.3 70B"]
end
UI -->|POST /api/query| EP
EP --> G1 --> G2 --> G3 --> G4 --> G5
G5 -->|All checks passed| Planner
G5 -->|Blocked| UI
Planner -->|Execution plan| Retriever
Retriever -->|SQL queries| DuckDB
Retriever -->|Search queries| Retrieval
ChromaDB --> VS
BM25 --> KS
VS --> RRF
KS --> RRF
RRF --> RE
RE -->|Top results| Retriever
Retriever -->|Retrieved data| Analyst
Analyst -->|Draft answer| Critic
Critic -->|APPROVED| OG1
Critic -->|REVISE| Analyst
OG1 --> UI
Planner -.->|LLM calls| LLM
Analyst -.->|LLM calls| LLM
Critic -.->|LLM calls| LLMGuardrails โ Safety Mechanisms
The system implements a layered defense with 6 guardrails (5 input + 1 output) that run on every query. Blocked queries never reach the LLM โ saving cost and preventing abuse.
How Guardrails Work
- Sequential short-circuit: Input checks run cheapest-first and stop on the first failure โ no LLM calls wasted
- Visual feedback: The UI displays guardrail status as colored pills (green โ = passed, red โ = blocked) on every response
- SQL layer protection: DuckDB queries are additionally restricted to
SELECT/WITH/EXPLAINonly โ no DDL/DML ever reaches the database - Zero-cost blocking: Rejected queries return instantly with the guardrail name and a user-friendly explanation
Quick Start
Prerequisites
- Python 3.10+
- An OpenAI API key (or Anthropic/Groq โ see Swapping LLMs)
1. Clone & Setup Environment
git clone <repo-url>
cd financial-intelligence-agent
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt2. Configure API Key
cp .env.example .env
# Edit .env and add your OPENAI_API_KEY3. Run Data Ingestion
python setup.pyDownloads ~110K financial news articles from HuggingFace, generates company fundamentals for 105 companies (7 sectors, 20 quarters), and builds all indexes (DuckDB, ChromaDB, BM25). Takes ~15โ20 min on first run.
4. Start the Application
# FastAPI (recommended โ full-featured UI with guardrails)
uvicorn src.api:app --host 0.0.0.0 --port 8000
# Or Streamlit (simpler alternative)
streamlit run src/ui/app.pyOpen http://localhost:8000 in your browser.
Swapping LLMs
Change the provider in .env โ no code changes needed:
LLM_PROVIDER=anthropic
ANTHROPIC_API_KEY=sk-ant-...
LLM_MODEL= # leave blank for defaultsProject Structure
โโโ frontend/ # production web UI
โ โโโ index.html
โ โโโ static/
โ โโโ style.css # dark theme, responsive
โ โโโ app.js # chat logic, guardrail display
โโโ src/
โ โโโ api.py # FastAPI backend + endpoints
โ โโโ config.py # central config (reads .env)
โ โโโ llm.py # LLM factory (provider swapping)
โ โโโ guardrails.py # 6-layer safety system
โ โโโ logger.py # structured logging
โ โโโ data_platform/
โ โ โโโ ingest.py # data download + processing
โ โ โโโ duckdb_store.py # SQL database for financials
โ โ โโโ chroma_store.py # vector store for embeddings
โ โ โโโ bm25_store.py # BM25 keyword index
โ โโโ retrieval/
โ โ โโโ hybrid.py # vector + BM25 โ RRF โ reranking
โ โโโ tools/
โ โ โโโ agent_tools.py # LangChain tools
โ โโโ agents/
โ โ โโโ graph.py # multi-agent LangGraph pipeline
โ โโโ ui/
โ โโโ app.py # Streamlit alternative UI
โโโ evaluation/
โ โโโ evaluate.py # automated eval (LLM-as-judge)
โ โโโ test_queries.json # 20 test queries
โ โโโ results/ # evaluation output
โโโ data/ # generated by setup.py
โโโ setup.py # one-command data pipeline
โโโ requirements.txt
โโโ .env.example
โโโ ARCHITECTURE.md
โโโ README.mdData Sources
Structured data: 105 companies across 7 sectors (Technology, Healthcare, Finance, Energy, Consumer, Industrial, Communication) over 20 quarters (Q1 2020 โ Q4 2024).
Unstructured data: ashraq/financial-news-articles, oliverwang15/news_with_gpt_instructions, twitter-financial-news-sentiment, nickmuchi/financial-classification.
Evaluation Results
20 curated queries tested across SQL lookups, comparisons, aggregations, trend analysis, sentiment, and multi-hop reasoning:
python evaluation/evaluate.pyKey Design Decisions
- Hybrid retrieval: Dense embeddings + BM25 merged via Reciprocal Rank Fusion, re-scored by a cross-encoder. Catches both semantic and exact-match relevance.
- Multi-agent pipeline: Planner โ Retriever โ Analyst โ Critic. The Critic catches hallucinations before answers reach the user.
- Guardrails-first: Input validation runs before any LLM call. Blocked queries cost zero tokens.
- Local embeddings:
all-MiniLM-L6-v2on CPU โ no API key needed for the embedding pipeline. - DuckDB: Embedded SQL, no server required, read-only at runtime for safety.
Demo Video
Link to demo video
The demo covers:
- Normal flow: SQL queries, semantic search, complex multi-step analysis
- Guardrail activation: SQL injection, prompt injection, PII detection, off-topic blocking โ all caught with visual feedback
