On a CPU-only machine with no CUDA (i7-12700H, 16GB RAM), run Llama 3.1 8B locally with Ollama and set up the full finance AI Agent stack through LangChain, FastAPI, yfinance, and pykrx.
This series is written against a laptop with the specs below.
| Item | Spec | Impact on AI inference |
|---|---|---|
| CPU | Intel i7-12700H (14 cores/20 threads) | Ideal for llama.cpp, parallel token generation |
| RAM | 16GB LPDDR5 6400MHz | Can fully load an 8B Q4 model |
| GPU | Intel Arc A370M 4GB | No CUDA β CPU inference is used |
| Storage | D: 519GB free | Plenty of room for model files |
| Model | Size (Q4_K_M) | RAM use | Speed | Use |
|---|---|---|---|---|
| llama3.1:8b | ~5.0GB | 7GB | ~5 tok/s | Main agent reasoning |
| phi3.5:mini | ~2.7GB | 4GB | ~15 tok/s | Fast classification and routing |
| qwen2.5:7b | ~4.8GB | 7GB | ~5 tok/s | Analysis of Korean-language filings |
Claude API as backup: complex portfolio analysis and multi-agent orchestration call claude-sonnet-4-6 through the API. Local models and the API are mixed according to the task.
Ollama is the simplest way to run GGUF quantized models on a CPU. After installing on Windows, run it from PowerShell.
# 1. Install Ollama (Windows)
# Download from https://ollama.com/download and install
# 2. Verify the install
ollama --version
# 3. Pull the main model (about 5GB β stored on the D drive)
ollama pull llama3.1:8b
# 4. Model that is strong at Korean, for DART filings (optional)
ollama pull qwen2.5:7b
# 5. Small model for fast routing
ollama pull phi3.5:mini
# 6. List installed models
ollama list# Start the Ollama server (separate terminal)
ollama serve
# β serves an OpenAI-compatible API at http://localhost:11434
# Smoke test
curl http://localhost:11434/api/generate -d '{
"model": "llama3.1:8b",
"prompt": "Give three reasons why the Samsung Electronics share price is rising.",
"stream": false
}'Changing where models are stored: the default path is C:\Users\[user]\.ollama. To move it to the D drive, set the environment variable OLLAMA_MODELS=D:\ollama-models.
# Create the project folder
mkdir D:\ai-agent-finance
cd D:\ai-agent-finance
# Create the virtual environment (Python 3.13)
python -m venv venv
.\venv\Scripts\activate
# Install packages
pip install --upgrade pip# LangChain core
langchain==0.3.25
langchain-community==0.3.25
langchain-ollama==0.3.3
langchain-anthropic==0.3.15
langgraph==0.4.8
# Web server
fastapi==0.115.12
uvicorn[standard]==0.34.3
python-dotenv==1.1.0
pydantic==2.11.5
# Financial data
yfinance==0.2.61
pykrx==1.0.48
pandas==2.2.3
pandas-ta==0.3.14b0
# RAG / vector DB
chromadb==1.0.12
sentence-transformers==3.4.1
# Utilities
requests==2.32.3
beautifulsoup4==4.13.4
httpx==0.28.1pip install -r requirements.txt
# Anthropic SDK (Claude API)
pip install anthropicA library wrapping the Yahoo Finance API that fetches US stock, ETF, and index data for free.
import yfinance as yf
import pandas as pd
# Single ticker
ticker = yf.Ticker("AAPL")
# Basic info
info = ticker.info
print(f"Company: {info.get('longName')}")
print(f"Price: ${info.get('currentPrice')}")
print(f"Market cap: ${info.get('marketCap'):,}")
print(f"PER: {info.get('trailingPE'):.2f}")
# 6 months of daily bars
hist = ticker.history(period="6mo")
print(hist[['Open', 'High', 'Low', 'Close', 'Volume']].tail())
# Several tickers at once
tickers = yf.download(["AAPL", "MSFT", "NVDA", "GOOGL"], period="1mo")
print(tickers['Close'].tail())
# Indexes
sp500 = yf.Ticker("^GSPC") # S&P 500
nasdaq = yf.Ticker("^IXIC") # NASDAQ
print(f"S&P500: ${sp500.info.get('regularMarketPrice')}")Query Korea Exchange (KRX) data from Python. No API key is required.
from pykrx import stock
from pykrx import bond
import pandas as pd
from datetime import datetime, timedelta
today = datetime.today().strftime("%Y%m%d")
yesterday = (datetime.today() - timedelta(days=1)).strftime("%Y%m%d")
# Look up the stock name by code
name = stock.get_market_ticker_name("005930")
print(f"005930 = {name}") # Samsung Electronics
# Samsung Electronics OHLCV for the last 30 days
df = stock.get_market_ohlcv_by_date("20250101", today, "005930")
print(df.tail())
# Quotes for every KOSPI stock
kospi = stock.get_market_ohlcv_by_ticker(today, market="KOSPI")
print(f"KOSPI stocks: {len(kospi)}")
print(kospi.sort_values("κ±°λλ", ascending=False).head(5))
# Market index
index_df = stock.get_index_ohlcv_by_date("20250101", today, "1001") # KOSPI
print(index_df.tail())
# Top stocks by foreign net buying
foreigner = stock.get_market_net_purchases_of_equities_by_ticker(
today, today, "KOSPI", "μΈκ΅μΈ"
)
print(foreigner.head(10))pykrx caveat: the data for the day is updated after the market close (after 4 PM KST). It provides closing-price data, not real-time trade prices. Its DataFrame columns and some arguments are Korean strings, so they are kept as-is in the code.
Get a free API key from DART, run by the Korean Financial Supervisory Service, to collect filing data.
# 1. Sign up at https://opendart.fss.or.kr
# 2. Apply for the API β the key is issued immediately
# 3. Save it in the .env fileDART_API_KEY=your_dart_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
OLLAMA_BASE_URL=http://localhost:11434import requests
import os
from dotenv import load_dotenv
load_dotenv()
DART_KEY = os.getenv("DART_API_KEY")
def get_company_info(corp_name: str) -> dict:
"""Look up the DART corp code by company name"""
url = "https://opendart.fss.or.kr/api/company.json"
params = {"crtfc_key": DART_KEY, "corp_name": corp_name}
res = requests.get(url, params=params)
return res.json()
def get_disclosures(corp_code: str, days: int = 30) -> list:
"""List recent filings"""
from datetime import datetime, timedelta
bgn = (datetime.today() - timedelta(days=days)).strftime("%Y%m%d")
url = "https://opendart.fss.or.kr/api/list.json"
params = {
"crtfc_key": DART_KEY,
"corp_code": corp_code,
"bgn_de": bgn,
"sort": "date",
"sort_mth": "desc",
}
return requests.get(url, params=params).json().get("list", [])
# Test
company = get_company_info("μΌμ±μ μ") # Samsung Electronics β DART is searched by Korean company name
print(company)from langchain_ollama import ChatOllama
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage, SystemMessage
import os
from dotenv import load_dotenv
load_dotenv()
# Local Ollama model (free, CPU inference)
local_llm = ChatOllama(
model="llama3.1:8b",
base_url="http://localhost:11434",
temperature=0, # financial analysis: reproducibility first
num_predict=2048, # maximum output tokens
)
# Claude API (for complex analysis)
claude_llm = ChatAnthropic(
model="claude-sonnet-4-6",
api_key=os.getenv("ANTHROPIC_API_KEY"),
temperature=0,
max_tokens=4096,
)
def ask_local(question: str, system: str = "") -> str:
messages = []
if system:
messages.append(SystemMessage(content=system))
messages.append(HumanMessage(content=question))
return local_llm.invoke(messages).content
def ask_claude(question: str, system: str = "") -> str:
messages = []
if system:
messages.append(SystemMessage(content=system))
messages.append(HumanMessage(content=question))
return claude_llm.invoke(messages).content
# Test
if __name__ == "__main__":
print("=== Ollama (local) ===")
print(ask_local("What are the three key factors that move the NVIDIA share price?"))
print("\n=== Claude (API) ===")
print(ask_claude("Analyze the risks of the Samsung Electronics semiconductor business."))from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from llm_setup import ask_local
app = FastAPI(title="Finance AI Agent API", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class QueryRequest(BaseModel):
question: str
model: str = "local" # "local" | "claude"
class QueryResponse(BaseModel):
answer: str
model_used: str
@app.get("/health")
async def health():
return {"status": "ok"}
@app.post("/ask", response_model=QueryResponse)
async def ask(req: QueryRequest):
if req.model == "claude":
from llm_setup import ask_claude
answer = ask_claude(req.question)
else:
answer = ask_local(req.question)
return QueryResponse(answer=answer, model_used=req.model)# Start the server
uvicorn main:app --reload --port 8000
# Test the API
curl -X POST http://localhost:8000/ask \
-H "Content-Type: application/json" \
-d '{"question": "What is the current P/E of Apple stock?"}'
# Swagger UI
# http://localhost:8000/docs"""Verification script for the whole environment"""
import sys
def check(label, fn):
try:
fn()
print(f" β {label}")
except Exception as e:
print(f" β {label}: {e}")
sys.exit(1)
print("=== Finance AI Agent environment check ===\n")
# 1. Ollama
def test_ollama():
import requests
r = requests.get("http://localhost:11434/api/tags")
assert r.status_code == 200
models = [m["name"] for m in r.json().get("models", [])]
assert any("llama3.1" in m for m in models), f"llama3.1 model missing: {models}"
check("Ollama + llama3.1:8b", test_ollama)
# 2. yfinance
def test_yfinance():
import yfinance as yf
t = yf.Ticker("AAPL")
assert t.info.get("currentPrice") is not None
check("yfinance (US stocks)", test_yfinance)
# 3. pykrx
def test_pykrx():
from pykrx import stock
from datetime import datetime, timedelta
d = (datetime.today() - timedelta(days=3)).strftime("%Y%m%d")
df = stock.get_market_ohlcv_by_date(d, d, "005930")
assert not df.empty
check("pykrx (Korean stocks)", test_pykrx)
# 4. DART API
def test_dart():
import requests, os
from dotenv import load_dotenv
load_dotenv()
key = os.getenv("DART_API_KEY", "")
assert key, "DART_API_KEY is not set"
r = requests.get(f"https://opendart.fss.or.kr/api/company.json",
params={"crtfc_key": key, "corp_name": "μΌμ±μ μ"})
assert r.status_code == 200
check("DART API", test_dart)
# 5. LangChain + Ollama
def test_langchain():
from llm_setup import ask_local
ans = ask_local("1+1=?")
assert len(ans) > 0
check("LangChain + Ollama", test_langchain)
# 6. ChromaDB
def test_chroma():
import chromadb
client = chromadb.Client()
col = client.create_collection("test")
col.add(documents=["test doc"], ids=["1"])
assert col.count() == 1
check("ChromaDB", test_chroma)
print("\nβ
All checks passed. Move on to 02 Market Data Agent.")# Run the check (the Ollama server must be running)
python verify_all.py
# Expected output:
# === Finance AI Agent environment check ===
# β Ollama + llama3.1:8b
# β yfinance (US stocks)
# β pykrx (Korean stocks)
# β DART API
# β LangChain + Ollama
# β ChromaDB
# β
All checks passed.