05 — Running the app step-by-step
Duration: 12 min Prerequisites: chapter 02 (env + API key ready).
All commands
Section titled “All commands”# From the project-2/ folder (where both repos sit side by side)cd csv-llm-openai
# Quick start (helper script).\start.ps1
# OR manualstreamlit run app.py --server.port 8505The app opens at http://127.0.0.1:8505.
The privacy banner
Section titled “The privacy banner”At the top of the page, a yellow warning:
⚠️ Privacy: every question sends a slice of your transactions to the OpenAI API. To stay 100% local, use
csv-llm-ollama/instead.
This is a key difference from Project 1. You’re immediately reminded data is leaving your machine.
First import
Section titled “First import”Same as Project 1:
- Sidebar → CSV path = pre-filled with
data/data1-anonymized.csv. - Profile name =
data1. - Click 📥 (Re)load this CSV.
- 734 transactions loaded into
data1.
The Dashboards tab now shows the same 4 KPIs and 4 figures from Project 1.
Dashboards tab — same numbers as Ollama
Section titled “Dashboards tab — same numbers as Ollama”| KPI | Value |
|---|---|
| Total | $118,667.69 |
| Monthly average | $9,888.97 |
| Avg per transaction | $175.80 |
| # expense transactions | 675 |
Since the data and SQL queries are identical, the dashboards are byte-for-byte the same. Only the chat tab will behave differently.
Chat tab — the speed difference
Section titled “Chat tab — the speed difference”Sidebar fields specific to this app:
- OPENAI_API_KEY (password mode). If empty, we read from environment or
.env. - Model: default
gpt-4o-mini. You can switch togpt-4ofor max accuracy at ~10× the price.
Type Q2: What is my biggest spending category?
Typical answer in ~2 seconds:
Your biggest spending category is Housing with a total of $30,895.29.
⏱️ 1.8 s · 1 SQL call · ~$0.0003
And the 🔧 SQL #1 expander shows:
SELECT category, SUM(amount) AS totalFROM transactionsWHERE profile = 'data1' AND amount > 0GROUP BY categoryORDER BY total DESCLIMIT 1| category | total |
|---|---|
| Housing | 30895.29 |
Identical SQL, identical answer. 5× faster than Project 1.
The 4 canonical questions
Section titled “The 4 canonical questions”| # | Question | Latency | Expected answer |
|---|---|---|---|
| Q1 | How much did I spend on groceries last month? | 1.5 s | Filter by category='Groceries' + last-month date |
| Q2 | What is my biggest spending category? | 1.8 s | Housing with $30,895.29 |
| Q3 | How many times did I visit Coastal Spoon Cafe this year? | 2.0 s | 19 times, total $1,221.34 |
| Q4 | List my top 5 merchants | 2.5 s | Rent Payment Willow Street, StorageBox Monthly, GardenCare Subscription, Postbox Rental, Queenstown Path Pass |
Total cost across all 4: ~$0.001 — one tenth of a cent.
Watch the live cost
Section titled “Watch the live cost”Open platform.openai.com/usage in another tab and refresh after each question. You’ll see your spend increment by ~$0.0003 at a time. Reassuring.
Side-by-side comparison
Section titled “Side-by-side comparison”If you also have Project 1 running on port 8504, you can compare answers in real time:
| Aspect | Ollama (llama3.1:8b) | OpenAI (gpt-4o-mini) |
|---|---|---|
| Q2 latency (1 SQL call) | ~5 s | ~2 s |
| Q4 latency (1 SQL call) | ~10 s | ~3 s |
| SQL accuracy | Very good (95%) | Excellent (99%) |
| Answer style | Often verbose, explanatory | More concise |
| Cost for 4 questions | $0 | ~$0.001 |
| Privacy | Data stays local | Data sent to OpenAI |
Sequence diagram in the wild
Section titled “Sequence diagram in the wild”If you click the debug toggle in the sidebar, each chat row prints the full sequence:
[14:32:01] User → "What is my biggest spending category?"[14:32:01] LLM call #1 (gpt-4o-mini, 612 input tokens)[14:32:02] tool_call: query_sql(SELECT category, SUM(amount) ...)[14:32:02] safe_query → 1 row: ('Housing', 30895.29)[14:32:02] LLM call #2 (gpt-4o-mini, 720 input tokens)[14:32:03] Response: "Your biggest spending category is Housing..."[14:32:03] Cost estimate: $0.00031Troubleshooting
Section titled “Troubleshooting”| Error | Cause | Fix |
|---|---|---|
OPENAI_API_KEY missing | No .env or env var | Create csv-llm-openai/.env or paste the key in the sidebar |
401 Unauthorized | Invalid or expired key | Regenerate at platform.openai.com |
429 Too Many Requests | Quota / rate-limit | Wait 1 min, add credit, or upgrade plan |
400 Bad Request: invalid tool call format | Old openai SDK | pip install --upgrade openai (≥ 1.40) |
| Empty Dashboards tab | Forgot to load the CSV | Click “(Re)load this CSV” in the sidebar |
| Port 8505 already in use | Previous Streamlit crashed | .\stop.ps1 then relaunch |
Stop the app
Section titled “Stop the app”.\csv-llm-openai\stop.ps1Frees port 8505.
Takeaways
Section titled “Takeaways”- Same UI, same dashboards, same numbers as Project 1.
- ~2 s per question vs ~10 s on Ollama (5× faster).
- Cost: under one cent for the entire course.
- Each question shows the SQL, the rows, and an estimated cost.