ai-chatbot-langchain

Full-stack AI Chatbot with LangChain, FastAPI, and Streamlit

Stars
10
Language
Python
Updated

šŸ¤– AI Chatbot with LangChain

A full-stack AI chatbot application built with LangChain, FastAPI, and Streamlit. Features document processing, conversational memory, tool integration, and a modern web interface.

✨ Features

  • šŸ¤– Modular AI Architecture: Clean abstraction to switch between OpenAI, Claude, or Ollama
  • šŸ“š Document Processing: Support for PDF, Markdown, TXT, and DOCX files
  • šŸ” Vector Database: ChromaDB or FAISS for efficient document retrieval
  • šŸ’¬ Conversational Memory: Maintains context across conversations
  • šŸ› ļø Tool Integration: Calculator, Web Search, and Weather API
  • 🌐 Modern Web Interface: Beautiful Streamlit frontend with real-time chat
  • ⚔ FastAPI Backend: RESTful API with comprehensive endpoints
  • 🐳 Docker Ready: Easy deployment with Docker and Docker Compose
  • šŸ“ Comprehensive Logging: Detailed logging and error handling
  • šŸ”§ Configurable: Environment-based configuration management

šŸ—ļø Architecture

AI Chatbot/
ā”œā”€ā”€ chatbot.py              # Main chatbot engine
ā”œā”€ā”€ tools.py                # Tool integrations (Calculator, Web Search, Weather)
ā”œā”€ā”€ vector_store.py         # Document processing and vector database
ā”œā”€ā”€ api.py                  # FastAPI backend
ā”œā”€ā”€ configs/
│   ā”œā”€ā”€ settings.py         # Configuration management
│   └── llm_wrapper.py      # LLM provider abstraction
ā”œā”€ā”€ frontend/
│   └── streamlit_app.py    # Streamlit web interface
ā”œā”€ā”€ requirements.txt        # Python dependencies
ā”œā”€ā”€ Dockerfile             # Container configuration
ā”œā”€ā”€ docker-compose.yml     # Multi-service deployment
└── README.md              # This file

šŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • OpenAI API key (or Ollama for local models)
  • Docker and Docker Compose (optional)

1. Clone and Setup

git clone <repository-url>
cd ai-chatbot

2. Environment Configuration

Copy the example environment file and configure your settings:

cp env.example .env

Edit .env with your configuration:

# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here
OPENAI_MODEL=gpt-3.5-turbo

# Optional: Weather API
WEATHER_API_KEY=your_weather_api_key_here

# Optional: Web Search API
SERPER_API_KEY=your_serper_api_key_here

3. Install Dependencies

pip install -r requirements.txt

4. Run the Application

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f
Option B: Local Development
# Terminal 1: Start the API backend
python api.py

# Terminal 2: Start the Streamlit frontend
streamlit run frontend/streamlit_app.py

5. Access the Application

šŸ“– Usage

Chat Interface

  1. Open the Streamlit frontend at http://localhost:8501
  2. Start chatting with the AI assistant
  3. The chatbot will automatically use tools when needed:
    • Calculator: "Calculate 15 * 23"
    • Web Search: "Search for latest AI news"
    • Weather: "What's the weather in New York?"

Document Upload

  1. Use the sidebar to upload documents (PDF, MD, TXT, DOCX)
  2. Documents are automatically processed and added to the knowledge base
  3. The chatbot can now answer questions based on your documents

API Endpoints

The FastAPI backend provides comprehensive endpoints:

  • POST /chat - Send a message and get AI response
  • GET /chat/history - Get conversation history
  • DELETE /chat/history - Clear conversation memory
  • POST /documents/upload - Upload documents
  • GET /documents/list - List uploaded documents
  • GET /tools - Get available tools
  • GET /status - Get system status

šŸ› ļø Configuration

LLM Providers

The system supports multiple LLM providers:

OpenAI (Default)
OPENAI_API_KEY=your_key_here
OPENAI_MODEL=gpt-3.5-turbo
Ollama (Local)
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=llama2

Vector Database

Choose between ChromaDB (default) or FAISS:

VECTOR_DB_TYPE=chroma  # or faiss
CHROMA_PERSIST_DIRECTORY=./chroma_db
FAISS_INDEX_PATH=./faiss_index

Tools Configuration

Enable optional tools by adding API keys:

# Weather API (OpenWeatherMap)
WEATHER_API_KEY=your_weather_api_key

# Web Search (Serper API)
SERPER_API_KEY=your_serper_api_key

šŸ”§ Development

Project Structure

ā”œā”€ā”€ chatbot.py              # Main chatbot engine with LangChain
ā”œā”€ā”€ tools.py                # Tool implementations
ā”œā”€ā”€ vector_store.py         # Document processing and vector storage
ā”œā”€ā”€ api.py                  # FastAPI REST API
ā”œā”€ā”€ configs/
│   ā”œā”€ā”€ settings.py         # Environment configuration
│   └── llm_wrapper.py      # LLM provider abstraction
└── frontend/
    └── streamlit_app.py    # Web interface

Adding New Tools

  1. Create a new tool class in tools.py:
class NewTool:
    def __init__(self):
        self.name = "new_tool"
        self.description = "Description of the tool"
    
    def execute(self, **kwargs):
        # Tool implementation
        return {"success": True, "result": "..."}
  1. Register the tool in ToolManager._initialize_tools():
self.tools["new_tool"] = NewTool()

Adding New LLM Providers

  1. Create a new wrapper class in configs/llm_wrapper.py:
class NewProviderWrapper(LLMWrapper):
    def get_llm(self) -> LLM:
        # Return LLM instance
        pass
  1. Add to LLMFactory.create_llm():
elif provider == "new_provider":
    return NewProviderWrapper(**kwargs)

🐳 Deployment

Docker Deployment

# Build and run with Docker Compose
docker-compose up -d

# Scale services
docker-compose up -d --scale chatbot-api=3

Production Deployment

  1. Environment Variables: Set production environment variables
  2. Reverse Proxy: Use Nginx or similar for SSL termination
  3. Database: Consider using external vector database for production
  4. Monitoring: Add monitoring and alerting

Render Deployment

  1. Connect your repository to Render
  2. Set environment variables in Render dashboard
  3. Deploy using the provided Dockerfile

šŸ“Š Monitoring and Logging

The application includes comprehensive logging:

  • Application Logs: ./logs/chatbot.log
  • API Logs: FastAPI automatic logging
  • Health Checks: /health endpoint for monitoring

šŸ¤ Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

šŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

šŸ™ Acknowledgments

šŸ†˜ Support

For support and questions:

  1. Check the Issues page
  2. Review the API documentation at /docs
  3. Check the logs for error details

Happy Chatting! šŸ¤–āœØ