Skip to content

Repository files navigation

📈 LLM-Powered Stock Analyzer

A Teaching Demonstration for Building LLM Applications


👨‍🏫 Course Information

Created by Dr. Anish Roychowdhury
Institution Plaksha University
Role Associate Professor of Practice

🎯 What You'll Learn

This hands-on lab teaches you how to:

  1. ✅ Build LLM-powered applications with Python
  2. ✅ Integrate real-time APIs (Yahoo Finance) with AI
  3. ✅ Create interactive web interfaces with Streamlit
  4. ✅ Write effective prompts for domain-specific analysis
  5. ✅ Handle API errors and rate limiting gracefully

🛠️ Tech Stack

Component Technology
Language Python 3.9+
Web Framework Streamlit
Stock Data yfinance
LLM API OpenAI GPT
Environment python-dotenv

🚀 Quick Start (5 Minutes)

Step 1: Extract & Enter Directory

unzip llm-stock-analyzer.zip -d llm-stock-analyzer
cd llm-stock-analyzer

Step 2: Run Initialization

bash init.sh

This will:

  • Make all scripts executable
  • Create virtual environment
  • Install dependencies
  • Prompt for your OpenAI API key

Step 3: Run the Application

./run.sh

Open browser at: http://localhost:8501


📁 Project Structure

llm-stock-analyzer/
│
├── 🐍 Python Modules
│   ├── app.py           # Main Streamlit application
│   ├── stock_data.py    # Yahoo Finance data fetching
│   ├── llm_service.py   # OpenAI API integration
│   ├── prompts.py       # Prompt templates
│   └── config.py        # Configuration management
│
├── 🔧 Shell Scripts
│   ├── init.sh          # First-time initialization
│   ├── setup.sh         # Full environment setup
│   ├── dev.sh           # Developer quick restart
│   ├── test.sh          # Test suite runner
│   └── run.sh           # Production runner
│
├── ⚙️ Configuration
│   ├── requirements.txt # Python dependencies
│   ├── .env.example     # Environment template
│   └── .gitignore       # Git ignore rules
│
└── 📖 Documentation
    └── README.md        # This file

🔧 Shell Scripts Reference

Script Command Purpose
init.sh bash init.sh First-time setup (run once)
setup.sh ./setup.sh Reinstall environment
dev.sh ./dev.sh Quick restart during development
dev.sh ./dev.sh --demo Use demo data (no API calls)
test.sh ./test.sh Run all tests
test.sh ./test.sh --quick Skip API tests
run.sh ./run.sh Run the application

🧪 Testing

Run All Tests

./test.sh

Quick Test (Skip API Tests)

./test.sh --quick

Test API Connection Only

source venv/bin/activate
python -c "
from llm_service import validate_api_connection
success, message = validate_api_connection()
print(f'Status: {message}')
"

🐛 Common Issues & Fixes

Issue: 429 Too Many Requests (Yahoo Finance)

429 Client Error: Too Many Requests

Fix: Use demo mode or wait 10-15 minutes

./dev.sh --demo

Issue: 401 Invalid API Key (OpenAI)

Incorrect API key provided

Fix: Update your .env file with a valid key from https://platform.openai.com/api-keys

Issue: Permission Denied

zsh: permission denied: ./setup.sh

Fix: Make scripts executable

chmod +x *.sh

Issue: yfinance TypeError (Python 3.9)

TypeError: unsupported operand type(s) for |

Fix: Install compatible version

pip install yfinance==0.2.40

📊 Demo Mode

When Yahoo Finance is rate-limiting, use demo mode with sample data:

./dev.sh --demo

Available demo tickers:

  • AAPL - Apple Inc.
  • GOOGL - Alphabet Inc.
  • MSFT - Microsoft Corporation
  • RELIANCE.NS - Reliance Industries (India)

🔑 API Key Setup

  1. Go to https://platform.openai.com/api-keys
  2. Create a new API key
  3. Add to .env file:
OPENAI_API_KEY=sk-your-api-key-here

📝 Sample Tickers to Try

Market Tickers
US AAPL, GOOGL, MSFT, AMZN, TSLA, NVDA, META
India (NSE) RELIANCE.NS, TCS.NS, INFY.NS, HDFCBANK.NS
UK (LSE) HSBA.L, BP.L, VOD.L

📚 Learning Exercises

After getting the app running, try these exercises:

  1. Modify Prompts: Edit prompts.py to change the analysis style
  2. Add Metrics: Add new stock metrics to stock_data.py
  3. Compare Stocks: Implement side-by-side comparison
  4. Add Charts: Use st.line_chart() for price history
  5. Error Handling: Improve error messages in app.py

⚠️ Disclaimer

This is an educational demonstration only. The analysis provided is not financial advice. Always:

  • Do your own research
  • Consult licensed financial advisors
  • Understand the risks of investing

📞 Support

  • Instructor: Dr. Anish Roychowdhury
  • Institution: Plaksha University

📄 License

Educational use only. Created for Plaksha University.


Happy Learning! 🚀

About

A simple LLM Application for Stock Analysis

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages