fix: reconcile ai-11-2 with local knowledge and permission changes
This commit is contained in:
@@ -17,7 +17,37 @@ class ModelOutputError(ValueError):
|
||||
self.output_chars = output_chars
|
||||
|
||||
|
||||
def normalize_output(candidate, text, input_chars):
|
||||
class ModelDiscard(Exception):
|
||||
"""模型判定这段对话没有可复用的知识(寒暄、个人订单查询、只对某位患者成立的处置……)。
|
||||
|
||||
不是错误:这条不生成草稿,只计数,审核的人就不用再逐条驳回这些。
|
||||
"""
|
||||
|
||||
def __init__(self, reason, input_chars=0, output_chars=0):
|
||||
super().__init__(reason)
|
||||
self.reason = reason
|
||||
self.input_chars = input_chars
|
||||
self.output_chars = output_chars
|
||||
|
||||
|
||||
NO = {'false', 'no', '否', '0'}
|
||||
|
||||
|
||||
def _json_object(raw):
|
||||
"""模型偶尔在 JSON 前后加一两句说明,或包在代码块里:取出最外层的对象。"""
|
||||
fence = re.fullmatch(r'```(?:json)?\s*([\s\S]*?)\s*```', raw, flags=re.IGNORECASE)
|
||||
if fence:
|
||||
raw = fence.group(1).strip()
|
||||
try:
|
||||
return json.loads(raw)
|
||||
except (ValueError, RecursionError):
|
||||
start, end = raw.find('{'), raw.rfind('}')
|
||||
if start == -1 or end <= start:
|
||||
raise
|
||||
return json.loads(raw[start:end + 1])
|
||||
|
||||
|
||||
def normalize_output(candidate, text, input_chars, *, allow_discard=False):
|
||||
output_chars = len(text) if isinstance(text, str) else 0
|
||||
|
||||
def invalid(reason):
|
||||
@@ -26,15 +56,17 @@ def normalize_output(candidate, text, input_chars):
|
||||
if not isinstance(text, str) or not text.strip():
|
||||
invalid('模型未返回知识正文')
|
||||
raw = text.strip().lstrip('\ufeff')
|
||||
fence = re.fullmatch(r'```(?:json)?\s*([\s\S]*?)\s*```', raw, flags=re.IGNORECASE)
|
||||
if fence:
|
||||
raw = fence.group(1).strip()
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
data = _json_object(raw)
|
||||
except (ValueError, RecursionError):
|
||||
invalid('模型输出不是完整 JSON')
|
||||
if not isinstance(data, dict):
|
||||
invalid('模型输出不是 JSON 对象')
|
||||
keep = data.get('keep', True)
|
||||
if allow_discard and isinstance(keep, (bool, int, str)) and str(keep).strip().lower() in NO:
|
||||
reason = data.get('reason')
|
||||
reason = redact(reason)[:120] if isinstance(reason, str) and reason.strip() else '模型判定没有可复用的知识'
|
||||
raise ModelDiscard(reason, input_chars, output_chars)
|
||||
|
||||
# Never stringify objects, invent an answer, or truncate clinical/business facts.
|
||||
result = copy.deepcopy(candidate)
|
||||
@@ -83,6 +115,32 @@ def normalize_output(candidate, text, input_chars):
|
||||
return result, input_chars, output_chars
|
||||
|
||||
|
||||
def split_batch(text, count):
|
||||
"""一次请求整理多段问答的输出 → {编号: 该项的 JSON 文本}。
|
||||
|
||||
整段解析不了(被截断、混了说明文字)就抛 ModelOutputError,调用方把这批拆成单条重试;
|
||||
个别项缺失或编号对不上,只是这几项拿不到结果,同样拆成单条重试,其余照常保存。
|
||||
"""
|
||||
if not isinstance(text, str) or not text.strip():
|
||||
raise ModelOutputError('模型未返回批量结果')
|
||||
try:
|
||||
data = _json_object(text.strip().lstrip('\ufeff'))
|
||||
except (ValueError, RecursionError):
|
||||
try:
|
||||
data = json.loads(text[text.find('['):text.rfind(']') + 1])
|
||||
except (ValueError, RecursionError):
|
||||
raise ModelOutputError('模型批量输出不是完整 JSON') from None
|
||||
items = data.get('items') if isinstance(data, dict) else data
|
||||
if not isinstance(items, list):
|
||||
raise ModelOutputError('模型批量输出缺少 items 列表')
|
||||
wanted = {str(index + 1) for index in range(count)}
|
||||
result = {}
|
||||
for item in items:
|
||||
if isinstance(item, dict) and str(item.get('id', '')).strip() in wanted:
|
||||
result.setdefault(str(item['id']).strip(), json.dumps(item, ensure_ascii=False))
|
||||
return result
|
||||
|
||||
|
||||
def original_draft(candidate, error):
|
||||
"""Keep complete redacted evidence, requiring a human edit before approval."""
|
||||
result = copy.deepcopy(candidate)
|
||||
|
||||
Reference in New Issue
Block a user