Files
CADmind/backend/engine/text_binding_audit.py
2026-09-24 10:40:57 +08:00

46 lines
3.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Source-only repair of parser-added feature/datum nouns, never numeric targets."""
import re
from pydantic import Field
from .quality_models import Record
class TextBinding(Record):
requirement_id:str
incorrect_fragment:str=Field(min_length=1,max_length=80)
replacement:str=Field(min_length=1,max_length=120)
source_quote:str=Field(min_length=1)
explanation:str=Field(min_length=1)
class TextBindingAudit(Record):
corrections:list[TextBinding]=Field(default_factory=list,max_length=12)
PROMPT='''独立检查需求解析器是否加错了特征归属或基准名词。只看原始文字与需求,没有候选几何,不得按模型改验收。
仅检查给出的相关需求ID。若发现原文未规定、解析器自行添加的错误归属名词,可替换最小名词片段,例如把局部边缘错误指定为整件包络基准、把一个孔组的沉孔绑到另一无关孔。
不能改用户原话,不能修改任何数值、单位、公差、轴向坐标。incorrect_fragment和replacement均不得含数字;原始要求有真正歧义时返回空corrections,保留用户选择。
source_quote逐字复制原始文字连续片段,explanation说明原文为何支持替换;无充分证据不修改。所有原始功能、尺寸、结构、关键性保留。
禁止为候选成功放宽要求、删除结构或转为以后确认。仅输出结构。'''
def audit_text_bindings(client,job,brief,text,ids,call):
reqs={r.id:r for r in brief.requirements if r.id in ids}
def validate(result):
errors=[];seen=set()
for c in result.corrections:
req=reqs.get(c.requirement_id)
if req is None or c.requirement_id in seen:
errors.append('每个相关需求最多一项纠正,不能改其他ID');continue
seen.add(c.requirement_id)
if c.incorrect_fragment not in req.description or c.incorrect_fragment in text:
errors.append('只能改需求草稿自行加入的错误片段,不能改原文')
if re.search(r'\d',c.incorrect_fragment+c.replacement):errors.append('此工具不能修改任何数值或数字基准')
if c.source_quote not in text:errors.append('必须逐字引用原始文字的连续片段')
return errors
content=[{'type':'input_text','text':'原始请求:\n'+text+'\n相关草稿:\n'+'\n'.join(r.model_dump_json() for r in reqs.values())}]
result=call(client,job,PROMPT,content,TextBindingAudit,'read',validate=validate)
job.write('text_binding_audit.json',result.model_dump())
updated=brief.model_copy(deep=True)
if result.corrections:job.write('requirements_before_text_binding_audit.json',brief.model_dump())
for correction in result.corrections:
req=next(r for r in updated.requirements if r.id==correction.requirement_id)
req.description=req.description.replace(correction.incorrect_fragment,correction.replacement)
req.evidence+=';来源复核:'+correction.source_quote+';'+correction.explanation
return updated