code / legex /prompts /__init__.py
anonymous
[code] Initial release of the code.
6f5156a
"""Versioned system prompts for `legex-classify`.
Each version lives in `legex/prompts/{version}.py` and exposes one of:
# single-call mode (one LLM call per case)
PROMPT: str — template with {rules_block}/{isic_block}
build(rules, isic) -> str — returns the filled prompt
# per-column mode (one LLM call per Classification field per case)
MODE = "per_column"
build_columns(rules, isic) -> dict[column_name, system_prompt]
`load_plan(...)` normalises both shapes into a `PromptPlan`.
"""
from dataclasses import dataclass
from importlib import import_module
@dataclass
class PromptPlan:
mode: str # "single" or "per_column"
system: str | None = None
column_systems: dict[str, str] | None = None
def load_plan(
version: str,
rules: list[tuple[str, str]],
isic: list[tuple[str, str, str]],
) -> PromptPlan:
try:
mod = import_module(f"legex.prompts.{version}")
except ImportError as e:
raise ValueError(f"Unknown prompt version: {version!r}") from e
mode = getattr(mod, "MODE", "single")
if mode == "per_column":
return PromptPlan(mode="per_column", column_systems=mod.build_columns(rules, isic))
return PromptPlan(mode="single", system=mod.build(rules, isic))
def build_prompt(
version: str,
rules: list[tuple[str, str]],
isic: list[tuple[str, str, str]],
) -> str:
"""Single-mode shortcut. Raises if the version is per-column."""
plan = load_plan(version, rules, isic)
if plan.mode != "single" or plan.system is None:
raise ValueError(
f"build_prompt() is single-mode only; prompt version {version!r} is {plan.mode}"
)
return plan.system