@unify.traced
def call_agent(
system_msg,
question,
sub_questions,
markscheme,
answer,
available_marks_total,
):
local_agent = agent.copy()
with_subqs = len(markscheme) > 1
response_format = create_response_format(
list(markscheme.keys()) if with_subqs else None,
)
local_agent.set_response_format(response_format)
if with_subqs:
output_response_exp = output_response_explanations["with_subqs"]
output_response_exp = output_response_exp.replace(
"{subquestions}",
", ".join(list(markscheme.keys())),
)
else:
output_response_exp = output_response_explanations["without_subqs"]
local_agent.set_system_message(
system_msg.replace(
"{question}",
textwrap.indent(question, " " * 4),
)
.replace(
"{markscheme}",
pretty_print_dict(markscheme, indent=4),
)
.replace(
"{answer}",
pretty_print_dict(answer, indent=4),
)
.replace(
"{available_marks_total}",
str(available_marks_total),
)
.replace(
"{questions_markscheme_and_answers}",
pretty_print_dict(
{
k: {
"sub-question": sub_questions[k],
"markscheme": markscheme[k],
"answer": answer[k],
}
for k in sub_questions.keys()
},
indent=4,
),
)
.replace(
"{output_response_explanation}",
output_response_exp,
),
)
ret = local_agent.generate()
if "```" in ret:
ret = ret.split("```")[-2].lstrip("json")
ret = response_format.model_validate_json(ret).model_dump()
if not with_subqs:
return {"_": ret}
return ret