Skip to content

chanlii/StockBotAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stock AI Agent

An AI-powered stock analysis chatbot. Ask questions about stocks and the agent uses tools to look up real-time data, then explains its findings.

Built with: FastAPI + OpenAI-compatible function calling + yfinance + PostgreSQL.

User: "How is AAPL doing?"

Agent calls: get_stock_price("AAPL") → get_stock_history("AAPL")

Agent: "Apple is trading at $195.50, up 1.2% today. RSI is 58 (neutral), trading above its 20-day SMA..."


Architecture

Browser (/ui) │ ▼ main.py (FastAPI) │ ├── POST /api/chat/ ──▶ agent.py ──▶ OpenAI / Gemini / Groq / Ollama │ │ │ ▼ │ tools.py (yfinance) │ ├── get_stock_price() │ ├── get_stock_history() ← RSI, MACD, SMA │ └── get_company_info() │ ├── GET /api/stocks/{ticker} ──▶ tools.py (direct call) │ └── db.py (PostgreSQL) ──▶ saves chat history

4 Python files, 7 pip packages.


Quick Start

1. Install

python -m venv venv && source venv/bin/activate pip install -r requirements.txt

2. Get a free API key (pick one)

Provider Free Tier Get Key
Google Gemini (recommended) 15 req/min, 1500/day https://aistudio.google.com/apikey
Groq 30 req/min https://console.groq.com/keys
Ollama (local) Unlimited curl -fsSL https://ollama.com/install.sh | sh && ollama pull llama3.1

3. Configure

cp .env.example .env

Edit .env — for Gemini: OPENAI_API_KEY=your-gemini-key OPENAI_BASE_URL=https://generativelanguage.googleapis.com/v1beta/openai/ MODEL=gemini-2.0-flash DATABASE_URL=postgresql://postgres:postgres@localhost:5432/stock_ai

4. Set up PostgreSQL

createdb stock_ai

Tables are created automatically on first run.

5. Run

python main.py

Open http://localhost:8000/ui — Chat tab to talk to the agent, Stock Lookup tab for quick quotes.

API docs at http://localhost:8000/docs.


Project Structure

stock-ai-agent/ ├── main.py # FastAPI app + all routes ├── agent.py # OpenAI function-calling agent loop ├── tools.py # Tool schemas + implementations (yfinance) ├── db.py # PostgreSQL chat history (SQLAlchemy) ├── frontend/ │ └── index.html # Chat + Stock Lookup UI ├── requirements.txt # 7 packages └── .env.example # Config template

API Endpoints

Method Endpoint Description
GET /ui Web UI
POST /api/chat/ Chat with the agent
GET /api/chat/history Chat history
GET /api/stocks/{ticker} Stock quote
GET /api/stocks/{ticker}/history Price history + technicals
GET /api/stocks/{ticker}/company Company info
GET /api/system/health Health check

Adding a New Tool

Add 3 things in tools.py:

1. Schema — tells the LLM when/how to call it

TOOL_SCHEMAS.append({ "type": "function", "function": { "name": "my_tool", "description": "What this tool does", "parameters": { "type": "object", "properties": { "arg1": {"type": "string", "description": "..."} }, "required": ["arg1"], }, }, })

2. Function — does the actual work

def my_tool(arg1: str) -> str: return json.dumps({"result": "..."})

3. Register it

TOOL_FUNCTIONS["my_tool"] = my_tool

The agent picks it up automatically on next request.


License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors