|
|
import os |
|
|
from pydantic import Field, ConfigDict, field_validator |
|
|
from pydantic_settings import BaseSettings |
|
|
from pathlib import Path |
|
|
from dotenv import load_dotenv |
|
|
from pydantic import ValidationError |
|
|
import sys |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
class OpenAIApiKey(BaseSettings): |
|
|
"""Schema for validating and loading the OpenAI API key configuration. |
|
|
""" |
|
|
model_config = ConfigDict( |
|
|
title="OpenAI API Key Schema", |
|
|
description="Validates and loads the OpenAI API key from environment variables.", |
|
|
) |
|
|
api_key: str = Field( |
|
|
..., |
|
|
title="OpenAI API Key", |
|
|
description="API key for OpenAI authentication.", |
|
|
examples=["sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"], |
|
|
alias="OPENAI_API_KEY", |
|
|
) |
|
|
|
|
|
@field_validator("api_key") |
|
|
@classmethod |
|
|
def validate_openai_api_key(cls, v: str) -> str: |
|
|
"""Validate that the API key is present and has the correct format. |
|
|
""" |
|
|
if not v: |
|
|
raise ValueError( |
|
|
"π₯ Missing `OPENAI_API_KEY` environment variable." |
|
|
) |
|
|
if not v.startswith("sk-"): |
|
|
raise ValueError( |
|
|
"π₯ Invalid `OPENAI_API_KEY` β must start with 'sk-'." |
|
|
) |
|
|
return v |
|
|
|
|
|
@classmethod |
|
|
def validate_environment(cls) -> "OpenAIApiKey": |
|
|
""" |
|
|
Load .env from the root directory |
|
|
and validate that the API key is present and valid. |
|
|
""" |
|
|
try: |
|
|
|
|
|
config = cls() |
|
|
os.environ["OPENAI_API_KEY"] = config.api_key |
|
|
return config |
|
|
except ValidationError as e: |
|
|
print(f"π₯ OpenAI API key misconfiguration:\n{e}") |
|
|
sys.exit(1) |
|
|
|