Skip to content

Madhav-P-2005/LLM_Powered-Financial-Service-ChatBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

39 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’¬ LLM‑Powered Financial Services Chatbot

React 18.x Vite 7.x Tailwind CSS 4.x Flask 3.x HuggingFace OpenAI API Vercel Render

An intelligent, responsive financial services chatbot built with React + Vite + Tailwind on the frontend and Flask + HuggingFace on the backend. Delivers concise, cited answers to financial questions with a dual-provider LLM architecture β€” HuggingFace (free, primary) and OpenAI (paid, fallback).

Try asking:

  • "What is cryptocurrency?"
  • "Explain credit default swap in simple terms"
  • "How does compound interest work?"
  • "What are the risks of trading stocks?"

Every answer includes valid Investopedia source links for further reading.


πŸš€ Live App

Service URL
Frontend (Vercel) finsathi-chatbot-madhavp.vercel.app
Backend (Render) madhavp-financial-service-backend.onrender.com
GitHub Madhav-P-2005/LLM_Powered-Financial-Service-ChatBot

✨ Features

  • πŸ—¨οΈ Conversational Chat UI β€” Real-time chat with message history and auto-scroll
  • πŸ“š Curated Knowledge Base β€” Instant, hand-written answers for high-traffic topics (no API call needed)
  • πŸ€– Qwen2.5-7B LLM β€” Powered by HuggingFace's free Inference API (Qwen2.5-7B-Instruct)
  • πŸ”„ OpenAI Fallback β€” Automatic fallback to OpenAI (gpt-4o-mini) if HuggingFace is down
  • πŸ”— Smart Citations β€” 200+ verified Investopedia URLs with multi-tier fallback resolution
  • ⚑ Retry Button β€” Failed messages show a styled retry button for better UX
  • πŸŒ“ Dark/Light Mode β€” Auto-applies based on saved or system preference
  • 🧹 Clear Chat & Home β€” Quick-action toolbar buttons
  • πŸ“± Fully Responsive β€” Mobile-first design with Tailwind CSS
  • πŸ”’ Secure β€” API keys loaded from backend .env, never exposed to the browser

🧠 How It Works

Dual-Provider LLM Architecture

User Question
     β”‚
     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Curated KB        β”‚ ──→ Instant response (no API call)
β”‚  (6 topics)        β”‚     Hand-written, verified answers
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚ Not found
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  HuggingFace       β”‚ ──→ FREE (Qwen2.5-7B-Instruct)
β”‚  (Primary)         β”‚     With retry + exponential backoff
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚ Failed / rate-limited
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  OpenAI            β”‚ ──→ PAID fallback (gpt-4o-mini)
β”‚  (Fallback)        β”‚     Only if OPENAI_API_KEY is set
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Smart URL Resolution (4-tier)

  1. Exact match β€” Direct lookup from 200+ verified Investopedia URLs
  2. Partial match β€” Longest canonical key found inside the user's query
  3. Heuristic β€” Auto-generate URL from financial term patterns
  4. Directory fallback β€” Link to Investopedia's alphabetical term pages

πŸ—‚οΈ Project Structure

LLM-Powered Financial Services Chatbot/
β”‚
β”œβ”€β”€ financial-service-chatbot-backend/
β”‚   β”œβ”€β”€ app.py                          # Flask entry point (~70 lines)
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── chat.py                     # /chat and / endpoints (Blueprint)
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ llm_service.py             # HuggingFace + OpenAI dual-provider
β”‚   β”‚   └── url_service.py             # Investopedia URL generation
β”‚   β”œβ”€β”€ knowledge/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ curated_kb.py              # Hand-written answers (6 topics)
β”‚   β”‚   └── canonical_urls.py          # 200+ verified Investopedia URLs
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── .env                       # HF_API_TOKEN + OPENAI_API_KEY
β”‚   β”œβ”€β”€ requirements.txt               # Python dependencies
β”‚   β”œβ”€β”€ Procfile                       # Render start command
β”‚   β”œβ”€β”€ Dockerfile                     # Container config (optional)
β”‚   └── MyEnvironment/                 # Python venv (gitignored)
β”‚
β”œβ”€β”€ financial-service-chatbot-frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ Navbar.jsx             # Header with theme toggle
β”‚   β”‚   β”‚   └── Footer.jsx            # Footer with social links
β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”‚   β”œβ”€β”€ HomePage.jsx           # Landing page
β”‚   β”‚   β”‚   └── ChatPage.jsx          # Chat UI (Retry/Clear/Home)
β”‚   β”‚   β”œβ”€β”€ App.jsx                    # Router setup
β”‚   β”‚   β”œβ”€β”€ main.jsx                   # React entry point
β”‚   β”‚   └── index.css                  # Global styles
β”‚   β”œβ”€β”€ public/
β”‚   β”‚   └── finsathi.svg               # Custom favicon
β”‚   β”œβ”€β”€ tailwind.config.js             # darkMode: 'class'
β”‚   β”œβ”€β”€ vite.config.js                 # Vite configuration
β”‚   └── package.json                   # Node dependencies
β”‚
β”œβ”€β”€ Readme.md                          # This file
β”œβ”€β”€ LICENSE                            # MIT License
└── .gitignore                         # Ignores .env, __pycache__, node_modules, etc.

πŸš€ Quick Start (Local Development)

Prerequisites


1️⃣ Backend Setup (Flask)

Open a terminal and navigate to financial-service-chatbot-backend/:

A. Create & Activate Virtual Environment

Windows (PowerShell):

python -m venv MyEnvironment
.\MyEnvironment\Scripts\Activate.ps1

macOS / Linux:

python3 -m venv MyEnvironment
source MyEnvironment/bin/activate

