66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
"""CLI 演示:构建 mock 全链路路由系统,跑一组样例查询并打印结果。
|
|
|
|
用法:
|
|
python scripts/demo.py [--query "自定义查询"] [--batch]
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
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):
|
|
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 verbose:
|
|
print(f" --- response ---\n{r.response[:400]}")
|
|
|
|
|
|
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="打印响应正文")
|
|
args = parser.parse_args()
|
|
|
|
router = build_router()
|
|
print("系统组件:", router.health())
|
|
print()
|
|
|
|
if args.query:
|
|
await run_demo(router, [args.query], verbose=True)
|
|
else:
|
|
await run_demo(router, SAMPLE_QUERIES, verbose=args.verbose)
|
|
|
|
print()
|
|
print("=" * 72)
|
|
print("运行指标:", router.stats.summary())
|
|
print("缓存统计:", router.cache.stats())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|
|
|