This document explains how the Movie Chatbot application loads and manages configuration settings, including API keys and other parameters.
The application uses a priority system for loading configuration values:
- Service Bindings (highest priority): When deployed to Cloud Foundry, the application checks for service bindings first.
- Environment Variables: If a value is not found in service bindings, environment variables are checked.
- config.json: If a value is not found in environment variables, the application looks for it in the
config.jsonfile. - Default Values (lowest priority): If a value is not found in any of the above sources, a default value is used if available.
The following configuration values are required for the application to function properly:
| Name | Description | Required | Default |
|---|---|---|---|
DJANGO_SECRET_KEY |
Secret key for Django | Yes | None |
OPENAI_API_KEY or LLM_API_KEY |
API key for the LLM service | Yes | None |
TMDB_API_KEY |
API key for The Movie Database | Yes | None |
SERPAPI_API_KEY |
API key for SerpAPI (for theater data) | Recommended | None |
The application uses Gunicorn as the WSGI server. The following configuration options are available:
| Name | Description | Required | Default |
|---|---|---|---|
--timeout |
Worker timeout in seconds | No | 30 |
The worker timeout is set in the Procfile and manifest.yml:
# In Procfile
web: gunicorn movie_chatbot.wsgi --log-file - --timeout 600
# In manifest.yml
command: python manage.py makemigrations chatbot && python manage.py migrate && gunicorn movie_chatbot.wsgi --log-file - --timeout 600The default worker timeout is 30 seconds, but we've increased it to 600 seconds to accommodate longer LLM API calls. If you're experiencing worker timeout issues, you may need to increase this value further.
When deployed to Cloud Foundry, the application can use service bindings to provide configuration values. This is the recommended approach for production deployments.
Example of creating and binding a service:
# Create a user-provided service with configuration values
cf create-user-provided-service movie-chatbot-config -p '{"TMDB_API_KEY":"your-api-key"}'
# Bind the service to the application
cf bind-service movie-chatbot movie-chatbot-configEnvironment variables can be set in various ways:
- In a
.envfile for local development - Using
cf set-envfor Cloud Foundry deployments - In the
manifest.ymlfile for Cloud Foundry deployments
Example .env file:
DJANGO_SECRET_KEY=your-secret-key
OPENAI_API_KEY=your-openai-api-key
TMDB_API_KEY=your-tmdb-api-key
SERPAPI_API_KEY=your-serpapi-api-key
Example of setting environment variables in Cloud Foundry:
cf set-env movie-chatbot TMDB_API_KEY your-tmdb-api-keyThe config.json file can be used to provide configuration values. This is useful for local development or when deploying to environments that don't support environment variables or service bindings.
Example config.json file:
{
"DJANGO_SECRET_KEY": "your-secret-key",
"OPENAI_API_KEY": "your-openai-api-key",
"TMDB_API_KEY": "your-tmdb-api-key",
"SERPAPI_API_KEY": "your-serpapi-api-key"
}The application validates required configuration values during startup. If any required values are missing, error messages will be logged.
You can check the logs to see if all required configuration values are present:
# For local development
python manage.py runserver
# For Cloud Foundry deployments
cf logs movie-chatbot --recentIf you encounter configuration-related issues, check the following:
- Missing API Keys: Ensure that all required API keys are provided through one of the configuration sources.
- Service Binding Issues: If using Cloud Foundry, check that the service bindings are correctly set up.
- Environment Variables: Verify that environment variables are correctly set.
- config.json: Check that the
config.jsonfile exists and contains the correct values.
CRITICAL: TMDB API key is missing. Movie image enhancement will be unavailable.: The TMDB API key is missing. Provide it through one of the configuration sources.CRITICAL: LLM API key is missing. The application will not function correctly.: The LLM API key is missing. Provide it through one of the configuration sources.Error in process_query: 1 validation error for EnhanceMovieImagesTool: This error occurs when the TMDB API key is missing or invalid.
When adding new configuration values to the application, follow these guidelines:
-
Use the
config_loader.pyfunctions to load the values:get_config(key, default=None): Get a configuration value with fallback to defaultget_required_config(key): Get a required configuration value (raises an error if not found)get_int_config(key, default=None): Get a configuration value as an integerget_float_config(key, default=None): Get a configuration value as a floatget_bool_config(key, default=None): Get a configuration value as a boolean
-
Add the new configuration value to the validation function in
validation.pyif it's required. -
Document the new configuration value in this guide.
Example of adding a new configuration value:
# In a settings file
from . import config_loader
# Get a configuration value with a default
MY_CONFIG_VALUE = config_loader.get_config('MY_CONFIG_VALUE', 'default-value')
# Get a required configuration value
MY_REQUIRED_VALUE = config_loader.get_required_config('MY_REQUIRED_VALUE')
# Get a configuration value as an integer
MY_INT_VALUE = config_loader.get_int_config('MY_INT_VALUE', 10)