46 lines
2.2 KiB
Python
46 lines
2.2 KiB
Python
"""Production graph-to-answer context adapter; no benchmark or oracle dependencies."""
|
|
from .context_policy import joined_candidates
|
|
|
|
|
|
def graph_context(result: dict) -> dict:
|
|
"""Remove storage/profile metadata that cannot answer the actual question.
|
|
|
|
Uses only actual retrieval output and query intent, never gold annotations.
|
|
Values/subjects/source paths are copied without inventing or rewriting facts.
|
|
"""
|
|
facts = []; intent = result['constraints']
|
|
for row in result['facts']:
|
|
kind = row.get('fact_type', '')
|
|
if kind.startswith('shared_'):
|
|
field = {'shared_keyword': 'keyword', 'shared_category': 'category',
|
|
'shared_document_author': 'document_author'}.get(kind)
|
|
if field:
|
|
for side in ['left', 'right']:
|
|
facts.append({'person': row[f'{side}_person'], field: row[field],
|
|
'document_path': row.get(f'{side}_document_path'),
|
|
'source_url': row.get(f'{side}_source_url')})
|
|
continue
|
|
if not row.get('person'):
|
|
continue
|
|
allowed = {'person', 'document_path', 'source_url', 'keyword', 'category', 'document_author'}
|
|
if intent['mentions_title']:
|
|
allowed.add('title')
|
|
if intent['mentions_title'] or intent['mentions_academic_year'] or intent['years']:
|
|
allowed.add('year')
|
|
if intent['mentions_start_year']:
|
|
allowed.add('start_year')
|
|
fact = {k: v for k, v in row.items() if k in allowed and v is not None}
|
|
if fact not in facts:
|
|
facts.append(fact)
|
|
sources=result.get('sources')
|
|
if sources is None:
|
|
sources=[{k:d[k] for k in ['document_path','source_url'] if d.get(k)} for d in result.get('documents',[])]
|
|
context={'status':result['status'],'reason':result['reason'],'facts':facts,'sources':sources,
|
|
'aggregation':result['aggregation'],'complete':result.get('complete'),
|
|
'truncated':result.get('truncated',False),
|
|
'truncation_reasons':result.get('truncation_reasons',[]),
|
|
'index_version':result.get('index_version')}
|
|
context['answer_candidates']=joined_candidates(facts,intent)
|
|
return context
|
|
|