v1.0.162: auto-classify backend — classify/grouping services, 7 new columns, /classify-batch, /api/groups, /apply-groups

This commit is contained in:
2026-06-24 08:22:21 +04:00
parent 740f75a791
commit 7a96f5b95b
8 changed files with 372 additions and 6 deletions
+48 -4
View File
@@ -2,12 +2,12 @@
from .connection import query, execute, execute_returning
def insert(filename, mime_type, original_bytes, status="uploaded"):
def insert(filename, mime_type, original_bytes, status="uploaded", batch_id=None):
"""Insert document, return row dict."""
return execute_returning(
"""INSERT INTO documents (filename, mime_type, original_bytes, status)
VALUES (%s, %s, %s, %s) RETURNING *""",
(filename, mime_type, original_bytes, status),
"""INSERT INTO documents (filename, mime_type, original_bytes, status, batch_id)
VALUES (%s, %s, %s, %s, %s) RETURNING *""",
(filename, mime_type, original_bytes, status, batch_id),
)
@@ -34,3 +34,47 @@ def set_error(doc_id, error_message):
def delete(doc_id):
return execute("DELETE FROM documents WHERE id = %s", (doc_id,))
def set_classification(doc_id, doc_type, own_number, parent_number, doc_date, counterparty):
"""Store LLM classification results."""
return execute(
"""UPDATE documents SET doc_type=%s, own_number=%s, parent_number=%s,
doc_date=%s, counterparty=%s, classify_status='classified'
WHERE id=%s""",
(doc_type, own_number, parent_number, doc_date, counterparty, doc_id),
)
def set_classify_failed(doc_id, error):
return execute(
"UPDATE documents SET classify_status='failed', error_message=%s WHERE id=%s",
(error, doc_id),
)
def list_pending(batch_id):
"""Documents waiting for classification."""
return query(
"SELECT id, filename, elements_json FROM documents WHERE batch_id=%s AND classify_status='pending' AND status='parsed'",
(batch_id,),
)
def list_by_batch(batch_id):
"""All documents in a batch with classification fields."""
return query(
"""SELECT id, filename, status, doc_type, own_number, parent_number,
doc_date, counterparty, classify_status, error_message
FROM documents WHERE batch_id=%s ORDER BY created_at""",
(batch_id,),
)
def count_by_status(batch_id):
"""Count documents by classify_status."""
rows = query(
"SELECT classify_status, count(*) as cnt FROM documents WHERE batch_id=%s GROUP BY classify_status",
(batch_id,),
)
return {r["classify_status"]: r["cnt"] for r in rows}
+15
View File
@@ -35,3 +35,18 @@ def seed_defaults():
true, 'Авто-создан из llm_prompt.py _build_diff'
)
""")
_ensure_classify_prompt()
def _ensure_classify_prompt():
"""Ensure classify prompt exists (idempotent)."""
existing = query("SELECT id FROM prompts WHERE role = 'classify' AND is_active = true LIMIT 1")
if existing:
return
execute("""
INSERT INTO prompts (role, name, body, is_active, notes) VALUES (
'classify', 'default-v1',
'Ты — классификатор договорных документов. Ниже фрагмент текста документа.\n\nОпредели:\n1. doc_type: "contract" (договор), "supplement" (допсоглашение/допник), "specification" (спецификация/приложение), "other"\n2. own_number: номер ЭТОГО документа (например "МЭС-123-2024" или "ДС №2")\n3. parent_number: номер родительского договора (для допников/спецификаций — "к Договору №..."). Для самого договора — null.\n4. doc_date: дата документа в формате YYYY-MM-DD\n5. counterparty: название контрагента (из поля "Заказчик", "Арендатор" или аналогичного)\n6. confidence: "ok" если уверен, "low" если сомневаешься\n\nВерни СТРОГО JSON без пояснений:\n{\n "doc_type": "...",\n "own_number": "..." или null,\n "parent_number": "..." или null,\n "doc_date": "YYYY-MM-DD" или null,\n "counterparty": "..." или null,\n "confidence": "ok" или "low"\n}\n\nДОКУМЕНТ:\n---\n{header_text}\n---',
true, 'Авто-создан: classify prompt'
)
""")