Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

LLM-Powered Financial Data Analyst

An AI system that converts natural language questions into SQL queries, executes them on a financial dataset, and returns human-readable explanations.

Architecture

User → FastAPI → LLM → SQL → SQLite → LLM → Explanation

Stack

Layer Tech
Backend FastAPI + Uvicorn
LLM Groq (llama-3.3-70b-versatile)
Database SQLite via raw sqlite3
Data 60 stocks × 6 years = 360 rows
Frontend React/HTML

Dataset Schema — fundamentals table

Column Type Description
ticker TEXT Stock ticker symbol
year INTEGER Fiscal year (2018–2023)
revenue_growth FLOAT YoY revenue growth rate
roe FLOAT Return on equity
pe_ratio FLOAT Price-to-earnings ratio
debt_to_equity FLOAT Debt-to-equity ratio
profit_margin FLOAT Net profit margin
market_cap_b FLOAT Market cap in billions USD
eps FLOAT Earnings per share (USD)
dividend_yield FLOAT Annual dividend yield

Setup

# 1. Install dependencies
pip install -r requirements.txt

# 2. Initialize the database (auto-seeds from CSV)
python -m app.db.database

# 3. Start the API server
uvicorn app.main:app --reload --port 8000

API key configuration needed.

API Endpoints

POST /api/query

Convert a natural language question into SQL, execute it, and explain the results.

Request:

{ "query": "Top 10 stocks by ROE in 2023" }

Response:

{
  "sql": "SELECT ticker, ROUND(roe, 4) AS roe FROM fundamentals WHERE year = 2023 ORDER BY roe DESC LIMIT 10",
  "results": [...],
  "explanation": "...",
  "row_count": 10,
  "retries_used": 0
}

GET /api/schema

Returns the full database schema and column descriptions.

GET /api/health

Health check — returns {"status": "ok"}.

Key Features

  • Prompt-engineered SQL generation — schema-aware system prompt with rules preventing hallucinated columns, unsafe queries, and format issues
  • SQL validation — blocks DDL/DML, enforces SELECT-only access
  • Retry logic — if SQL execution fails, a second LLM call fixes and retries (up to 2 retries)
  • Explanation layer — a separate LLM call translates raw results into plain-English insights
  • CORS enabled — ready for any frontend origin

Sample Queries

  • "Top 10 stocks by ROE"
  • "Stocks with negative revenue growth in 2022"
  • "High PE but low profit margin companies"
  • "Average ROE by ticker over all years"
  • "Best dividend yielding stocks in 2023"
  • "Stocks with debt-to-equity above 2.0"

Project Structure

financial-analyst/
├── app/
│   ├── main.py                  # FastAPI app + CORS + startup
│   ├── api/routes.py            # /query, /schema, /health endpoints
│   ├── services/
│   │   ├── llm_service.py       # SQL generation + retry via LLM API
│   │   ├── sql_service.py       # Validation + SQLite execution
│   │   └── explanation_service.py  # Result explanation via LLM API
│   ├── db/database.py           # DB init + seeding
│   └── utils/prompt_templates.py   # All LLM prompts
├── data/
│   ├── financial_data.csv       # 360-row synthetic dataset
│   └── generate_data.py         # Dataset generator
├── requirements.txt
└── README.md

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages