121 lines
10 KiB
Python
121 lines
10 KiB
Python
"""A fixed, authored Chinese text-only diagnostic; not a general Chinese benchmark."""
|
||
import argparse
|
||
import hashlib
|
||
import json
|
||
import math
|
||
from pathlib import Path
|
||
import statistics
|
||
import time
|
||
import urllib.request
|
||
|
||
# (category, supplied evidence, question, expected answer, optional choice descriptions).
|
||
# Gold answers are fixed before inference. None means insufficient evidence or no matching option.
|
||
CASES = [
|
||
("直接理解", "盒子里有两个红色零件和一个蓝色零件。", "这个盒子是空的吗?", False, None),
|
||
("直接理解", "纸盒内部没有任何物品。", "这个纸盒是空的吗?", True, None),
|
||
("直接理解", "小王右手握着一把螺丝刀。", "小王手上拿着东西吗?", True, None),
|
||
("直接理解", "小王双手摊开,两只手上都没有物品。", "小王手上拿着东西吗?", False, None),
|
||
("数量计算", "盒内原有三个零件,又放入两个零件,没有取出。", "现在盒内共有五个零件吗?", True, None),
|
||
("数量计算", "托盘原有十颗螺丝,取走四颗,没有其他变化。", "托盘里还剩七颗螺丝吗?", False, None),
|
||
("数量计算", "甲盒有七个零件,乙盒有十二个零件。", "甲盒的零件比乙盒多吗?", False, None),
|
||
("数量计算", "比较两个小数:0.9 和 0.11。", "哪个数更大?", "0.9", {"0.9": "小数零点九", "0.11": "小数零点一一"}),
|
||
("数量计算", "五个零件的颜色依次是红、蓝、蓝、红、绿。", "蓝色零件一共有几个?", "两个", {"一个": "一个", "两个": "两个", "三个": "三个"}),
|
||
("数量计算", "有两个纸盒,每盒恰好三颗螺丝。", "这两个纸盒合计六颗螺丝吗?", True, None),
|
||
("条件规则", "规则:当且仅当底座已到位且上盖已扣紧,步骤才算完成。当前底座已到位,上盖已扣紧。", "当前步骤完成了吗?", True, None),
|
||
("条件规则", "规则:当且仅当底座已到位且上盖已扣紧,步骤才算完成。当前底座已到位,上盖未扣紧。", "当前步骤完成了吗?", False, None),
|
||
("条件规则", "规则:当且仅当左按钮或右按钮至少有一个按下时,指示灯亮。左按钮未按下,右按钮已按下。", "指示灯亮吗?", True, None),
|
||
("条件规则", "规则:当且仅当左按钮或右按钮至少有一个按下时,指示灯亮。两个按钮都未按下。", "指示灯亮吗?", False, None),
|
||
("条件规则", "规则:设备就绪并且没有处于维护模式时才允许运行,除此之外不允许。当前设备已就绪,但处于维护模式。", "当前允许设备运行吗?", False, None),
|
||
("条件规则", "规则:耗时严格大于五秒才算超时。当前耗时恰好五秒。", "当前已经超时了吗?", False, None),
|
||
("条件规则", "规则:耗时严格大于五秒才算超时。当前耗时六秒。", "当前已经超时了吗?", True, None),
|
||
("条件规则", "操作记录:先放入底座,再放入上盖,最后拧紧螺丝。", "拧紧螺丝发生在放入上盖之后吗?", True, None),
|
||
("否定与关系", "记录写明:不存在尚未完成的步骤。", "是否所有步骤都已完成?", True, None),
|
||
("否定与关系", "小李没有拿红色零件,他拿的是蓝色零件。", "小李拿的是红色零件吗?", False, None),
|
||
("否定与关系", "在同一条水平直线上,甲在乙的左侧,乙在丙的左侧。", "甲在丙的左侧吗?", True, None),
|
||
("中文多选", "手中物品的表面同时有红色和蓝色。", "手中物品是什么颜色?", "有红有蓝", {"黄": "黄色", "绿": "绿色", "有红有蓝": "同时有红色和蓝色"}),
|
||
("中文多选", "工具箱里有一把螺丝刀、一只杯子和一块毛巾。", "哪个物品通常用于拧螺丝?", "螺丝刀", {"杯子": "杯子", "毛巾": "毛巾", "螺丝刀": "螺丝刀"}),
|
||
("中文多选", "工序记录依次为:取料、装配、检查。", "第二个工序是什么?", "装配", {"检查": "检查", "取料": "取料", "装配": "装配"}),
|
||
("信息不足", "桌上有一个打开的纸盒,记录没有说明盒内情况,也没有提供图片。", "盒子里面是空的吗?", None, None),
|
||
("信息不足", "小王把右手伸到了桌子上方,记录没有说明手里有没有物品,也没有提供图片。", "小王右手拿着东西吗?", None, None),
|
||
("信息不足", "记录只写着:有人在工位操作。没有关于零件颜色的其他信息或图片。", "手中零件是什么颜色?", None, {"黄": "黄色", "绿": "绿色", "有红有蓝": "同时有红色和蓝色"}),
|
||
("信息不足", "完成条件是底座到位且上盖扣紧。记录只确认底座已到位,上盖的状态没有记录。", "当前步骤已经完成了吗?", None, None),
|
||
("信息不足", "规则只规定:只有校验通过,设备才可以启动。现在校验通过了,设备是否实际启动没有记录。", "设备现在已经启动了吗?", None, None),
|
||
("选项不匹配", "手中零件只有紫色,没有其他颜色。", "手中零件是什么颜色?如果没有匹配的选项,请返回不确定。", None, {"黄": "黄色", "绿": "绿色", "有红有蓝": "同时有红色和蓝色"}),
|
||
("信息不足", "第一次检查通过了。记录没有包含第二次检查的结果。", "第二次检查也通过了吗?", None, None),
|
||
("信息不足", "甲盒里有三个零件,乙盒内的数量没有记录。", "甲盒和乙盒的零件数量相同吗?", None, None),
|
||
]
|
||
|
||
|
||
def post(endpoint, payload):
|
||
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
||
request = urllib.request.Request(endpoint, data=data, headers={"Content-Type": "application/json; charset=utf-8"})
|
||
started = time.perf_counter()
|
||
with urllib.request.urlopen(request, timeout=60) as response:
|
||
content = response.read()
|
||
return json.loads(content), round((time.perf_counter() - started) * 1000, 2)
|
||
|
||
|
||
def label(answer):
|
||
if answer["abstained"]:
|
||
return None
|
||
if answer["type"] == "choice":
|
||
return answer["choice"]
|
||
probability = answer["noul"]
|
||
return None if abs(probability - .5) < 1e-9 else probability > .5
|
||
|
||
|
||
def readable(value):
|
||
return "不确定" if value is None else "是" if value is True else "否" if value is False else str(value)
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser(description=__doc__)
|
||
parser.add_argument("--endpoint", default="http://127.0.0.1:8765/v1/systemone")
|
||
parser.add_argument("--output", type=Path, required=True)
|
||
args = parser.parse_args()
|
||
corpus = []
|
||
for index, (category, state, question, expected, choices) in enumerate(CASES, 1):
|
||
item = {"type": "choice" if choices else "noul", "instructions": question}
|
||
if choices:
|
||
item["criteria"] = choices
|
||
corpus.append({"id": f"q{index:02d}", "category": category, "expected": expected,
|
||
"payload": {"state": state, "questions": {f"q{index:02d}": item}}})
|
||
report = {"endpoint": args.endpoint, "scope": "Authored, text-only Chinese diagnostic; fixed gold answers; one question per request; no translation or images", "corpus_sha256": hashlib.sha256(json.dumps(corpus, ensure_ascii=False, sort_keys=True).encode()).hexdigest(), "runs": []}
|
||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||
# Save the corpus, including its gold answers, before any inference.
|
||
args.output.with_name(args.output.stem + "-questions.json").write_text(json.dumps(corpus, ensure_ascii=False, indent=2), encoding="utf-8")
|
||
warmup, elapsed = post(args.endpoint, {"state": "桌上有一个空盒。", "questions": {"warmup": {"type": "noul", "instructions": "盒子是空的吗?"}}})
|
||
report["warmup"] = {"http_ms": elapsed, "response": warmup}
|
||
lines = ["Imajev-2B 纯中文文本诊断(固定题目与标准答案,不含图片或翻译)", ""]
|
||
for case in corpus:
|
||
result, elapsed = post(args.endpoint, case["payload"])
|
||
assert result["usage"]["images"] == [], "Unexpected image input"
|
||
answer = result["answers"][case["id"]]
|
||
actual = label(answer)
|
||
row = {**case, "actual": actual, "correct": actual == case["expected"], "http_ms": elapsed,
|
||
"unknown_probability": answer["unknown_probability"], "response": result}
|
||
report["runs"].append(row)
|
||
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||
options = case["payload"]["questions"][case["id"]].get("criteria")
|
||
lines += [f"{case['id']} [{case['category']}]", "材料:" + case["payload"]["state"],
|
||
"问题:" + case["payload"]["questions"][case["id"]]["instructions"]]
|
||
if options:
|
||
lines.append("选项:" + "、".join(options))
|
||
lines += [f"标准:{readable(case['expected'])};模型:{readable(actual)};{'正确' if row['correct'] else '错误'};耗时 {elapsed:.2f}ms;未知概率 {answer['unknown_probability']:.2%}", ""]
|
||
print(json.dumps({"id": case["id"], "category": case["category"], "expected": case["expected"], "actual": actual, "correct": row["correct"], "http_ms": elapsed}, ensure_ascii=False), flush=True)
|
||
known = [r for r in report["runs"] if r["expected"] is not None]
|
||
unknown = [r for r in report["runs"] if r["expected"] is None]
|
||
times = sorted(r["http_ms"] for r in report["runs"])
|
||
report["summary"] = {"count": len(times), "correct": sum(r["correct"] for r in report["runs"]),
|
||
"answerable_count": len(known), "answerable_correct": sum(r["correct"] for r in known),
|
||
"unknown_count": len(unknown), "unknown_correct": sum(r["correct"] for r in unknown),
|
||
"mean_http_ms": round(statistics.mean(times), 2), "p95_http_ms": times[math.ceil(.95 * len(times)) - 1],
|
||
"max_http_ms": max(times), "under_200ms": sum(t < 200 for t in times)}
|
||
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||
args.output.with_suffix(".txt").write_text("\n".join(lines), encoding="utf-8")
|
||
print(json.dumps(report["summary"], ensure_ascii=False), flush=True)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|