klg-asutk-app/backend/app/services/legal_agents/classifier.py
Yuriy 0150aba4f5 Consolidation: KLG ASUTK + PAPA integration
- Unify API: lib/api.ts uses /api/v1, inbox uses /api/inbox (rewrites)
- Remove localhost refs: openapi, inbox page
- Add rewrites: /api/inbox|tmc -> inbox-server, /api/v1 -> FastAPI
- Add stub routes: knowledge/insights, recommendations, search, log-error
- Transfer from PAPA: prompts (inspection, tmc), scripts, supabase, data/tmc-requests
- Fix inbox-server: ORDER BY created_at, package.json
- Remove redundant app/api/inbox/files route (rewrites handle it)
- knowledge/ in gitignore (large PDFs)

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 17:18:31 +03:00

55 lines
2.8 KiB
Python

"""
Агент классификации типа документа (законодательный, рекомендательный, директивный, уведомительный и т.д.).
"""
import json
from .base import BaseLegalAgent, AgentResult
class DocumentClassifierAgent(BaseLegalAgent):
def __init__(self, llm_client=None):
super().__init__("DocumentClassifierAgent", llm_client)
def run(self, context: dict) -> AgentResult:
title = context.get("title", "")
content = (context.get("content") or "")[:8000]
jurisdiction = context.get("jurisdiction_code", "")
system = (
"Ты — эксперт по классификации юридических документов. "
"Определи тип: legislative, recommendatory, directive, notification, regulatory, contractual, judicial, other. "
"Верни JSON: {\"document_type\": \"...\", \"confidence\": 0.0-1.0, \"reasoning\": \"...\"}"
)
user = f"Юрисдикция: {jurisdiction}\nЗаголовок: {title}\n\nТекст (фрагмент):\n{content[:4000]}"
out = self._call_llm(system, user, json_mode=True)
if out:
try:
data = json.loads(out)
dt = data.get("document_type", "other")
if dt not in ("legislative", "recommendatory", "directive", "notification", "regulatory", "contractual", "judicial", "other"):
dt = "other"
return AgentResult(True, {"document_type": dt, "confidence": float(data.get("confidence", 0.5)), "reasoning": data.get("reasoning", "")}, agent_name=self.name)
except (json.JSONDecodeError, TypeError):
pass
# Заглушка: эвристика по заголовку
t = (title + " " + content[:500]).lower()
if "закон" in t or "кодекс" in t or " federal act" in t or "law" in t:
dt = "legislative"
elif "рекоменда" in t or "recommendation" in t or "руководство" in t:
dt = "recommendatory"
elif "директив" in t or "directive" in t or "приказ" in t:
dt = "directive"
elif "уведом" in t or "notification" in t or "информ" in t:
dt = "notification"
elif "постановление" in t or "распоряжение" in t or "regulation" in t:
dt = "regulatory"
elif "договор" in t or "contract" in t:
dt = "contractual"
elif "решение" in t and ("суд" in t or "court" in t):
dt = "judicial"
else:
dt = "other"
return AgentResult(True, {"document_type": dt, "confidence": 0.5, "reasoning": "Эвристика по ключевым словам (LLM недоступен)"}, agent_name=self.name)