code / legex /prompts /v2.py
anonymous
[code] Initial release of the code.
6f5156a
"""Per-column system prompts: one focused LLM call per Classification field.
Each prompt is built from the column's coding rule (Vorlage
`Variables_Coding_Rules` sheet) plus the small targeted instructions it
needs (ISIC list for industry fields, currency hint for the `currency_*`
fields, the "nonpecuniary" exception for `dispute_value_nominal`).
The LLM is told to return JSON `{"<column>": <value>}` so the response
can be validated by the same `Classification` model used in v1 — we just
look at the one field we asked for.
"""
from legex.models.classification import Classification
MODE = "per_column"
_PREAMBLE = (
"You extract a single variable from a court judgment. Read the "
"judgment text the user provides and return a JSON object with "
"exactly one key: the variable name below. The value must be the "
"typed answer or JSON null. Do not invent information that is not "
"in the text. Use native JSON types — a number is a number, not a "
"string. Money amounts are JSON numbers; dates are JSON strings in "
"YYYY-MM-DD format; counts are JSON integers."
)
_DISPUTE_TAIL = 'The value may also be the literal string "nonpecuniary" for non-monetary disputes.'
_ISIC_TAIL = (
"The value must be one of the coded values listed below.\n\n"
"## Allowed ISIC industry categories\n\n{isic_block}"
)
_CURRENCY_TAIL = (
"Return the ISO-4217 currency code of the local currency of the "
"proceedings (e.g. CHF for Switzerland, EUR for France/Germany/"
"Belgium, GBP for the UK, AUD for Australia, NZD for New Zealand). "
"Return null if the corresponding amount in the judgment is null."
)
_ISIC_COLUMNS = {
"plaintiff_no1_ISIC1_industry_category",
"defendant_no1_ISIC1_industry_category",
}
_CURRENCY_COLUMNS = {
"Currency_dispute_value_nominal",
"Currency_court_cost_awarded_nominal",
"Currency_party_compensation_awarded_nominal",
}
def build_columns(
rules: list[tuple[str, str]],
isic: list[tuple[str, str, str]],
) -> dict[str, str]:
"""Returns {csv_column_name: system_prompt}.
Keys use the field's alias when set (e.g. `Currency_*` capital C) so
they line up with the GOLDENSET header. Python attribute names stay
lowercase on the `Classification` model.
"""
rules_by_name = {name: explanation for name, explanation in rules}
isic_block = "\n".join(
f"- {coded_value}{category}: {description}"
for coded_value, category, description in isic
)
out: dict[str, str] = {}
for field, info in Classification.model_fields.items():
col = info.alias or field
rule = rules_by_name.get(col) or rules_by_name.get(field, "")
body = (
f"{_PREAMBLE}\n\n"
f"## Variable: {col}\n\n"
f"{rule}\n\n"
f'Return JSON in the form: {{"{col}": <value>}}'
)
if col == "dispute_value_nominal":
body += f"\n\n{_DISPUTE_TAIL}"
if col in _ISIC_COLUMNS:
body += "\n\n" + _ISIC_TAIL.format(isic_block=isic_block)
if col in _CURRENCY_COLUMNS:
body += f"\n\n{_CURRENCY_TAIL}"
out[col] = body
return out