This example demonstrates a movie chatbot built with Django and CrewAI that can be deployed to Tanzu Platform for Cloud Foundry and integrate with LLM services through the GenAI tile.
- Modern React frontend with optimized component architecture
- Conversational interface to find movies based on interests or topics
- Two conversation modes:
- First Run Mode: Find movies currently in theaters with showtimes
- Casual Viewing Mode: Discover movies from any time period
- Recommends top 3 movie choices based on user preferences
- Shows nearby theaters where movies are playing with available showtimes
- Uses CrewAI to coordinate multiple AI agents working together
- Responsive web interface with real-time UI feedback
- Automatic location detection with multiple fallback mechanisms
- Flexible deployment to Cloud Foundry with GenAI tile integration
The application demonstrates an advanced multi-agent AI architecture with a React-based frontend:
graph TD
User(User) --> WebUI(React Web UI)
WebUI --> APIEndpoints(Django API Endpoints)
APIEndpoints --> MovieCrewManager(Movie Crew Manager)
subgraph "Frontend Architecture"
WebUI --> AppContextProvider(AppContext Provider)
AppContextProvider --> ChatComponents(Chat Components)
AppContextProvider --> MovieComponents(Movie Components)
AppContextProvider --> TheaterComponents(Theater Components)
ChatComponents --> APICalls(API Service Calls)
MovieComponents --> APICalls
TheaterComponents --> APICalls
end
subgraph "CrewAI Orchestration"
MovieCrewManager --> MovieFinder(Movie Finder Agent)
MovieCrewManager --> Recommender(Recommendation Agent)
MovieCrewManager --> TheaterFinder(Theater Finder Agent)
MovieFinder --> TMDbAPI[(TMDb API)]
TheaterFinder --> GeoServices[(Location Services)]
TheaterFinder --> SerpAPI[(SerpAPI Showtimes)]
end
WebUI --> Location(Location Detection)
Location --> BrowserGeo(Browser Geolocation)
Location --> IPApiGeo(ipapi.co Geolocation)
Location --> UserEntry(Manual User Entry)
The application consists of:
-
Modern Frontend Architecture:
- React-based UI using functional components and hooks
- Context API for state management
- React Query for data fetching and caching
- Suspense and lazy loading for performance optimization
- Component-based architecture for maintainability
- Responsive design for all devices
-
Django Backend Framework:
- RESTful API endpoints for chat interactions
- Session management for conversation tracking
- Database models for conversation history and recommendations
- Asynchronous processing for theater and showtime data
-
CrewAI Integration:
- Orchestrates specialized AI agents working together to process requests
- Movie Finder Agent: Searches for movies matching user criteria using TMDb API
- Recommendation Agent: Analyzes and ranks movie choices based on user preferences
- Theater Finder Agent: Finds nearby theaters and real-time showtimes
-
Service Binding:
- Connects to LLM services provided by the GenAI tile through a standard interface
- Automatic configuration based on environment
- Python 3.12+ and pip
- Node.js 18+ and npm
- Cloud Foundry CLI (for deployment)
- Access to Tanzu Platform for Cloud Foundry with GenAI tile installed
- API keys for external services:
-
Clone the repository:
git clone https://github.com/cf-toolsuite/tanzu-genai-showcase cd tanzu-genai-showcase/py-django-crewai -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate
-
Install backend dependencies:
pip install -r requirements.txt
-
Install frontend dependencies:
cd frontend npm install cd ..
-
Create a
.envfile with your API keys and configuration options (for local development only):# Django secret DJANGO_SECRET_KEY=any_old_django_hash # Required API keys OPENAI_API_KEY=your_llm_api_key_here LLM_BASE_URL=optional_custom_endpoint LLM_MODEL=gpt-4o-mini TMDB_API_KEY=your_movie_db_api_key_here SERPAPI_API_KEY=optional_serpapi_key_for_real_showtimes # Optional configuration parameters MOVIE_RESULTS_LIMIT=5 # Number of movie results to return from search MAX_RECOMMENDATIONS=3 # Maximum number of recommended movies to show THEATER_SEARCH_RADIUS_MILES=15 # Radius in miles to search for theaters DEFAULT_SEARCH_START_YEAR=1900 # Default start year for historical movie searches # Optional API request configuration API_REQUEST_TIMEOUT_SECONDS=180 # Maximum seconds to wait for API responses API_MAX_RETRIES=10 # Maximum number of retry attempts for failed API requests API_RETRY_BACKOFF_FACTOR=1.3 # Exponential backoff factor between retries (in seconds) # Optional SerpAPI request configuration SERPAPI_REQUEST_BASE_DELAY=5.0 # Base delay between theater requests for different movies (seconds) SERPAPI_PER_MOVIE_DELAY=2.0 # Additional delay per movie processed (seconds) SERPAPI_MAX_RETRIES=2 # Maximum retries for SerpAPI requests SERPAPI_BASE_RETRY_DELAY=3.0 # Base delay for exponential backoff during retries (seconds) SERPAPI_RETRY_MULTIPLIER=1.5 # Multiplier for exponential backoff during retries
-
Build the frontend:
cd frontend npm run build cd ..
-
Run migrations:
python manage.py makemigrations chatbot python manage.py migrate
-
Start the development server:
python manage.py runserver
-
Open your browser to
http://localhost:8000 -
For frontend development with hot reloading:
cd frontend npm startThis will start a development server at
http://localhost:8080that proxies API requests to the Django backend.
The application offers two distinct conversation modes:
-
First Run Mode: Default tab for finding movies currently in theaters with showtimes
- This mode uses all three agents (Movie Finder, Recommender, Theater Finder)
- Shows theaters and showtimes for current movies
- Uses geolocation to find nearby theaters
-
Casual Viewing Mode: Switch to this tab for historical movie recommendations
- This mode uses two agents (Movie Finder, Recommender)
- Allows exploration of movies from any time period
- No theater information (focuses on recommendations only)
Each mode maintains its own conversation history and movie recommendations for a streamlined user experience.
-
Switch to frontend directory
cd frontend -
Set up .env file as described in Local Development instructions.
-
Ensure webpack and Django are installed.
npm install --save-dev clean-webpack-plugin pip3 install Django
-
Create a production-ready build:
cd frontend npm run build cd .. python manage.py collectstatic --noinput
-
Set up database:
- For local testing, SQLite is fine
- For production, configure a PostgreSQL database via DATABASE_URL
- Cloud Foundry CLI installed and configured
- Access to a Tanzu Platform for Cloud Foundry environment
- GenAI tile installed in the target environment
-
Login to your Cloud Foundry instance:
cf login -a API_ENDPOINT -o YOUR_ORG -s YOUR_SPACE
-
Deploy the application:
# You have two options: # 1. Download dependencies during build phase cf push --no-start # Or # 2. Download dependencies in advance into vendor directory ./deploy-on-tp4cf.sh
-
Bind to a GenAI service instance:
# Discover available GenAI tile service offering plans cf marketplace -e genai # Create a GenAI service instance cf create-service genai PLAN_NAME movie-chatbot-llm # Bind the application to the service cf bind-service movie-chatbot movie-chatbot-llm
[!IMPORTANT] Replace
PLAN_NAMEabove with an available plan from the GenAI tile service offering -
Add hash and service API keys
cf set-env movie-chatbot DJANGO_SECRET_KEY any_old_django_hash cf set-env movie-chatbot TMDB_API_KEY your_movie_db_api_key cf set-env movie-chatbot SERPAPI_API_KEY your_serpapi_key_for_real_showtimes
[!IMPORTANT] Replace the hash and API key values above with authorized key values from each service. For
DJANGO_SECRET_KEY, this can be any value, it's used for hashing purposes. -
Start application
cf start movie-chatbot
-
Verify the deployment:
cf apps cf routes
The application automatically integrates with the GenAI tile through service binding:
-
Automatic Detection:
- When deployed to Cloud Foundry, the application automatically detects the
VCAP_SERVICESenvironment variables - These variables contain the bound service instances and their credentials
- When deployed to Cloud Foundry, the application automatically detects the
-
Credential Extraction:
- The application parses the
VCAP_SERVICESto extract LLM service credentials - It looks for services labeled 'genai' or named 'movie-chatbot-llm'
- It extracts the API key, base URL, and model name
- The application parses the
-
Fallback Mechanism:
- If no bound services are found, the application falls back to environment variables
- This allows for flexibility in different deployment scenarios
-
CrewAI Configuration:
- The extracted credentials are used to configure the CrewAI agents
- Agents are initialized with the appropriate LLM service configuration
- This allows the agents to make API calls to the LLM service
The frontend is built with modern React practices:
-
Component Structure:
- Organized by feature (Chat, Movies, Theaters)
- Lazy-loaded components for performance
- Suspense for loading states
-
State Management:
- React Context API for shared state
- React Query for API state
- Localized state where appropriate
-
API Integration:
- Centralized API service
- Error handling and retry logic
- Response caching for improved performance
-
User Experience:
- Progress indicators for long-running operations
- Responsive design for all devices
- Informative error messages with retry options
-
Missing API Keys:
- Ensure you've set up all required API keys in your
.envfile for local development - For Cloud Foundry deployments, verify your service binding is correct
- Ensure you've set up all required API keys in your
-
Location Detection Issues:
- The application requires a US location for theater information
- If automatic location detection fails, you can manually enter a US city and state
- Check browser console for geolocation errors
-
Movie Recommendations Not Showing:
- Verify your TMDb API key is valid
- Check network requests in browser dev tools to see API responses
- Look for specific error messages in the server logs
-
Theater Data Not Appearing:
- Theater data is only shown for current release movies
- Theater search is US-only (limited by SerpAPI capabilities)
- Check that location is correctly detected or entered
-
LLM Service Connection Issues:
- Verify API key and endpoint configuration
- Check service binding in Cloud Foundry (cf services)
- Try restaging the application after binding (cf restage app-name)
To enable debug logging:
-
For local development, add to your
.envfile:DEBUG=True LOG_LEVEL=DEBUG
-
For Cloud Foundry, set environment variables:
cf set-env movie-chatbot DEBUG True cf set-env movie-chatbot LOG_LEVEL DEBUG cf restage movie-chatbot
-
View logs:
cf logs movie-chatbot --recent