- Python version >= 3.9. Because langchainhub package requires it
- Docker and Docker Compose (for containerized deployment)
- This is a chatbot implementation with Langchain framework.
- Base LLM: Vertex AI or OpenAI API
- Memory: MongoDB
- UI:
- Next.js frontend
- FastAPI backend
- Prompt versioning and tracing: LangSmith
- User can custom bot's personality by setting bot information like gender, age, ...
- Demo UI:
The application follows a modern microservices architecture with containerized components:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Docker Compose Environment β
β β
β βββββββββββββββββ ββββββββββββββββββ βββββββββββββββββββββ β
β β β β β β β β
β β Next.js ββββββββΊβ FastAPI βββββββββΊβ MongoDB β β
β β Frontend β β Backend β β Database β β
β β (Port 3000) β β (Port 8080) β β (Port 27017) β β
β β β β β β β β
β βββββββββββββββββ ββββββββββ¬ββββββββ βββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββ ββββββββββββββββββββ β
β β β β β β
β β LangChain βββββββΊβ LangSmith β β
β β Framework β β (Tracing) β β
β β β β β β
β ββββββββββ¬βββββββββ ββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββ ββββββββββββββββββββ β
β β β β β β
β β LLM Provider β β Presidio β β
β β (OpenAI/Vertex) β β Anonymizer β β
β β β β (PII Protection) β β
β βββββββββββββββββββ ββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-
User Interaction:
- User sends a message through the Next.js frontend
- Frontend forwards the request to the FastAPI backend
-
Message Processing:
- Backend optionally anonymizes PII data using Presidio Anonymizer
- LangChain framework builds the conversation chain
- Request is sent to the selected LLM provider (OpenAI or Vertex AI)
- Response is traced using LangSmith for monitoring
-
Conversation Storage:
- Conversations are stored in MongoDB for history
- Each user session has a unique conversation ID
-
Response Generation:
- LLM response is de-anonymized if PII protection is enabled
- Backend sends the formatted response back to the frontend
- Frontend renders the response to the user
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β FastAPI Backend β
β β
β ββββββββββββββ βββββββββββββββ ββββββββββββββββββββββββββ β
β β β β β β β β
β β API ββββββΊβ Bot βββββββΊβ Memory System β β
β β Routes β β Manager β β (MongoDB/Redis) β β
β β β β β β β β
β ββββββββββββββ ββββββββ¬βββββββ ββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββ β
β β β ββββββββββββββββββββββ β
β β Chain ββββββββββΊβ β β
β β Manager β β Anonymizer β β
β β β β β β
β βββββββββ¬ββββββββ ββββββββββββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββββ βββββββββββββββββββββββ β
β β β β β β
β β LLM Models βββββββββΊβ External Tools β β
β β Integration β β (Search/etc.) β β
β β β β β β
β ββββββββββββββββββ βββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Key Backend Components:
- API Routes: FastAPI endpoints for chat, health checks, and conversation management
- Bot Manager: Core orchestration layer that handles message processing
- Memory System: Stores conversation history with MongoDB integration
- Chain Manager: Manages LangChain prompt templates and execution
- Anonymizer: Optional PII protection using Microsoft Presidio
- LLM Integration: Connects to OpenAI or Vertex AI models
- External Tools: Integrates with search and other auxiliary services
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Next.js Frontend β
β β
β ββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββ β
β β β β β β β β
β β Page Layout ββββββΊβ Chat Window βββββΊβ Message Bubbles β β
β β β β Component β β β β
β ββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββ βββββββββββββββββββββ β
β β β β β β
β β API Services βββββΊβ State Management β β
β β β β β β
β βββββββββββββββββββ βββββββββββββββββββββ β
β β
β βββββββββββββββββββ βββββββββββββββββββββ β
β β β β β β
β β UI Components βββββΊβ Utility Helpers β β
β β β β β β
β βββββββββββββββββββ βββββββββββββββββββββ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Key Frontend Components:
- Page Layout: Main application layout and container components
- Chat Window: Core component handling conversation display and input
- Message Bubbles: Display of user and AI messages with formatting
- API Services: REST client for communicating with the backend
- State Management: Manages conversation state and UI interactions
- UI Components: Reusable elements like buttons, inputs, and modals
- Utility Helpers: Support functions for data formatting and processing
Below is a sequence diagram showing how data flows through the system when a user sends a message:
User Frontend Backend LangChain LLM MongoDB
| | | | | |
|--message--->| | | | |
| |--POST /chat->| | | |
| | |--load history-| | |
| | | |--query------|----------->|
| | | |<--history---|------------|
| | |--anonymize--->| | |
| | | |--prompt---->| |
| | | |<--response--| |
| | |<-de-anonymize-| | |
| | |--store--------|-------------|----------->|
| |<---response--| | | |
|<--display---| | | | |
| | | | | |
Key Steps in the Sequence:
-
User Interaction:
- User types a message in the chat interface
- Frontend captures the input and sends to backend API
-
Backend Processing:
- Backend loads conversation history from MongoDB
- If enabled, PII is anonymized using Presidio
- The conversation chain is constructed with LangChain
-
LLM Interaction:
- The prompt with history and user message is sent to the LLM
- LLM processes the request and returns a response
- If PII protection is enabled, the response is de-anonymized
-
Response Handling:
- The conversation is stored in MongoDB
- Response is returned to the frontend
- Frontend displays the message to the user
-
Tracing and Monitoring:
- Throughout this process, LangSmith traces the execution
- Performance metrics and debugging information are collected
- Data anonymization with Microsoft Presidio
- To protect personally identifiable information (PII), we add
PresidioAnonymizer
to my bot to replace PIIs before pass to LLM api. View code in Anonymizer - Steps when using it:
The easiest way to run the entire application is using our setup script:
-
Make the script executable:
chmod +x setup.sh
-
Run the setup script:
./setup.sh
-
Follow the prompts:
- The script will create necessary .env files if they don't exist
- Choose between Docker Compose deployment or local development
- The script will guide you through the rest of the setup process
For Windows users, use setup.bat
instead.
If you prefer to run the commands manually:
-
Set up environment variables:
# For backend cp backend/.env.example backend/.env # Edit the .env file to add your OpenAI API key # For frontend cp frontend/.env.example frontend/.env
-
Start the application:
docker-compose up -d
-
Access the application:
- Frontend: http://localhost:3000
- Backend API: http://localhost:8080
- API documentation: http://localhost:8080/docs
-
Stop the application:
docker-compose down
- Langsmith docs: LangSmith
- Configure environment to connect to LangSmith. Add these to your
backend/.env
file:LANGCHAIN_TRACING_V2=true LANGCHAIN_ENDPOINT="https://api.smith.langchain.com" LANGCHAIN_API_KEY="<your-api-key>" LANGCHAIN_PROJECT="chatbot-with-langchain"
- Download the models for the languages to use in anonymizer. PII support.
python -m spacy download en_core_web_md
- RUN backend
- Clone repo:
git clone https://github.com/btrcm00/chatbot-with-langchain.git
- Add google-cloud-platform credential file to
secure/vertexai.json
or set up OpenAI API key cd backend
- Install required packages:
pip install -r requirements.txt
- Create MongoDB database and config environment variables to connect Mongo
- Run:
python -m uvicorn backend.main:app --reload --host 0.0.0.0 --port 8080
- Clone repo:
- RUN frontend
cd frontend
- Install packages:
npm install
- Start frontend:
npm run dev
For development purposes, you can use the Makefile commands:
make setup # Set up environment files
make start # Start all services
make stop # Stop all services
make logs # View logs from all containers