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
Option A: Docker (Recommended)
# 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
- Frontend: http://localhost:8501
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
š Usage
Chat Interface
- Open the Streamlit frontend at http://localhost:8501
- Start chatting with the AI assistant
- 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
- Use the sidebar to upload documents (PDF, MD, TXT, DOCX)
- Documents are automatically processed and added to the knowledge base
- 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 responseGET /chat/history- Get conversation historyDELETE /chat/history- Clear conversation memoryPOST /documents/upload- Upload documentsGET /documents/list- List uploaded documentsGET /tools- Get available toolsGET /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
- 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": "..."}
- Register the tool in
ToolManager._initialize_tools():
self.tools["new_tool"] = NewTool()
Adding New LLM Providers
- Create a new wrapper class in
configs/llm_wrapper.py:
class NewProviderWrapper(LLMWrapper):
def get_llm(self) -> LLM:
# Return LLM instance
pass
- 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
- Environment Variables: Set production environment variables
- Reverse Proxy: Use Nginx or similar for SSL termination
- Database: Consider using external vector database for production
- Monitoring: Add monitoring and alerting
Render Deployment
- Connect your repository to Render
- Set environment variables in Render dashboard
- 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:
/healthendpoint for monitoring
š¤ Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
š License
This project is licensed under the MIT License - see the LICENSE file for details.
š Acknowledgments
- LangChain for the AI framework
- FastAPI for the web framework
- Streamlit for the frontend
- ChromaDB for vector storage
- OpenAI for AI models
š Support
For support and questions:
- Check the Issues page
- Review the API documentation at
/docs - Check the logs for error details
Happy Chatting! š¤āØ