update
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import json
|
||||
from bisect import bisect_left
|
||||
|
||||
|
||||
@@ -38,13 +39,51 @@ def excerpt(value: str, question: str, limit: int = 1600) -> str:
|
||||
|
||||
|
||||
def dialogue_turns(context, budget: int = 6000) -> list[dict]:
|
||||
"""Replies are writing/conversation context only, never a source of business facts."""
|
||||
"""Replies resolve user references or edits; business facts must be re-read."""
|
||||
result = []
|
||||
for turn in reversed(context.turns[-4:]):
|
||||
item = turn.model_dump(exclude={"assistant_reply"})
|
||||
if turn.assistant_act == "general" and turn.assistant_reply and budget > 0:
|
||||
reply = excerpt(turn.assistant_reply, "", min(4000, budget))
|
||||
size = len(json.dumps(item, ensure_ascii=False))
|
||||
if size > budget:
|
||||
break
|
||||
budget -= size
|
||||
if turn.assistant_reply and budget > 100:
|
||||
reply = excerpt(turn.assistant_reply, "", min(4000, max(1, budget - 100)))
|
||||
item["assistant_reply"] = reply
|
||||
budget -= len(reply)
|
||||
if turn.assistant_act != "general":
|
||||
item["reply_usage"] = "仅用于解析上一条回复中的指代或改写;查询事实必须重新读取授权记录"
|
||||
budget -= len(reply) + 160
|
||||
result.append(item)
|
||||
return list(reversed(result))
|
||||
|
||||
|
||||
def planning_objects(objects: list[dict], question: str, budget: int = 6000) -> list[dict]:
|
||||
"""Preserve record order/IDs for ordinal references; bound reloaded body text.
|
||||
|
||||
Only the model view is shortened. Authorization and reference checks continue
|
||||
to use the original complete objects, not the excerpts.
|
||||
"""
|
||||
result = []
|
||||
remaining = budget
|
||||
for index, obj in enumerate(objects[:20]):
|
||||
item = {k: obj[k] for k in ("source_id", "resource", "id", "title") if k in obj}
|
||||
if "title" in item:
|
||||
item["title"] = str(item["title"])[:200]
|
||||
per_object = max(0, remaining // max(1, min(20, len(objects)) - index))
|
||||
fields, used, clipped = {}, 0, False
|
||||
for name, value in obj.get("fields", {}).items():
|
||||
text = str(value) if value is not None else ""
|
||||
available = max(0, per_object - used - len(name) - 10)
|
||||
if len(text) > available:
|
||||
clipped = True
|
||||
if available < 40:
|
||||
continue
|
||||
value = excerpt(text, question, min(1000, max(1, available - 40)))
|
||||
fields[name] = value
|
||||
used += len(json.dumps({name: value}, ensure_ascii=False))
|
||||
item["fields"] = fields
|
||||
if clipped:
|
||||
item["content_truncated"] = True
|
||||
remaining -= used
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user