任务契约、示例与失败状态:练习答案
参考实现
追加一个包装函数,不改变原类型与字段校验:
python
def validate_source(raw, source):
value = validate(raw)
if value['label'] != 'unknown' and value['evidence'] not in source:
raise ValueError('fabricated quote')
return value
assert validate_source('{"label":"billing","evidence":"退款"}', '我的退款失败')['label']=='billing'
try:
validate_source('{"label":"billing","evidence":"欠费"}', '我的退款失败')
except ValueError:
pass
else:
raise AssertionError('fabricated quote accepted')子串检查不等于分类正确,例如“不是退款问题”也包含退款;保留否定场景的语义测试。
返回题目。
链路变体
在examples末尾追加一对user/assistant消息,并将tests追加 ('Do not send another parcel; pay me back','refund');记录数变为6,修改最后断言为 assert len(records)==6。不能把新保留输入也放进examples。
摘要的业务反例可用确定性规则作为gold:
python
def policy(days,opened,shipping_fee=False):
return 0<=days<=7 and not opened and not shipping_fee
assert policy(3,True) is False
assert policy(8,False) is False
assert policy(3,False,True) is False
assert policy(3,False) is True用原文与压缩文分别回答这四题,再比较模型答案与gold;这个函数不是假装模型已经答对。若压缩版丢失条件,应保留原证据或重新压缩,而非修改gold迁就摘要。