Skip to content

05 — Running the app step-by-step

Duration: 12 min Prerequisites: chapter 02 (env + API key ready).

Terminal window
# From the project-2/ folder (where both repos sit side by side)
cd csv-llm-openai
# Quick start (helper script)
.\start.ps1
# OR manual
streamlit run app.py --server.port 8505

The app opens at http://127.0.0.1:8505.

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.

Same as Project 1:

  1. Sidebar → CSV path = pre-filled with data/data1-anonymized.csv.
  2. Profile name = data1.
  3. Click 📥 (Re)load this CSV.
  4. 734 transactions loaded into data1.

The Dashboards tab now shows the same 4 KPIs and 4 figures from Project 1.

KPIValue
Total$118,667.69
Monthly average$9,888.97
Avg per transaction$175.80
# expense transactions675

Since the data and SQL queries are identical, the dashboards are byte-for-byte the same. Only the chat tab will behave differently.

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 to gpt-4o for 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 total
FROM transactions
WHERE profile = 'data1' AND amount > 0
GROUP BY category
ORDER BY total DESC
LIMIT 1
categorytotal
Housing30895.29

Identical SQL, identical answer. 5× faster than Project 1.

#QuestionLatencyExpected answer
Q1How much did I spend on groceries last month?1.5 sFilter by category='Groceries' + last-month date
Q2What is my biggest spending category?1.8 sHousing with $30,895.29
Q3How many times did I visit Coastal Spoon Cafe this year?2.0 s19 times, total $1,221.34
Q4List my top 5 merchants2.5 sRent Payment Willow Street, StorageBox Monthly, GardenCare Subscription, Postbox Rental, Queenstown Path Pass

Total cost across all 4: ~$0.001 — one tenth of a cent.

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.

If you also have Project 1 running on port 8504, you can compare answers in real time:

AspectOllama (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 accuracyVery good (95%)Excellent (99%)
Answer styleOften verbose, explanatoryMore concise
Cost for 4 questions$0~$0.001
PrivacyData stays localData sent to OpenAI

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.00031
ErrorCauseFix
OPENAI_API_KEY missingNo .env or env varCreate csv-llm-openai/.env or paste the key in the sidebar
401 UnauthorizedInvalid or expired keyRegenerate at platform.openai.com
429 Too Many RequestsQuota / rate-limitWait 1 min, add credit, or upgrade plan
400 Bad Request: invalid tool call formatOld openai SDKpip install --upgrade openai (≥ 1.40)
Empty Dashboards tabForgot to load the CSVClick “(Re)load this CSV” in the sidebar
Port 8505 already in usePrevious Streamlit crashed.\stop.ps1 then relaunch
Terminal window
.\csv-llm-openai\stop.ps1

Frees port 8505.

  • 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.

Next: Cost, latency, accuracy comparison →