Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

📊 Data-Analysis-Chatbot

A Streamlit front end + Azure OpenAI multi-agent backend for chatting with tabular data — intent routing, Pandas analysis, and chart generation with GPT-4.

Python Streamlit Azure OpenAI LangChain


Overview

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:

  1. Builds a system prompt grounded in the loaded dataframe
  2. Classifies user intent with GPT
  3. Routes to specialized paths (analyze, chart, peer compare, general chat)
  4. Uses LangChain’s Pandas dataframe agent for code-assisted analysis
  5. 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.


Features

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

Architecture

┌─────────────────────────────────────────────┐
│              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 routes (high level)

Intent Behavior
AnalyzeDataFrame Pandas agent answers with currency/percentage formatting hints
DrawChartBarChart / 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

Project layout

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.


Setup

1. Environment

python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install streamlit pandas openai python-dotenv tiktoken langchain langchain-community pillow

Exact LangChain package names evolved quickly in 2023–2024; pin versions that match the imports in MainBot.py if you revive an older environment.

2. Azure OpenAI credentials

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_variables

Template 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=32768

MainBot can load different env profiles depending on whether a 32k (or other) deployment is selected for the current step.

3. Data

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.


Running

Streamlit UI

streamlit run chat_interface.py

Open 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”

Backend-only CLI

cd agents
python main.py

Type questions interactively; type end to stop (as implemented in the loop).


Chart response contract

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.


Session state (Streamlit)

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

Design notes

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

Limitations

  • 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

License

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.

About

A Streamlit frontend to an Azure OpenAI backend for chatting with your files using GPT-4.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages