File size: 2,150 Bytes
7ff7119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Schema-rendszer: JSON schema betöltés + Pydantic mirror választás.

Használat:

    from schemas import load_schema, pydantic_for

    json_schema = load_schema("szamla")          # dict
    pydantic_cls = pydantic_for("szamla")        # InvoiceModel

A 6 doc_type:
  * szamla              → invoice.json + InvoiceModel
  * szallitolevle       → delivery_note.json + DeliveryNoteModel
  * megrendeles         → purchase_order.json + PurchaseOrderModel
  * szerzodes           → contract.json + ContractModel
  * penzugyi_kimutatas  → financial_report.json + FinancialReportModel
  * egyeb               → universal.json + UniversalModel
"""

from __future__ import annotations

import json
from functools import lru_cache
from pathlib import Path

from schemas.flatten_universal import flatten_universal
from schemas.pydantic_models import (
    ContractModel,
    DeliveryNoteModel,
    FinancialReportModel,
    InvoiceModel,
    PurchaseOrderModel,
    UniversalModel,
    pydantic_for,
)

SCHEMA_DIR = Path(__file__).parent

# doc_type → JSON fájlnév (relatív a schemas/ mappához)
SCHEMA_FILES = {
    "szamla": "invoice.json",
    "szallitolevle": "delivery_note.json",
    "megrendeles": "purchase_order.json",
    "szerzodes": "contract.json",
    "penzugyi_kimutatas": "financial_report.json",
    "egyeb": "universal.json",
}


@lru_cache(maxsize=8)
def load_schema(doc_type: str) -> dict:
    """A doc_type-hoz tartozó JSON schema-t adja vissza dict formában.

    Lru_cache: ugyanazt a dict-et adja vissza ismételten (a Pydantic mirror-rel
    együtt használjuk runtime validációhoz; a JSON schema az LLM-nek megy
    `with_structured_output(method="json_schema")`-en át).

    Ismeretlen doc_type → universal.json fallback.
    """
    fname = SCHEMA_FILES.get(doc_type, SCHEMA_FILES["egyeb"])
    path = SCHEMA_DIR / fname
    return json.loads(path.read_text(encoding="utf-8"))


__all__ = [
    "load_schema",
    "pydantic_for",
    "flatten_universal",
    "InvoiceModel",
    "ContractModel",
    "DeliveryNoteModel",
    "PurchaseOrderModel",
    "FinancialReportModel",
    "UniversalModel",
]