B. Install Dependencies

pip install -r requirements.txt

C. Configure Environment Variables

Create the file config/.env with your keys:

# PRIMARY β€” HuggingFace Inference API (FREE, no expiry)
# Get your token at: https://huggingface.co/settings/tokens
# Click "Create new token" β†’ Select "Read" access β†’ Copy the hf_... token
HF_API_TOKEN=hf_your_actual_token_here

# FALLBACK β€” OpenAI API (PAID, optional)
# Only needed if you want OpenAI as a backup when HuggingFace is down
# OPENAI_API_KEY=sk-your-key-here
# OPENAI_MODEL=gpt-4o-mini

⚠️ Never commit .env to GitHub! It is already listed in .gitignore.

D. Run the Backend Server

python app.py

Backend runs at: http://127.0.0.1:5000

You can verify it's working by visiting http://127.0.0.1:5000/ in your browser β€” you should see:

{ "status": "ok", "providers": { "huggingface": { "configured": true, ... } } }

2️⃣ Frontend Setup (React + Vite)

Open a second terminal and navigate to financial-service-chatbot-frontend/:

A. Install Packages

npm install

B. Run the Development Server

npm run dev

Frontend runs at: http://localhost:5173

Open http://localhost:5173 in your browser, click "Get Started", and try asking:

  • "What is a credit card?" β†’ Curated answer (instant)
  • "What is dollar cost averaging?" β†’ HuggingFace LLM answer

πŸ”§ Tech Stack & Dependencies

Frontend

Package Version Purpose
React 18.x UI library
Vite 7.x Build tool & dev server
Tailwind CSS 4.x Utility-first styling
Axios 1.x HTTP client for API calls
React Router 6.x Client-side routing
React Icons 5.x Icon library (FaPaperPlane, FaSpinner, etc.)

Backend

Package Version Purpose
Flask 3.x Web framework
Flask-CORS 4.x Cross-origin support for frontend
huggingface_hub 0.25+ Primary LLM β€” HuggingFace Inference API
openai 1.x Fallback LLM β€” OpenAI API (optional)
python-dotenv 1.x Load .env variables
gunicorn 23.x Production WSGI server (Render)

πŸ” Environment Variables

Backend (config/.env)

Variable Required Description
HF_API_TOKEN βœ… Yes Free HuggingFace Inference API token
OPENAI_API_KEY ❌ Optional Paid OpenAI API key (fallback only)
OPENAI_MODEL ❌ Optional OpenAI model name (default: gpt-4o-mini)

Frontend (Vercel Environment Variables)

Variable Required Description
VITE_API_BASE βœ… For production Backend URL (e.g., https://madhavp-financial-service-backend.onrender.com)

πŸ”— API Contract

Health Check

GET /

Response:

{
  "status": "ok",
  "providers": {
    "huggingface": { "configured": true, "model": "Qwen/Qwen2.5-7B-Instruct", "role": "primary (free)" },
    "openai": { "configured": false, "model": "gpt-4o-mini", "role": "fallback (paid)" }
  }
}

Chat

POST /chat
Content-Type: application/json

Request:

{ "message": "What is a credit default swap?" }

Response:

{ "response": "Credit Default Swap (CDS) β€” Plain-English definition\nβ€’ A CDS is a derivative contract...\nSource: https://www.investopedia.com/terms/c/creditdefaultswap.asp" }

Error Response (500):

{ "error": "Sorry, I couldn't process your question right now. Error: ..." }

CORS is configured for http://localhost:5173 (dev) and https://finsathi-chatbot-madhavp.vercel.app (production).


πŸ“¦ Deployment Guide

Backend β†’ Render

  1. Create a new Web Service on Render.
  2. Connect your GitHub repository and select the branch (Project-3).
  3. Configure the service:
Setting Value
Name madhavp-financial-service-backend
Runtime Python 3
Root Directory financial-service-chatbot-backend
Build Command pip install -r requirements.txt
Start Command gunicorn app:app
  1. Set Environment Variables in the Render dashboard:
Key Value
HF_API_TOKEN hf_your_token_here
OPENAI_API_KEY (optional β€” only if you have one)
  1. Click Deploy. Render will build and start your backend.
  2. Verify: Visit your Render URL β€” you should see the health check JSON.

πŸ’‘ Tip: Render's free tier spins down after 15 min of inactivity. The first request after sleep may take ~30s (cold start).


Frontend β†’ Vercel

  1. Import your repository on Vercel.
  2. Configure the project:
Setting Value
Framework Preset Vite
Root Directory financial-service-chatbot-frontend
Build Command npm run build
Output Directory dist
  1. Set Environment Variables in Vercel dashboard:
Key Value
VITE_API_BASE https://madhavp-financial-service-backend.onrender.com

⚠️ Important: Vite environment variables must start with VITE_ to be accessible in the frontend code.

  1. Click Deploy. Vercel will build the React app and serve it.
  2. Verify: Open your Vercel URL β†’ Click "Get Started" β†’ Ask a question.

Updating the Live App

Whenever you push new code to GitHub, both Render and Vercel will auto-redeploy. If you change environment variables (like API tokens), update them in the respective dashboards and trigger a manual redeploy.


🀝 Contributing

PRs and suggestions welcome. Open an issue for bugs or improvements.

πŸͺͺ License

This project is licensed under the MIT License. See the LICENSE file for details.


Built with πŸ’™ by Madhav P

About

πŸ€– FinSathi AI β€” LLM-powered finance chatbot built with React + Tailwind + Flask. Dual-provider architecture (HuggingFace + OpenAI fallback). Delivers concise, cited answers with 200+ verified Investopedia sources.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors