LLM-powered Chatbot that allows users to explore and visualize the Titanic dataset using natural language. Built using FastAPI, Streamlit and a LangChain Agent.
- ⚙️ Backend: A FastAPI server hosting a LangChain-powered agent.
- 🛠️ Agent Capabilities: The agent uses a ReAct (Reasoning and Acting) loop to decide between performing data computations (using Pandas) or generating visualizations (using Plotly).
- 📊 Dynamic Visualization: Charts are generated as Plotly objects on the backend, serialized and sent via the API, and rendered natively by Streamlit using Plotly.
- 🖥️ Frontend: A Streamlit application that maintains the chat session and utilizes the backend API for responses.
- 📁 Dataset: Uses the Titanic Dataset (automatically downloaded and saved on first boot).
| Method | Route | Description | Auth |
|---|---|---|---|
POST |
/api/chat |
Processes user query and returns text + plot artifact. | Bearer Token |
The system utilizes a LangChain "ReAct" Agent with access to two specialized tools:
-
python_data_tool: Allows the model to execute Pandas queries in anexecwith the included Titanic dataframe. The result of the computation is returned to the model as a string. -
python_plot_tool: Allows the model to generate code for making a PlotlyFigureobject inside anexec, which it then assigns to a specified variable. The tool then extracts this object and serializes it into JSON to be saved as a tool artifact. The model gets a confirmation if the plot generation was successful.
After the ReAct loop is completed, the system returns the final output message in the API response, along with the serialized plot, if any, to be rendered on the frontend.
- Streamlit
session_idis used to maintain chat context and is mapped to thethread_idof the agent's checkpointer. - Persistence: The agent uses a checkpointer (
AsyncSqliteSaver) to store agent history in a SQLite database, allowing it to maintain context across API calls for the same chat.
- The API is protected via Bearer Token authentication. Additionally, the frontend (optionally) requires a specific URL query parameter to prevent unauthorized public access to the demo.
- The backend is containerized with Docker for providing a basic layer of security against the Remote Code Execution (RCE) risk from LLM code-generation.
- Logging is set up to record user queries, agent tool-calls, and system errors to log files in a Docker volume.
- Navigate to the
backend/directory. - Copy
.env.exampleinto.envand fill in your values.API_TOKEN=your_secure_token OPENAI_API_KEY=your_provider_api_key OPENAI_API_BASE=https://api.yourprovider.com/v1 MODEL_NAME=your_model_choice
- Run with Docker Compose:
docker compose up --build
- Navigate to the
frontend/directory. - Create a virtual environment and install requirements:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt - Copy
.streamlit/secrets.toml.exampleinto.streamlit/secrets.tomland fill in your values.API_URL = "http://localhost:8900/api/chat" API_TOKEN = "your_secure_token"
- Run the Streamlit app:
streamlit run app.py
