"""The model's reading of what a link node leaves to reviewers, shown beside each case: whether a person and a possible owner are one patient, and which record of a torn person belongs to someone else, each with its reason. A suggestion decides nothing: reviewers still answer every case (persistance.nodes).""" import json from core.drafting import SAME_PERSON, TORN, same_person_schema, torn_schema from core.pipeline import HELD, RECORD from persistance import llm, nodes PAIRS, TORN_PERSONS = "__suggestions", "__torn_suggestions" def _side(row: dict, prefix: str) -> dict: return {k.removeprefix(prefix): v for k, v in row.items() if k.startswith(prefix) and v is not None} # fmt: skip def _ask(system: str, text: str, schema: dict, unsure: dict) -> dict: """The model's suggestion; an answer it couldn't give says so to the reviewer.""" try: return llm.ask(system, text, schema) except llm.Unreadable: return {**unsure, "reason": "El modelo no dio una respuesta legible para este caso."} def persons(pipeline_id: str, link: str) -> None: """Suggestions for the link node's queue (pairs) and its torn persons.""" pairs = [] for row in nodes.rows(pipeline_id, f"{link}__review"): case = {"a": _side(row, "person_"), "b": _side(row, "candidate_"), "why": row["reason"]} text = json.dumps(case, default=str, ensure_ascii=False) answer = _ask(SAME_PERSON, text, same_person_schema(), {"same": None}) pairs.append({"person": row["person"], "candidate": row["candidate"], "same": answer["same"], "reason": answer["reason"]}) # fmt: skip nodes.write(pipeline_id, f"{link}{PAIRS}", pairs) torn: dict[str, list[dict]] = {} for row in nodes.rows(pipeline_id, link): if "disagree" in (row.get(HELD) or ""): record = {k: v for k, v in row.items() if not k.startswith("_") and v is not None} torn.setdefault(row["person_id"], []).append({**record, "id": row[RECORD]}) found = [] for person, records in torn.items(): text = json.dumps(records, default=str, ensure_ascii=False) answer = _ask(TORN, text, torn_schema([r["id"] for r in records]), {"record": None}) found.append({"person": person, "record": answer["record"], "reason": answer["reason"]}) nodes.write(pipeline_id, f"{link}{TORN_PERSONS}", found)