A conversational AI agent built with LangGraph and MongoDB Atlas that can search movies and maintain conversation memory. This tutorial demonstrates how to build an intelligent agent with vector search, full-text search, and persistent memory capabilities.
- Vector Search: Semantic search through movie plots using embeddings
- Full-text Search: Direct title-based movie lookup
- Conversation Memory: Persistent memory across sessions using MongoDB
- Tool Selection Logging: See which tools the agent chooses to use
- Session Management: Maintain conversation context with thread IDs
- Python 3.8+
- MongoDB Atlas cluster with the sample_mflix dataset
- OpenAI API key (for Azure OpenAI)
- Voyage AI API key (for embeddings)
git clone <your-repo-url>
cd langgraph-agent-0
pip install -r requirements.txt
Create a .env
file in the project root:
MONGODB_URI=mongodb+srv://username:[email protected]/
OPENAI_API_KEY=your_openai_api_key
AZURE_OPENAI_ENDPOINT=your_azure_endpoint
AZURE_OPENAI_API_KEY=your_azure_api_key
VOYAGEAI_API_KEY=your_voyage_api_key
Ensure your MongoDB Atlas cluster has:
- The
sample_mflix
database withembedded_movies
collection - Movie documents with
plot
,title
, andfullplot
fields - Existing plot embeddings in
plot_embedding_voyage_3_large
field
python main.py
The application will:
- Set up search indexes (first run may take ~1-2 minutes)
- Prompt for a session ID
- Start the conversation interface
Enter a session ID: user123
Ask me about movies! Type 'quit' to exit.
Your question: recommend a funny movie
🔧 Using tools: plot_search
Answer: I recommend "Caddyshack" - a hilarious golf comedy with great characters and memorable humor!
Your question: save that I like comedies
🔧 Using tools: save_memory
Answer: Got it! I've saved that you enjoy comedies.
Your question: what do you know about my preferences?
🔧 Using tools: retrieve_memories
Answer: I remember that you like comedies.
main.py
: Entry point and conversation loopconfig.py
: MongoDB setup, models, and index creationagent.py
: LangGraph workflow definitiontools.py
: Movie search tools (vector and full-text)memory.py
: Conversation memory tools
plot_search
: Vector similarity search through movie plotstitle_search
: Full-text search by movie titlesave_memory
: Store important conversation informationretrieve_memories
: Recall stored memories
User Input → Agent → Tool Selection → Tool Execution → Response
↑ ↓
←─────────── Memory Persistence ←─────────────────────
- LLM: Azure OpenAI GPT-4
- Embeddings: Voyage AI voyage-3-large (2048 dimensions)
- Vector Store: MongoDB Atlas Vector Search
- Memory: MongoDB Atlas with vector indexing
The application automatically creates:
- Vector Index: For semantic plot search
- Full-text Index: For title-based search
langgraph-agent-0/
├── main.py # Application entry point
├── config.py # Configuration and setup
├── agent.py # LangGraph agent definition
├── tools.py # Movie search tools
├── memory.py # Memory management tools
├── requirements.txt # Python dependencies
├── .env # Environment variables (create this)
├── .gitignore # Git ignore rules
└── README.md # This file
- Define your tool function with the
@tool
decorator - Add it to the appropriate tools list (
TOOLS
orMEMORY_TOOLS
) - The agent will automatically discover and use it
- Modify the system prompt in
agent.py
- Adjust search parameters in
tools.py
- Change memory configuration in
memory.py
- Index Creation Timeout: Increase
wait_until_complete
values inconfig.py
- Memory Errors: Check MongoDB connection and permissions
- Search Issues: Verify sample_mflix dataset is properly loaded
The agent shows tool selection for transparency:
🔧 Using tools: plot_search, save_memory
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request