dp-zp-agent/tools/answer_evaluation/render_answer.py
2026-11-02 16:44:29 +01:00

38 lines
2.0 KiB
Python

"""Render only validated structured facts; never infer facts or read gold."""
from copy import deepcopy
from tools.graphrag.graphrag import WORK_TYPE_LABELS
def render_graph_answer(answer: dict, context: dict) -> dict:
out = deepcopy(answer)
lines = []
if out['status'] == 'ok':
labels = {'work_label': 'záznam', 'work_type': 'typ práce',
'title': 'názov práce', 'year': 'rok práce', 'start_year': 'začiatok štúdia',
'keyword': 'kľúčové slovo', 'category': 'kategória', 'document_author': 'autor dokumentu'}
for claim in out['claims']:
values = [f'{label}: {WORK_TYPE_LABELS.get(claim[key], claim[key]) if key == "work_type" else claim[key]}'
for key, label in labels.items() if claim.get(key) is not None]
if claim.get('record_id') and not claim.get('title'):
values.append('názov v zdroji nie je uvedený')
if claim.get('record_id') and claim.get('year') is None:
values.append('rok v zdroji nie je uvedený')
lines.append(claim['person'] + (' — ' + '; '.join(values) if values else '') + '.')
if out.get('count') is not None:
lines.append(f'Počet vyhovujúcich osôb: {out["count"]}.')
if context.get('complete') is False or context.get('truncated'):
lines.append('Výpis je neúplný.')
elif context.get('complete') is None:
lines.append('Úplnosť vyhľadania nemožno zaručiť.')
elif out['status'] == 'ambiguous':
lines.append('Požiadavka je nejednoznačná; údaje nemožno spoľahlivo určiť.')
elif out['status'] == 'error':
lines.append('Vyhľadanie zlyhalo; odpoveď nemožno pripraviť.')
else:
lines.append('Požadované údaje sa v dostupných podkladoch nenašli.')
if out['source_urls']:
lines.append('Zdroje: ' + ', '.join(out['source_urls']))
out['answer'] = '\n'.join(lines)
return out