dp-zp-agent/tools/answer_evaluation/checked_generation.py

47 lines
2.4 KiB
Python

"""One bounded corrective request based solely on retrieval evidence."""
import json
from .answer_format import parse_answer, Answer
from .context_policy import answer_context_issues
def generate_checked_answer(question: str, context: dict, prompt: str, complete) -> dict:
messages=[{'role':'system','content':prompt},{'role':'user','content':json.dumps(
{'question':question,'graph_context':context},ensure_ascii=False)}]
result={'attempts':[],'accepted_for_delivery':False}
for number in range(2):
record={};text=''
try:
response=complete(messages)
result['response']=response;record['response']=response
text=response['choices'][0]['message']['content'].strip()
except Exception as error:
result['error']=f'{type(error).__name__}: {str(error)[:500]}'
result['attempts'].append(record)
return result
result['attempts'].append(record)
try:
answer,normalized=parse_answer(text)
record['answer']=answer;record['format_normalized']=normalized
except (ValueError,TypeError) as error:
record['format_error']=type(error).__name__
if number==1:
result['error']='InvalidAnswerFormat: two invalid JSON/schema responses'
return result
feedback='Vráť jediný platný JSON objekt podľa schémy, bez zmeny faktov z pôvodného kontextu. Schéma: '+json.dumps(Answer.model_json_schema(),ensure_ascii=False)
else:
issues=answer_context_issues(answer,context)
record['context_issues']=issues
result['answer']=answer
if not issues:
result['accepted_for_delivery']=True
return result
if number==1:
result['context_rejected']=True
return result
feedback=('Odpoveď porušila pravidlá dodaného kontextu: '+', '.join(issues)+'. '
'Oprav jediný JSON objekt výhradne z pôvodných podkladov. Použi iba source_urls zo sources. '
'Typ práce nie je keyword. answer_candidates dokladajú spojenie požadovaného názvu alebo roku a tagu cez tú istú osobu. '
'Pri neúplnom výstupe uveď: Výpis je neúplný.')
messages += [{'role':'assistant','content':text},{'role':'user','content':feedback}]
return result