Environment Variable Setup
Environment Variable Setup
To use Udility Diffuser, you must provide an OpenRouter API Key. This key allows the package to communicate with the Meta Llama-3.1/3.5 models via OpenRouter's API.
Obtaining an API Key
- Visit OpenRouter.ai.
- Create an account or sign in.
- Navigate to the Keys section in your dashboard and generate a new API key.
- Copy this key; you will need to set it as an environment variable named
OPENROUTER_API_KEY.
Configuration Methods
You can configure the API key using one of the following methods, depending on your workflow.
Method 1: Inline via Python (Recommended for Notebooks/Colab)
If you are working in a Jupyter Notebook or Google Colab, you can set the environment variable directly in your script before importing the diffuser module.
import os
# Set your API key
os.environ['OPENROUTER_API_KEY'] = 'your_openrouter_api_key_here'
# Now you can use Udility
from Udility import diffuser
Method 2: Persistent Setup (Terminal/Shell)
To avoid setting the key every time you run a script, you can export it to your system's environment.
macOS and Linux:
Add the following line to your shell profile (e.g., ~/.bashrc or ~/.zshrc):
export OPENROUTER_API_KEY='your_openrouter_api_key_here'
Windows (Command Prompt):
setx OPENROUTER_API_KEY "your_openrouter_api_key_here"
Method 3: Using a .env file
For local development, you can create a .env file in your project root:
OPENROUTER_API_KEY=your_openrouter_api_key_here
Note: If using this method, ensure you have python-dotenv installed and call load_dotenv() at the start of your application.
Error Handling
If the environment variable is not set or is inaccessible, the Udility.diffuser module will raise an EnvironmentError when it is initialized:
EnvironmentError: The OpenRouter API key is not set. Please set it as an environment variable.
If you encounter this error, verify that OPENROUTER_API_KEY is correctly spelled and that the variable is set in the specific shell or environment where your Python process is running.