This project documents my learning journey exploring Langchain, MongoDB Atlas Vector Search, and Next.js by building a simple HR assistant chatbot. The goal was to understand Retrieval-Augmented Generation (RAG), agent creation, state management with LangGraph, and integrating various AI models and databases within a modern web framework.
- Chat Interface: A simple UI built with Next.js and Tailwind CSS to interact with the HR agent.
- HR Agent: A Langchain agent capable of answering questions based on employee data.
- Employee Data Lookup: Uses MongoDB Atlas Vector Search to find relevant employee information based on natural language queries.
- Database Seeding: A script (
seed-database.ts) to generate synthetic employee data and populate the vector database using an LLM. - Model Flexibility: Designed to potentially swap between different LLMs (OpenAI, HuggingFace, Mistral, Ollama) and embedding models.
- State Management: Uses LangGraph with MongoDB persistence to manage conversation history.
- Framework: Next.js (App Router)
- Language: TypeScript
- AI/LLM Orchestration: Langchain & LangGraph
- Database: MongoDB Atlas (with Vector Search)
- Styling: Tailwind CSS
- LLM (Primary): OpenAI (Configurable for others like Mistral, HuggingFace, Ollama)
- Embeddings (Primary): OpenAI (Configurable for others like HuggingFace)
- Package Manager: pnpm
Follow these steps to set up and run the project locally.
- Node.js (LTS version recommended)
- pnpm
- A MongoDB Atlas account with a running cluster.
- API Keys:
- OpenAI API Key
- (Optional: Mistral API Key, HuggingFace API Key if configuring different models)
-
Clone the repository:
git clone <your-repo-url> cd <repo-name>
-
Install dependencies:
pnpm install
-
Copy the sample environment file:
cp .env.sample .env
-
Populate the
.envfile with your credentials:# MongoDB Atlas connection string (make sure IP access is configured) MONGODB_URI=mongodb+srv://... # OpenAI API Key OPENAI_API_KEY=sk-... # Optional: Set to true to clear the database before seeding RESET_DB=false # Optional: Add other keys if using different models # MISTRAL_API_KEY=... # HUGGINGFACE_API_KEY=hf_...
-
Configure MongoDB Atlas:
- Ensure you have created a database and collection (names specified in
src/app/utils/config.ts). - Create a Vector Search Index on your collection (details in
src/app/utils/config.tsfor index name and fields).
- Ensure you have created a database and collection (names specified in
-
Seed the database: This script uses the configured LLM (currently OpenAI) to generate synthetic employee data and creates embeddings.
pnpm seed-db
- Note: If you want to clear existing data before seeding, set
RESET_DB=truein your.envfile.
- Note: If you want to clear existing data before seeding, set
-
Start the development server:
pnpm dev
-
Open http://localhost:3000 in your browser.
- Retrieval-Augmented Generation (RAG): Combining LLM capabilities with external knowledge retrieval from a vector database.
- Langchain Agents: Building autonomous agents that can use tools (like database lookups) to answer questions.
- LangGraph: Managing multi-step agent interactions and conversation state.
- MongoDB Atlas Vector Search: Setting up and querying a vector database for semantic similarity search.
- Next.js App Router: Building API routes and server/client components.
- Environment Management: Handling API keys and configuration securely.
- Database Seeding: Programmatically generating and populating database records.
- Model Integration: Swapping between different LLM providers and embedding models within Langchain.
This project is licensed under the MIT License - see the LICENSE file for details.