File size: 6,843 Bytes
d520909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""
Document Classification Schemas

Pydantic models for document type classification and categorization.
"""

from enum import Enum
from typing import List, Dict, Any, Optional
from pydantic import BaseModel, Field

from .core import EvidenceRef


class DocumentType(str, Enum):
    """
    Common document types for classification.
    Extensible for domain-specific types.
    """
    # Legal & Business
    CONTRACT = "contract"
    INVOICE = "invoice"
    RECEIPT = "receipt"
    PURCHASE_ORDER = "purchase_order"
    AGREEMENT = "agreement"
    NDA = "nda"
    TERMS_OF_SERVICE = "terms_of_service"

    # Technical & Scientific
    PATENT = "patent"
    RESEARCH_PAPER = "research_paper"
    TECHNICAL_REPORT = "technical_report"
    SPECIFICATION = "specification"
    DATASHEET = "datasheet"
    USER_MANUAL = "user_manual"

    # Financial
    FINANCIAL_REPORT = "financial_report"
    BANK_STATEMENT = "bank_statement"
    TAX_FORM = "tax_form"
    BALANCE_SHEET = "balance_sheet"
    INCOME_STATEMENT = "income_statement"

    # Identity & Administrative
    ID_DOCUMENT = "id_document"
    PASSPORT = "passport"
    DRIVERS_LICENSE = "drivers_license"
    CERTIFICATE = "certificate"
    FORM = "form"
    APPLICATION = "application"

    # Medical
    MEDICAL_RECORD = "medical_record"
    PRESCRIPTION = "prescription"
    LAB_REPORT = "lab_report"
    INSURANCE_CLAIM = "insurance_claim"

    # General
    LETTER = "letter"
    EMAIL = "email"
    MEMO = "memo"
    PRESENTATION = "presentation"
    SPREADSHEET = "spreadsheet"
    REPORT = "report"
    ARTICLE = "article"
    BOOK = "book"

    # Catch-all
    OTHER = "other"
    UNKNOWN = "unknown"


class ClassificationScore(BaseModel):
    """Score for a single document type classification."""
    document_type: DocumentType = Field(..., description="Document type")
    confidence: float = Field(..., ge=0.0, le=1.0, description="Classification confidence")
    reasoning: Optional[str] = Field(default=None, description="Reasoning for classification")


class DocumentClassification(BaseModel):
    """
    Document classification result with confidence scores.
    """
    document_id: str = Field(..., description="Document identifier")

    # Primary classification
    primary_type: DocumentType = Field(..., description="Most likely document type")
    primary_confidence: float = Field(
        ...,
        ge=0.0,
        le=1.0,
        description="Confidence in primary classification"
    )

    # All classification scores
    scores: List[ClassificationScore] = Field(
        default_factory=list,
        description="Scores for all considered types"
    )

    # Evidence
    evidence: List[EvidenceRef] = Field(
        default_factory=list,
        description="Evidence supporting classification"
    )

    # Classification metadata
    method: str = Field(
        default="llm",
        description="Classification method used (llm/rule-based/hybrid)"
    )
    model_used: Optional[str] = Field(default=None, description="Model used for classification")

    # Warnings and flags
    is_confident: bool = Field(
        default=True,
        description="Whether classification meets confidence threshold"
    )
    warnings: List[str] = Field(default_factory=list, description="Classification warnings")
    needs_human_review: bool = Field(
        default=False,
        description="Whether human review is recommended"
    )

    # Additional attributes detected
    attributes: Dict[str, Any] = Field(
        default_factory=dict,
        description="Additional detected attributes (language, domain, etc.)"
    )

    def get_top_k(self, k: int = 3) -> List[ClassificationScore]:
        """Get top k classifications by confidence."""
        sorted_scores = sorted(self.scores, key=lambda x: x.confidence, reverse=True)
        return sorted_scores[:k]

    def is_type(self, doc_type: DocumentType, min_confidence: float = 0.5) -> bool:
        """Check if document is classified as a specific type with minimum confidence."""
        for score in self.scores:
            if score.document_type == doc_type and score.confidence >= min_confidence:
                return True
        return False


class DocumentCategoryRule(BaseModel):
    """
    Rule for rule-based document classification.
    """
    name: str = Field(..., description="Rule name")
    document_type: DocumentType = Field(..., description="Target document type")

    # Matching criteria
    title_keywords: List[str] = Field(
        default_factory=list,
        description="Keywords to match in title"
    )
    content_keywords: List[str] = Field(
        default_factory=list,
        description="Keywords to match in content"
    )
    required_sections: List[str] = Field(
        default_factory=list,
        description="Required section headings"
    )
    file_patterns: List[str] = Field(
        default_factory=list,
        description="Filename patterns (regex)"
    )

    # Confidence adjustment
    base_confidence: float = Field(
        default=0.8,
        ge=0.0,
        le=1.0,
        description="Base confidence when rule matches"
    )
    keyword_boost: float = Field(
        default=0.05,
        ge=0.0,
        le=0.2,
        description="Confidence boost per matched keyword"
    )

    # Priority
    priority: int = Field(
        default=0,
        description="Rule priority (higher = checked first)"
    )


class ClassificationConfig(BaseModel):
    """
    Configuration for document classification.
    """
    # Confidence thresholds
    min_confidence: float = Field(
        default=0.6,
        ge=0.0,
        le=1.0,
        description="Minimum confidence for classification"
    )
    human_review_threshold: float = Field(
        default=0.7,
        ge=0.0,
        le=1.0,
        description="Below this, flag for human review"
    )

    # Classification method
    use_llm: bool = Field(default=True, description="Use LLM for classification")
    use_rules: bool = Field(default=True, description="Use rule-based classification")
    hybrid_mode: str = Field(
        default="llm_primary",
        description="Hybrid mode: llm_primary, rules_primary, or ensemble"
    )

    # Custom rules
    custom_rules: List[DocumentCategoryRule] = Field(
        default_factory=list,
        description="Custom classification rules"
    )

    # Document types to consider
    enabled_types: List[DocumentType] = Field(
        default_factory=lambda: list(DocumentType),
        description="Document types to consider"
    )

    # Evidence requirements
    require_evidence: bool = Field(
        default=True,
        description="Require evidence for classification"
    )
    max_evidence_snippets: int = Field(
        default=3,
        description="Maximum evidence snippets to include"
    )