Skip to content

davidhou17/langgraph-mongodb-movie-agent

Repository files navigation

LangGraph MongoDB Movie Agent

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.

Features

  • 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

Prerequisites

  • Python 3.8+
  • MongoDB Atlas cluster with the sample_mflix dataset
  • OpenAI API key (for Azure OpenAI)
  • Voyage AI API key (for embeddings)

Setup

1. Clone the Repository

git clone <your-repo-url>
cd langgraph-agent-0

2. Install Dependencies

pip install -r requirements.txt

3. Environment Variables

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

4. MongoDB Setup

Ensure your MongoDB Atlas cluster has:

  • The sample_mflix database with embedded_movies collection
  • Movie documents with plot, title, and fullplot fields
  • Existing plot embeddings in plot_embedding_voyage_3_large field

Usage

Running the Agent

python main.py

The application will:

  1. Set up search indexes (first run may take ~1-2 minutes)
  2. Prompt for a session ID
  3. Start the conversation interface

Example Interactions

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.

Architecture

Components

  • main.py: Entry point and conversation loop
  • config.py: MongoDB setup, models, and index creation
  • agent.py: LangGraph workflow definition
  • tools.py: Movie search tools (vector and full-text)
  • memory.py: Conversation memory tools

Tools Available

  1. plot_search: Vector similarity search through movie plots
  2. title_search: Full-text search by movie title
  3. save_memory: Store important conversation information
  4. retrieve_memories: Recall stored memories

LangGraph Workflow

User Input → Agent → Tool Selection → Tool Execution → Response
     ↑                                                      ↓
     ←─────────── Memory Persistence ←─────────────────────

Configuration

Models Used

  • 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

Search Indexes

The application automatically creates:

  • Vector Index: For semantic plot search
  • Full-text Index: For title-based search

Development

Project Structure

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

Adding New Tools

  1. Define your tool function with the @tool decorator
  2. Add it to the appropriate tools list (TOOLS or MEMORY_TOOLS)
  3. The agent will automatically discover and use it

Customizing the Agent

  • Modify the system prompt in agent.py
  • Adjust search parameters in tools.py
  • Change memory configuration in memory.py

Troubleshooting

Common Issues

  1. Index Creation Timeout: Increase wait_until_complete values in config.py
  2. Memory Errors: Check MongoDB connection and permissions
  3. Search Issues: Verify sample_mflix dataset is properly loaded

Logs

The agent shows tool selection for transparency:

🔧 Using tools: plot_search, save_memory

License

MIT License - see LICENSE file for details.

Contributing

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

About

A conversational AI agent built with LangGraph and MongoDB Atlas for movie search with persistent memory

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages