Use the unified pydantic-settings configuration in run_llm
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
# Tools for A Feminist Audit of Pop Music.
|
||||
# Copyright 2026 imacat. All rights reserved.
|
||||
# Authors:
|
||||
# imacat@mail.imacat.idv.tw (imacat), 2026/7/31
|
||||
"""The configuration.
|
||||
|
||||
"""
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""The configuration settings."""
|
||||
app_name: str = "Tools for A Feminist Audit of Pop Music"
|
||||
"""The application name."""
|
||||
admin_email: str = "imacat@mail.imacat.idv.tw"
|
||||
"""The administrator email address."""
|
||||
SQLALCHEMY_DATABASE_URL: str
|
||||
"""The SQLAlchemy database URL."""
|
||||
ANTHROPIC_API_KEY: str
|
||||
"""The Anthropic API key."""
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
|
||||
"""The model configuration."""
|
||||
|
||||
|
||||
__settings: Settings | None = None
|
||||
"""The configuration settings."""
|
||||
|
||||
|
||||
def get_settings() -> Settings:
|
||||
"""Returns the configuration settings.
|
||||
|
||||
:return: The configuration settings.
|
||||
"""
|
||||
global __settings
|
||||
if __settings is None:
|
||||
__settings = Settings()
|
||||
return __settings
|
||||
|
||||
|
||||
def set_settings(settings: Settings) -> None:
|
||||
"""Sets the configuration settings.
|
||||
|
||||
:param settings: The configuration settings.
|
||||
:return: None.
|
||||
"""
|
||||
global __settings
|
||||
__settings = settings
|
||||
@@ -14,7 +14,6 @@ arbitration batch, and archives every artifact self-contained under
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
@@ -23,6 +22,8 @@ from typing import Any
|
||||
|
||||
import anthropic
|
||||
|
||||
from .config import get_settings
|
||||
|
||||
type Item = dict[str, str]
|
||||
"""An input item with "id" and "content"."""
|
||||
|
||||
@@ -135,49 +136,6 @@ def load_items(path: Path) -> list[Item]:
|
||||
return items
|
||||
|
||||
|
||||
def parse_env_file(path: Path) -> dict[str, str]:
|
||||
"""Parse a simple KEY=VALUE .env file.
|
||||
|
||||
Blank lines and lines starting with "#" are ignored.
|
||||
|
||||
:param path: The path of the .env file.
|
||||
:return: The key-value pairs; empty when the file is missing.
|
||||
"""
|
||||
values: dict[str, str] = {}
|
||||
if not path.is_file():
|
||||
return values
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
stripped: str = line.strip()
|
||||
if stripped == "" or stripped.startswith("#"):
|
||||
continue
|
||||
if "=" not in stripped:
|
||||
continue
|
||||
key, _, value = stripped.partition("=")
|
||||
values[key.strip()] = value.strip()
|
||||
return values
|
||||
|
||||
|
||||
def resolve_api_key(env_path: Path) -> str:
|
||||
"""Resolve the Anthropic API key.
|
||||
|
||||
The ``ANTHROPIC_API_KEY`` environment variable takes precedence;
|
||||
the .env file is consulted as a fallback.
|
||||
|
||||
:param env_path: The path of the .env file.
|
||||
:return: The API key.
|
||||
:raises RuntimeError: When no API key can be found.
|
||||
"""
|
||||
key: str | None = os.environ.get("ANTHROPIC_API_KEY")
|
||||
if key:
|
||||
return key
|
||||
key = parse_env_file(env_path).get("ANTHROPIC_API_KEY")
|
||||
if key:
|
||||
return key
|
||||
raise RuntimeError(
|
||||
"ANTHROPIC_API_KEY is not set in the environment and not"
|
||||
f" found in {env_path}")
|
||||
|
||||
|
||||
def build_request(item: Item, system_prompt: str,
|
||||
max_tokens: int) -> dict[str, Any]:
|
||||
"""Build one Message Batches request for an input item.
|
||||
@@ -571,13 +529,8 @@ def main(argv: list[str] | None = None) -> int:
|
||||
print(f"dry run: archive created at {run_dir}",
|
||||
file=sys.stderr)
|
||||
return 0
|
||||
try:
|
||||
api_key: str = resolve_api_key(Path(".env"))
|
||||
except RuntimeError as error:
|
||||
print(f"error: {error}", file=sys.stderr)
|
||||
return 1
|
||||
client: anthropic.Anthropic = anthropic.Anthropic(
|
||||
api_key=api_key)
|
||||
api_key=get_settings().ANTHROPIC_API_KEY)
|
||||
run1: Results
|
||||
run2: Results
|
||||
run1, run2 = execute_runs(
|
||||
|
||||
Reference in New Issue
Block a user