Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:PacktPublishing LLM Engineers Handbook Settings Load

From Leeroopedia


Knowledge Sources
Domains Configuration, Infrastructure, Secrets_Management
Last Updated 2026-02-08 08:00 GMT

Overview

Concrete tool for loading and managing all application configuration from environment variables, .env files, or the ZenML secret store.

Description

The Settings class extends Pydantic's BaseSettings to declare typed configuration fields covering OpenAI, HuggingFace, Comet ML, MongoDB, Qdrant, AWS, SageMaker, RAG models, and LinkedIn credentials. The load_settings() class method first attempts to read secrets from the ZenML secret store and falls back to .env file / default values. The export() method pushes current settings into ZenML for pipeline consumption. A computed OPENAI_MAX_TOKEN_WINDOW property returns 90% of the official token limit for the configured OpenAI model. A module-level settings singleton is instantiated at import time, making configuration available globally via from llm_engineering.settings import settings.

Usage

Import the settings singleton whenever you need to access any project configuration value. This is the centralized configuration hub used by every subsystem: data pipelines, model training, inference, RAG, and deployment. The singleton is auto-loaded at import time so no explicit initialization is required.

Code Reference

Source Location

Signature

class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")

    # --- Required settings even when working locally. ---
    OPENAI_MODEL_ID: str = "gpt-4o-mini"
    OPENAI_API_KEY: str | None = None
    HUGGINGFACE_ACCESS_TOKEN: str | None = None
    COMET_API_KEY: str | None = None
    COMET_PROJECT: str = "twin"

    # --- Required settings when deploying the code. ---
    DATABASE_HOST: str = "mongodb://llm_engineering:llm_engineering@127.0.0.1:27017"
    DATABASE_NAME: str = "twin"
    USE_QDRANT_CLOUD: bool = False
    QDRANT_DATABASE_HOST: str = "localhost"
    QDRANT_DATABASE_PORT: int = 6333
    QDRANT_CLOUD_URL: str = "str"
    QDRANT_APIKEY: str | None = None
    AWS_REGION: str = "eu-central-1"
    AWS_ACCESS_KEY: str | None = None
    AWS_SECRET_KEY: str | None = None
    AWS_ARN_ROLE: str | None = None

    # --- Optional settings used to tweak the code. ---
    HF_MODEL_ID: str = "mlabonne/TwinLlama-3.1-8B-DPO"
    GPU_INSTANCE_TYPE: str = "ml.g5.2xlarge"
    SM_NUM_GPUS: int = 1
    MAX_INPUT_LENGTH: int = 2048
    MAX_TOTAL_TOKENS: int = 4096
    MAX_BATCH_TOTAL_TOKENS: int = 4096
    COPIES: int = 1
    GPUS: int = 1
    CPUS: int = 2
    SAGEMAKER_ENDPOINT_CONFIG_INFERENCE: str = "twin"
    SAGEMAKER_ENDPOINT_INFERENCE: str = "twin"
    TEMPERATURE_INFERENCE: float = 0.01
    TOP_P_INFERENCE: float = 0.9
    MAX_NEW_TOKENS_INFERENCE: int = 150
    TEXT_EMBEDDING_MODEL_ID: str = "sentence-transformers/all-MiniLM-L6-v2"
    RERANKING_CROSS_ENCODER_MODEL_ID: str = "cross-encoder/ms-marco-MiniLM-L-4-v2"
    RAG_MODEL_DEVICE: str = "cpu"
    LINKEDIN_USERNAME: str | None = None
    LINKEDIN_PASSWORD: str | None = None

    @property
    def OPENAI_MAX_TOKEN_WINDOW(self) -> int: ...

    @classmethod
    def load_settings(cls) -> "Settings": ...

    def export(self) -> None: ...

Import

from llm_engineering.settings import settings

I/O Contract

Inputs

Name Type Required Description
.env file File No Environment file with key=value pairs; auto-loaded by Pydantic
Environment variables OS env No Override any setting via matching env var name
ZenML secret "settings" ZenML Secret No Preferred source; tried first by load_settings()

Outputs

Name Type Description
settings (singleton) Settings Module-level instance with all configuration loaded
OPENAI_MAX_TOKEN_WINDOW int 90% of the official token limit for the configured model

Usage Examples

Accessing Configuration

from llm_engineering.settings import settings

# Access database configuration
print(settings.DATABASE_HOST)
print(settings.DATABASE_NAME)

# Access API keys
if settings.OPENAI_API_KEY:
    print("OpenAI configured")

# Access computed property
max_tokens = settings.OPENAI_MAX_TOKEN_WINDOW
print(f"Safe token window: {max_tokens}")

Exporting Settings to ZenML

from llm_engineering.settings import settings

# Push all settings to ZenML secret store for pipeline access
settings.export()
# Note: Will warn if secret "settings" already exists

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment