A Streamlit front end + Azure OpenAI multi-agent backend for chatting with tabular data — intent routing, Pandas analysis, and chart generation with GPT-4.
Data-Analysis-Chatbot is a demo assistant for exploring CSVs and business datasets through natural language. Users chat in Streamlit; behind the scenes a MainBot class:
- Builds a system prompt grounded in the loaded dataframe
- Classifies user intent with GPT
- Routes to specialized paths (analyze, chart, peer compare, general chat)
- Uses LangChain’s Pandas dataframe agent for code-assisted analysis
- Returns markdown answers or structured JSON that Streamlit renders as bar/line charts
It was designed for cost-aware multi-model use (e.g. cheaper models for intent, larger context models like GPT-4-32k for heavy analysis) on Azure OpenAI.
| Capability | Description |
|---|---|
| 💬 Chat UI | Streamlit chat messages, avatars, session state history |
| 🎯 Intent engine | GPT classifies prompts into analysis / chart / peer / other / unsupported |
| 📈 Charts | Bar & line chart JSON → Streamlit charts + data expanders |
| 🧾 Pandas agent | LangChain create_pandas_dataframe_agent over the working dataframe |
| 🔀 Model switching | Env profiles for GPT-4 vs GPT-4-32k style deployments |
| 🪙 Token awareness | tiktoken helpers for history sizing against context limits |
| 📝 Run logging | Timestamped log text for prompts, intents, and timings |
| 🖥️ CLI harness | agents/main.py REPL for backend testing without Streamlit |
┌─────────────────────────────────────────────┐
│ chat_interface.py │
│ (Streamlit chat + charts) │
└─────────────────────┬───────────────────────┘
│ query_bot(prompt)
▼
┌─────────────────────────────────────────────┐
│ agents/MainBot.py │
│ system prompt · intent · routing · logs │
└───────┬─────────────┬──────────────┬────────┘
│ │ │
▼ ▼ ▼
AnalyzeDataFrame DrawChart CompareToPeers
(Pandas agent) (JSON → UI) (peer CSV)
│ │ │
└─────────────┴──────┬───────┘
▼
Azure OpenAI deployments
(chat + agent LLM backends)
| Intent | Behavior |
|---|---|
AnalyzeDataFrame |
Pandas agent answers with currency/percentage formatting hints |
DrawChart → BarChart / LineChart |
Agent produces series data; formatter model emits chart JSON |
CompareToPeers |
Loads peer dataset and runs comparative analysis |
Other / UnsupportedRequest |
Falls back to standard chat completion with history |
Data-Analysis-Chatbot/
├── chat_interface.py # Streamlit front end
├── agents/
│ ├── MainBot.py # Core bot: intent, agents, Azure OpenAI
│ └── main.py # Simple CLI loop for MainBot
├── envs/
│ └── env_example.txt # Azure OpenAI env template
└── README.md
Optional assets referenced by the UI (favicon, logos, sample CSVs) may live under static/, agents/files/, or remote blob URLs as configured in code.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install streamlit pandas openai python-dotenv tiktoken langchain langchain-community pillowExact LangChain package names evolved quickly in 2023–2024; pin versions that match the imports in
MainBot.pyif you revive an older environment.
Copy the example env and fill in your resource values:
cp envs/env_example.txt agents/envs/.env # or follow paths used in MainBot.load_env_variablesTemplate fields:
OPENAI_API_KEY=...
OPENAI_API_TYPE=azure
OPENAI_API_BASE=https://YOUR_RESOURCE.openai.azure.com/
OPENAI_API_VERSION=2023-07-01-preview
DEPLOYMENT_NAME=your-gpt4-deployment
MODEL_NAME=gpt-4-32k
MAX_TOKENS=32768MainBot can load different env profiles depending on whether a 32k (or other) deployment is selected for the current step.
By default the bot loads a demo CSV (path or Azure blob URL inside MainBot). Point self.file_path at your own dataset, or extend the Streamlit app to accept uploads into st.session_state.file_df.
streamlit run chat_interface.pyOpen the local URL Streamlit prints, then ask questions like:
- “What is total revenue by region?”
- “Show a bar chart of sales by state”
- “Compare this entity to peers on margin”
cd agents
python main.pyType questions interactively; type end to stop (as implemented in the loop).
When the chart path succeeds, the UI expects JSON such as:
{
"bar": {
"columns": ["New York", "Texas", "Virginia"],
"data": [120.91, 459.53, 390.12]
}
}or "line" with the same shape. chat_interface.py decodes that payload into Streamlit bar_chart / line_chart widgets and optional data expanders.
| Key | Purpose |
|---|---|
chat_history |
Message list for the bot and UI |
file_df |
Active pandas DataFrame |
system_message |
Cached system prompt |
chart_data / chart_data_indices |
Persist rendered charts across reruns |
log_text |
Accumulated run log |
- Intent before tools keeps general chat cheap and routes heavy Pandas work only when needed
- Structured chart JSON separates “get the numbers” from “paint the chart”
- Logging + timings around intent and agent calls help debug latency and prompt cost
- Built as a product-style demo (branding hooks, default assistant greeting, clear conversation controls)
- Azure-specific env and deployment names are hard-wired to the demo era of the OpenAI Python / LangChain APIs
- Sample file paths and blob URLs in the repo may need updating for a fresh run
- Error handling returns a friendly chat message; check console logs for stack traces
- Not a multi-tenant production app — treat as a portfolio / internal demo foundation
See repository license information as published on GitHub. If none is present in a given clone, assume all rights reserved by the author unless otherwise noted.
Natural language in · dataframe insights and charts out — Azure OpenAI + Streamlit.