fix(agent): reuse collected vehicle counts and avoid repeated questions

This commit is contained in:
Eric Wang
2026-10-28 11:30:56 +08:00
parent a277d4c7e6
commit 20c5e39e9d
15 changed files with 7073 additions and 47 deletions
+89 -9
View File
@@ -8,7 +8,7 @@ import re
from pathlib import Path
from zoneinfo import ZoneInfo
from src.agent.collect import parse_state_reply, run_collect
from src.agent.collect import _build_user_message, parse_state_reply, run_collect
from src.agent.llm import build_llm_service
from src.agent.prompts import COLLECTION, EXTRACTION, render_prompt
@@ -20,7 +20,73 @@ BASE = '十分钟前两辆汽车追尾,没有人受伤,没有非机动车或
def probes():
injury_q = [{'role': 'assistant', 'content': '<state>1002</state>请问这次事故中,有没有人员受伤呢?'}]
complaint = '我这个问题上次已经回答过了,你能看到吗?'
count_question = {'role': 'assistant', 'content': '<state>1002</state>请问有几辆汽车卷入了这次事故呢?请您告诉我一个具体的数字。'}
fixed_object = '我的小轿车撞到了路边围栏,旁边还有汽车,我不清楚是否也碰到了。没有人受伤,没有非机动车或行人,我还在现场。'
confirmed = [{'role': 'assistant', 'content': CONFIRM}, {'role': 'user', 'content': '可以。'}]
known_form = dict(accident_cause='其他:车辆碰撞', has_injury=False,
has_non_motor_vehicle_hit=False, accident_time=TIME, is_on_scene=True)
return [
dict(name='feedback_single_motor_fixed_object', user='我现在还在事故现场。', history=[
{'role':'user','content':'我的车子是自己的小轿车,撞到了路边围栏。'},
{'role':'user','content':'没有人员伤伤就车子撞了。'},
{'role':'user','content':'没有涉及非机动车,也没有行人。'}, *confirmed,
{'role':'assistant','content':'<state>1002</state>请问您现在还在事故现场吗?'}],
code='2000', count=1, not_matches='(几辆|多少辆|其他.*车.*吗|这个时间对吗)'),
dict(name='feedback_single_motor_complaint', user=complaint, history=[
{'role':'user','content':'我的小轿车撞到路边围栏,没有人受伤,没有非机动车或行人,我还在现场。'},
*confirmed, count_question], code='2000', count=1,
not_matches='(几辆|多少辆|没.*看到|其他.*车.*吗)'),
dict(name='holdout_single_motor_different_object', user='我的货车倒车碰到停车场立柱,车受损了。',
history=[], code='1002', count=1, contains='受伤'),
dict(name='holdout_only_vehicle_type_not_count', user='我开的是自己的小轿车。',
history=[], code='1002', count_absent=True),
dict(name='feedback_unknown_count_acknowledge_history', user=complaint,
history=[{'role': 'user', 'content': fixed_object}, *confirmed, count_question],
code='1002', count_absent=True,
matches='(其他|还有|几辆|多少辆)',
not_matches='(没.*(看到|收到)|未.*(提供|回答)|具体的数字)'),
dict(name='feedback_known_count_in_history', user=complaint,
history=[{'role': 'user', 'content': '只有我这一辆小轿车撞到围栏。没有人受伤,没有非机动车或行人,我还在现场。'}, *confirmed, count_question],
code='2000', count=1, not_matches='(几辆|多少辆|具体的数字|没.*看到)'),
dict(name='feedback_reverse_collision_extracts_one', user='倒车时碰到了停车场墙面,车刮坏了。',
history=[], code='1002', count=1, contains='受伤'),
dict(name='feedback_form_two_skips_count', user=complaint,
history=[*confirmed, count_question], form={**known_form, 'vehicle_count': 2},
code='2010', count=2, not_matches='(几辆|多少辆|是否.*两辆|确认.*数量|没.*看到)'),
dict(name='feedback_form_three_skips_count', user=complaint,
history=[*confirmed, count_question], form={**known_form, 'vehicle_count': 3},
code='0003', count=3, not_matches='(几辆|多少辆|确认.*数量|没.*看到)'),
dict(name='feedback_form_one_other_field_missing', user='我还在现场。',
history=[*confirmed], form={'vehicle_count': 1, 'accident_cause': '倒车',
'has_non_motor_vehicle_hit': False, 'accident_time': TIME},
code='1002', count=1, contains='受伤',
not_matches='(几辆|多少辆|什么.*车型|是否.*一辆|只有.*一辆.*吗)'),
dict(name='feedback_known_count_in_form', user=complaint,
history=[*confirmed, count_question], form={**known_form, 'vehicle_count': 1},
code='2000', not_matches='(几辆|多少辆|具体的数字|没.*看到)'),
dict(name='feedback_form_false_and_unknown_count', user=complaint,
history=[*confirmed, count_question], form=known_form,
code='1002', count_absent=True,
matches='(其他|还有|几辆|多少辆|是否.*一辆|只有.*一辆)',
not_matches='(有没有人员受伤|是否涉及非机动车|现在还在事故现场|没.*看到|具体的数字)'),
dict(name='feedback_form_time_not_confirmed', user='这些我已经说过了。',
history=[], form={**known_form, 'vehicle_count': 1},
code='1002', contains='这个时间对吗'),
dict(name='feedback_correct_form_count', user='刚才的数量说错了,是两辆汽车。',
history=[*confirmed, count_question], form={**known_form, 'vehicle_count': 1},
code='2010', count=2),
dict(name='feedback_repeated_connection_check', user='喂喂喂,有人吗?', history=[
{'role':'assistant','content':'<state>1002</state>请简单描述一下事发经过,比如车辆大概是怎么撞在一起的?'},
{'role':'user','content':'喂喂喂,有人吗?'},
{'role':'assistant','content':'<state>1002</state>您好,我在的。请简单描述一下事发经过,比如车辆大概是怎么撞在一起的?'},
{'role':'user','content':'喂喂喂,有没有人,而且。'},
{'role':'assistant','content':'<state>1002</state>您好,我在的。请您简单说一下事发经过,比如车辆大概是怎么撞在一起的?'}],
code='1002', matches='(在的|我在|收到)',
not_matches='(简单.*(描述|说一下).*经过|能听到|听得.*清)'),
dict(name='feedback_no_evidence_of_previous_answer', user=complaint,
history=[count_question], code='1002', count_absent=True,
not_matches='(已记录.*一辆|已确认.*一辆|没.*看到|具体的数字)'),
dict(name='partial_symptom_negation', user='没有流血。', history=injury_q,
code='1002', injury=None, contains='受伤'),
dict(name='negation_with_positive_injury', user='没有流血,但胳膊受伤了。', history=injury_q,
@@ -33,9 +99,10 @@ def probes():
{'role':'user','content':'两辆汽车追尾,人员都没事。'},
{'role':'assistant','content':'<state>1002</state>请问事故中是否涉及非机动车或行人呢?'}],
code='1002', non_motor=None, contains='非机动车'),
dict(name='unknown_count_after_time', user='对', history=[
dict(name='single_reverse_count_after_time', user='对', history=[
{'role':'user','content':'十分钟前我倒车撞到护栏,没有人受伤,没有非机动车或行人,我还在现场。'},
{'role':'assistant','content':CONFIRM}], code='1002', contains='几辆', count_absent=True),
{'role':'assistant','content':CONFIRM}], code='2000', count=1,
not_matches='(几辆|多少辆|什么.*车型|是否.*一辆|只有.*一辆.*吗|这个时间对吗)'),
dict(name='known_count_after_time', user='对', history=[
{'role':'user','content':BASE}, {'role':'assistant','content':CONFIRM}], code='2010'),
dict(name='time_changed_requires_confirmation', user='不对,是今天十一点四十分。', history=[
@@ -70,22 +137,28 @@ async def main(args):
checks = []
evidence = {}
try:
state = {'stage_code': '1002', 'input_text': case['user'],
'dialogue': case['history'], 'accident_fields': case.get('form', {})}
if args.runtime:
result = await run_collect({'stage_code':'1002', 'input_text':case['user'],
'dialogue':case['history']}, llm, now=NOW)
result = await run_collect(state, llm, now=NOW)
raw = result['response_text']
fields = result['accident_fields']
else:
raw = await llm.complete(render_prompt(COLLECTION, {}, now=NOW), case['user'], history=case['history'])
user_message = _build_user_message(state, now=NOW)
raw = await llm.complete(render_prompt(COLLECTION, state, now=NOW), user_message, history=case['history'])
code, body = parse_state_reply(raw)
evidence['reply'] = raw
checks.append(dict(text='expected state '+case['code'], passed=code==case['code'], evidence=raw))
if 'contains' in case:
checks.append(dict(text='asks/mentions '+case['contains'], passed=case['contains'] in body, evidence=body))
for key, negate in [('matches', False), ('not_matches', True)]:
if key in case:
matched = bool(re.search(case[key], body))
checks.append(dict(text=key + ' ' + case[key], passed=matched != negate, evidence=body))
if args.runtime:
raw_fields = json.dumps(fields,ensure_ascii=False)
else:
raw_fields = await llm.complete(render_prompt(EXTRACTION, {}, now=NOW), case['user'], history=case['history'])
raw_fields = await llm.complete(render_prompt(EXTRACTION, state, now=NOW), user_message, history=case['history'])
fields = json.loads(raw_fields)
evidence['fields'] = fields
for probe_key, field in [('injury','has_injury'),('non_motor','has_non_motor_vehicle_hit')]:
@@ -93,6 +166,8 @@ async def main(args):
expected = case[probe_key]
ok = field not in fields if expected is None else fields.get(field) is expected
checks.append(dict(text=f'{field} expected {expected}', passed=ok, evidence=raw_fields))
if 'count' in case:
checks.append(dict(text='motor vehicle total ' + str(case['count']), passed=fields.get('vehicle_count') == case['count'], evidence=raw_fields))
if case.get('count_absent'):
checks.append(dict(text='no inferred motor vehicle total', passed='vehicle_count' not in fields, evidence=raw_fields))
if case.get('time_absent'):
@@ -100,8 +175,10 @@ async def main(args):
checks.append(dict(text='dialogue also avoids invented historical timestamp', passed=not re.search(r'\d{4}年.*点.*分',body), evidence=body))
except Exception as exc:
checks.append(dict(text='provider and output usable',passed=False,evidence=type(exc).__name__+': '+str(exc)))
return dict(name=case['name'], user=case['user'], history=case['history'], passed=all(c['passed'] for c in checks), checks=checks, **evidence)
selected = [c for c in probes() if not args.case or any(value in c['name'] for value in args.case)]
return dict(name=case['name'], user=case['user'], history=case['history'], form=case.get('form', {}), passed=all(c['passed'] for c in checks), checks=checks, **evidence)
selected = [c for c in probes()
if (not args.case or any(value in c['name'] for value in args.case))
and not any(value in c['name'] for value in args.exclude_case)]
if not selected:
raise SystemExit('No matching probes')
results = await asyncio.gather(*(run(c) for c in selected))
@@ -109,6 +186,8 @@ async def main(args):
from src.agent.prompts import load_prompt
profile = Config.llm_profile(Config.LLM_PROVIDER) or {}
output = dict(model=profile.get('model'), temperature=profile.get('temperature'),
probe_hash=sha256(json.dumps(selected, ensure_ascii=False, sort_keys=True).encode('utf-8')).hexdigest(),
excluded_cases=[c['name'] for c in probes() if any(value in c['name'] for value in args.exclude_case)],
execution_mode='runtime' if args.runtime else 'raw_prompt',
prompt_hashes={name:sha256(load_prompt(name).encode('utf-8')).hexdigest() for name in (COLLECTION,EXTRACTION)},
passed=sum(r['passed'] for r in results), total=len(results), results=results)
@@ -122,5 +201,6 @@ if __name__ == '__main__':
parser.add_argument('--runtime', action='store_true', help='Exercise production collection, including provenance validation')
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--case', action='append', default=[], help='Run only probe names containing this value')
parser.add_argument('--exclude-case', action='append', default=[], help='Defer probes whose names contain this value; recorded separately from passes')
parser.add_argument('--prompt-dir', type=Path, help='Optional snapshot containing collection.md and extraction.md')
asyncio.run(main(parser.parse_args()))