76 lines
2.9 KiB
Python
76 lines
2.9 KiB
Python
"""CLI 演示:构建专家系统内核路由(默认 L0 零参数模式),跑样例查询并打印推理链。
|
|
|
|
用法:
|
|
python scripts/demo.py [--query "自定义查询"] [--batch] [--trace] [--verbose]
|
|
--trace 打印完整推理链(分类 → 拆解 DAG → 规则轨迹 → 子任务执行 → Judge)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
if hasattr(sys.stdout, "reconfigure"):
|
|
sys.stdout.reconfigure(encoding="utf-8")
|
|
sys.stderr.reconfigure(encoding="utf-8")
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from router_system.router import build_router
|
|
|
|
SAMPLE_QUERIES = [
|
|
"用 Python 写一个快速排序函数,并解释时间复杂度",
|
|
"求解方程 x^2 - 5x + 6 = 0",
|
|
"劳动合同里约定离职后两年内不得从事同行业,是否有效?",
|
|
"高血压患者日常饮食需要注意什么?",
|
|
"给我总结一下深度学习中注意力机制的优缺点",
|
|
"为什么天空是蓝色的?",
|
|
"帮我调试这段代码:def f(x): return x + 1 报 TypeError",
|
|
"求 ∫ x^2 dx 从 0 到 1 的定积分是多少?",
|
|
]
|
|
|
|
|
|
async def run_demo(router, queries, verbose: bool = False, trace: bool = False):
|
|
for q in queries:
|
|
r = await router.route(q)
|
|
print("=" * 72)
|
|
print(f"Q: {q}")
|
|
print(f" domain={r.domain} difficulty={r.difficulty} conf={r.confidence:.2f} "
|
|
f"upgraded={r.upgraded} quality={r.quality_score:.2f} model={r.model_used} "
|
|
f"latency={r.latency_ms:.1f}ms cache={r.cache_hit}({r.cache_level}) cost=${r.cost_est:.6f}")
|
|
print(f" route: {' -> '.join(r.route)}")
|
|
if trace:
|
|
# 拆解轨迹从 route 中展开为更可读的形式
|
|
plan_steps = [s for s in r.route if s.startswith("plan:") or ":" in s]
|
|
print(" 推理链: " + " -> ".join(r.route))
|
|
if verbose:
|
|
print(f" --- response ---\n{r.response[:600]}")
|
|
|
|
|
|
async def main():
|
|
parser = argparse.ArgumentParser(description="多专家路由系统(专家系统内核)CLI 演示")
|
|
parser.add_argument("--query", type=str, default=None, help="单条查询(覆盖默认样例)")
|
|
parser.add_argument("--batch", action="store_true", help="批量模式(打印全部响应)")
|
|
parser.add_argument("--verbose", action="store_true", help="打印响应正文")
|
|
parser.add_argument("--trace", action="store_true", help="打印完整推理链")
|
|
args = parser.parse_args()
|
|
|
|
router = build_router()
|
|
print("系统组件:", router.health())
|
|
print()
|
|
|
|
if args.query:
|
|
await run_demo(router, [args.query], verbose=True, trace=True)
|
|
else:
|
|
await run_demo(router, SAMPLE_QUERIES, verbose=args.verbose, trace=args.trace)
|
|
|
|
print()
|
|
print("=" * 72)
|
|
print("运行指标:", router.stats.summary())
|
|
print("缓存统计:", router.cache.stats())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|