feat: LLM-merged group meta-summary (T70)
This commit is contained in:
@@ -334,26 +334,92 @@ async def apply_scene_close_summary(
|
||||
timeout_s=timeout_s,
|
||||
)
|
||||
|
||||
# Group node update: naive per-POV concat for v2. Only fires when
|
||||
# both POVs ran (i.e. the guest is present) and a group_node row
|
||||
# exists for this chat.
|
||||
# Group node update: T70 runs a third classifier call to merge the
|
||||
# two per-POV summaries into a coherent group-level view + a brief
|
||||
# group-dynamic note. Falls back to the Phase 2 naive concat on
|
||||
# classifier failure (see :func:`merge_group_summary`). Only fires
|
||||
# when both POVs ran (i.e. the guest is present) and a group_node
|
||||
# row exists for this chat.
|
||||
if guest_pov is not None and get_group_node(conn, chat_id) is not None:
|
||||
host_bot = get_bot(conn, host_bot_id) or {"name": host_bot_id}
|
||||
guest_bot = get_bot(conn, guest_bot_id) or {"name": guest_bot_id}
|
||||
host_name = host_bot.get("name", host_bot_id) or host_bot_id
|
||||
guest_name = guest_bot.get("name", guest_bot_id) or guest_bot_id
|
||||
group_summary = (
|
||||
f"{host_name}: {host_pov.summary}\n\n"
|
||||
f"{guest_name}: {guest_pov.summary}"
|
||||
merged = await merge_group_summary(
|
||||
client,
|
||||
classifier_model=classifier_model,
|
||||
host_name=host_name,
|
||||
host_pov_summary=host_pov.summary,
|
||||
guest_name=guest_name,
|
||||
guest_pov_summary=guest_pov.summary,
|
||||
timeout_s=timeout_s,
|
||||
)
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="group_node_updated",
|
||||
payload={
|
||||
"chat_id": chat_id,
|
||||
"summary": group_summary,
|
||||
"dynamic": "",
|
||||
"summary": merged.summary,
|
||||
"dynamic": merged.dynamic,
|
||||
},
|
||||
)
|
||||
|
||||
return host_pov
|
||||
|
||||
|
||||
class GroupMetaSummary(BaseModel):
|
||||
"""Classifier output: a merged group-level view of a closed scene.
|
||||
|
||||
Defaults are an empty no-op so callers can use the schema's default
|
||||
as a sentinel; in practice :func:`merge_group_summary` builds an
|
||||
explicit naive-concat fallback rather than returning these defaults
|
||||
directly so existing Phase 2 behavior is preserved on classifier
|
||||
failure.
|
||||
"""
|
||||
|
||||
summary: str = ""
|
||||
dynamic: str = ""
|
||||
|
||||
|
||||
_GROUP_MERGE_SYSTEM = (
|
||||
"Given two per-POV scene summaries from a 3-entity scene (you + "
|
||||
"host + guest), produce a coherent group-level summary capturing "
|
||||
"the shared events as both witnesses experienced them, plus a "
|
||||
"brief 'dynamic' note describing the trio's group dynamic during "
|
||||
"the scene. Output strict JSON matching schema."
|
||||
)
|
||||
|
||||
|
||||
async def merge_group_summary(
|
||||
client: LLMClient,
|
||||
*,
|
||||
classifier_model: str,
|
||||
host_name: str,
|
||||
host_pov_summary: str,
|
||||
guest_name: str,
|
||||
guest_pov_summary: str,
|
||||
timeout_s: float = 30.0,
|
||||
) -> GroupMetaSummary:
|
||||
"""Merge two per-POV scene summaries into a coherent group-level
|
||||
summary + group-dynamic note. Falls back to the naive concat (the
|
||||
existing behavior) on classifier failure."""
|
||||
user = (
|
||||
f"{host_name} (host) POV summary:\n{host_pov_summary}\n\n"
|
||||
f"{guest_name} (guest) POV summary:\n{guest_pov_summary}"
|
||||
)
|
||||
fallback = GroupMetaSummary(
|
||||
summary=(
|
||||
f"{host_name}: {host_pov_summary}\n\n"
|
||||
f"{guest_name}: {guest_pov_summary}"
|
||||
),
|
||||
dynamic="",
|
||||
)
|
||||
return await classify(
|
||||
client,
|
||||
model=classifier_model,
|
||||
system=_GROUP_MERGE_SYSTEM,
|
||||
user=user,
|
||||
schema=GroupMetaSummary,
|
||||
default=fallback,
|
||||
timeout_s=timeout_s,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user