Skip to content

AI-powered chatbot with FastAPI backend, LangChain agent, Streamlit UI, and PostgresDB persistence.

Notifications You must be signed in to change notification settings

krgaurav7/Chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Chatbot 🧠

A modern, intelligent chatbot application built with FastAPI and LangChain, featuring a Streamlit web interface for interactive conversations. This project demonstrates the implementation of an AI-powered conversational agent with multi-threaded conversation management and streaming capabilities.

Features ✨

  • FastAPI Backend: High-performance REST API with automatic OpenAPI documentation
  • LangChain Integration: Leverages LangChain for agent orchestration and conversation management
  • Thread Management: Support for multiple concurrent conversation threads with persistent history
  • Streaming Responses: Real-time streaming of chatbot responses for improved UX
  • Streamlit Frontend: Interactive web interface for chatting with the bot
  • Database Integration: MongoDB-based storage for conversation history and thread management
  • State Management: Sophisticated state machine for managing chat agent behavior

Tech Stack πŸ› οΈ

Backend:

  • FastAPI 0.128.0
  • LangChain (AI/LLM orchestration)
  • Uvicorn (ASGI server)
  • Python 3.13

Frontend:

  • Streamlit
  • Requests (HTTP client)

Database:

  • MongoDB

Additional Libraries:

  • python-dotenv (environment configuration)
  • Various AI/ML dependencies

Project Structure πŸ“

.
β”œβ”€β”€ main.py                 # FastAPI application entry point
β”œβ”€β”€ new_app.py             # Enhanced Streamlit interface with threading
β”œβ”€β”€ app.py                 # Alternative Streamlit interface (basic)
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ agents/
β”‚   β”‚   └── chat_agent/
β”‚   β”‚       β”œβ”€β”€ graph.py           # Chat agent graph builder
β”‚   β”‚       β”œβ”€β”€ nodes/             # Agent nodes/actions
β”‚   β”‚       β”œβ”€β”€ states/            # State definitions
β”‚   β”‚       └── tools/             # Agent tools
β”‚   β”œβ”€β”€ handlers/
β”‚   β”‚   └── chat_handler.py        # Request handlers for chat operations
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── chat_route.py          # API route definitions
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── database_service.py    # Database operations
β”‚   └── main.py
└── my_env/                # Python virtual environment

Getting Started πŸš€

Prerequisites

  • Python 3.13+
  • MongoDB instance (local or cloud)
  • pip or poetry for dependency management

Installation

  1. Clone the repository:
git clone https://github.com/krgaurav7/Chatbot.git
cd Chatbot
  1. Create and activate virtual environment:
# Windows
python -m venv my_env
my_env\Scripts\activate

# Linux/macOS
python3 -m venv my_env
source my_env/bin/activate
  1. Install dependencies:
pip install -r requirements.txt

Or if using the virtual environment in the repo:

my_env\Scripts\activate
  1. Configure environment variables: Create a .env file in the root directory:
DB_URI=mongodb://localhost:27017/chatbot
# Add other required environment variables

Running the Application

Option 1: FastAPI Backend + Streamlit Frontend (Recommended)

  1. Start the FastAPI backend:
python main.py

The API will be available at http://localhost:8000

  • API documentation: http://localhost:8000/docs
  1. In another terminal, start the Streamlit interface:
# Enhanced version with threading support
streamlit run new_app.py

# Or basic version
streamlit run app.py

The interface will open at http://localhost:8501

Option 2: Basic Streamlit Only

streamlit run app.py

API Endpoints πŸ“‘

Chat Endpoints

  • POST /chat/{thread_id} - Send a message and get a response

    • Parameters: message (query parameter)
    • Returns: Chat state with messages
  • POST /stream/{thread_id} - Stream chat response

    • Parameters: message (query parameter)
    • Returns: Streaming text response

Thread Management

  • GET /chat/threads - Get all conversation threads

    • Returns: List of thread IDs
  • GET /chat/history/{thread_id} - Get conversation history

    • Returns: Chat state with message history

Architecture πŸ—οΈ

Backend Architecture

The backend follows a modular architecture:

  1. FastAPI Application (main.py)

    • Manages application lifecycle
    • Database initialization on startup
    • Request routing
  2. Chat Agent (src/agents/chat_agent/)

    • Graph-based agent orchestration
    • State management for conversations
    • Tool integration for extended capabilities
  3. Route Handler (src/routes/)

    • REST endpoint definitions
    • Request/response mapping
  4. Business Logic (src/handlers/)

    • Chat processing logic
    • Streaming response handling
    • Thread and history management
  5. Database Service (src/services/)

    • MongoDB integration
    • Data persistence

Frontend Architecture

  • Streamlit Interface (new_app.py)
    • Multi-threaded conversation support
    • Real-time chat history
    • Thread management UI
    • Streaming response display

Usage Examples πŸ’¬

Using the Streamlit Interface

  1. Open the Streamlit app in your browser
  2. View existing threads or create a new conversation
  3. Type your message in the chat input
  4. View the bot's response in real-time

Using the API Directly

# Get all threads
curl http://localhost:8000/chat/threads

# Send a message
curl -X POST "http://localhost:8000/chat/thread-abc123?message=Hello%20there"

# Stream a response
curl -N "http://localhost:8000/stream/thread-abc123?message=Tell%20me%20a%20story"

Development πŸ‘¨β€πŸ’»

Project Setup for Development

  1. Activate virtual environment
  2. Install development dependencies
  3. Set up MongoDB locally or use a cloud instance
  4. Configure .env file with your settings

Key Files to Modify

  • src/agents/chat_agent/graph.py - Modify agent behavior
  • src/agents/chat_agent/nodes/ - Add or modify agent actions
  • src/agents/chat_agent/tools/ - Add new tools/capabilities
  • src/handlers/chat_handler.py - Modify request handling logic

Environment Variables

Create a .env file with the following:

DB_URI=mongodb://localhost:27017/chatbot
# Add other configuration as needed

Troubleshooting πŸ”§

Database Connection Issues

  • Ensure MongoDB is running: mongod
  • Check DB_URI in .env matches your MongoDB setup

API Not Responding

  • Verify FastAPI server is running: python main.py
  • Check http://localhost:8000/docs for API documentation

Streamlit Issues

  • Make sure FastAPI backend is running before starting Streamlit
  • Clear Streamlit cache: streamlit cache clear

Contributing 🀝

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License πŸ“„

This project is open source and available under the MIT License.

Author πŸ‘€

Gaurav Kumar

Acknowledgments πŸ™

Support πŸ’¬

For support, please open an issue on the GitHub repository.


Happy Chatting! πŸš€

About

AI-powered chatbot with FastAPI backend, LangChain agent, Streamlit UI, and PostgresDB persistence.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published