6a. config.py

config.py Breakdown

Centralized configuration loader for API endpoints and authorization headers. Pulls values from .env and defines fallbacks for local use.


1. Load  .env  File

from dotenv import load_dotenv
load_dotenv()
Purpose Loads environment variables from a .env file into Python
Fallback Defaults are used if the .env file or variables are missing

2. Ollama API Configuration

OLLAMA_URL = os.getenv("OLLAMA_URL", "http://localhost:11434").rstrip("/")
OLLAMA_EMBEDDING_URL = os.getenv("OLLAMA_EMBEDDING_URL", f"{OLLAMA_URL}/api/embeddings")
Variable Description Default
OLLAMA_URL Base URL for Ollama API http://localhost:11434
OLLAMA_EMBEDDING_URL Embedding endpoint ${OLLAMA_URL}/api/embeddings

3. Open WebUI API Configuration

OPEN_WEBUI_URL = os.getenv("OPEN_WEBUI_URL", "http://localhost:3000").rstrip("/")
OPEN_WEBUI_TOKEN = os.getenv("OPEN_WEBUI_TOKEN", "Bearer YOUR_OPEN_WEBUI_TOKEN")
Variable Description Default
OPEN_WEBUI_URL Base URL for Open WebUI http://localhost:3000
OPEN_WEBUI_TOKEN Bearer token for API authentication Placeholder token

4. HTTP Headers

HEADERS = {
    "Authorization": OPEN_WEBUI_TOKEN,
    "Content-Type": "application/json"
}
Key Value
Authorization Bearer token pulled from .env
Content-Type JSON encoding for API payloads

Used by functions that interact with Open WebUI such as:


.env Example

# Ollama configuration
OLLAMA_URL=http://localhost:11434
OLLAMA_EMBEDDING_URL=http://localhost:11434/api/embeddings

# Open WebUI configuration
OPEN_WEBUI_URL=http://localhost:3000
OPEN_WEBUI_TOKEN=Bearer sk-abc123xyz

Summary Table

Variable Role Default Used In
OLLAMA_URL Base URL for Ollama API http://localhost:11434 Ollama-related calls
OLLAMA_EMBEDDING_URL Embedding endpoint ${OLLAMA_URL}/api/embeddings Ollama embedding
OPEN_WEBUI_URL Base URL for WebUI http://localhost:3000 WebUI calls
OPEN_WEBUI_TOKEN Bearer token Bearer YOUR_OPEN_WEBUI_TOKEN Auth header
HEADERS API request headers Constructed All WebUI API requests