run_local.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """本地跑需求生成:只写文件,不入库。
  2. 在项目根目录执行:
  3. .venv/bin/python examples/demand/run_local.py
  4. 默认:品类「早中晚好」,生成约 100 条。结果文件:
  5. examples/demand/result/早中晚好.json
  6. examples/demand/result/<execution_id>/execution_id_<execution_id>_demand_items.json
  7. 可选参数:
  8. .venv/bin/python examples/demand/run_local.py --cluster 早中晚好 --count 100
  9. """
  10. from __future__ import annotations
  11. import argparse
  12. import asyncio
  13. import os
  14. import sys
  15. from pathlib import Path
  16. ROOT = Path(__file__).resolve().parents[2]
  17. DEMAND_DIR = Path(__file__).resolve().parent
  18. sys.path.insert(0, str(ROOT))
  19. os.chdir(DEMAND_DIR)
  20. from examples.demand.run import main as run_demand
  21. def parse_args() -> argparse.Namespace:
  22. parser = argparse.ArgumentParser(description="本地生成需求并写入 JSON,不入库")
  23. parser.add_argument("--cluster", default="早中晚好", help="二级品类名")
  24. parser.add_argument("--platform", default="piaoquan", choices=["piaoquan", "changwen", "zengzhang"])
  25. parser.add_argument("--count", type=int, default=100, help="目标需求数量")
  26. return parser.parse_args()
  27. async def _run() -> None:
  28. args = parse_args()
  29. print(
  30. f"[local] cluster={args.cluster} platform={args.platform} "
  31. f"count={args.count} write_to_db=False",
  32. flush=True,
  33. )
  34. result = await run_demand(
  35. args.cluster,
  36. args.platform,
  37. args.count,
  38. write_to_db=False,
  39. )
  40. execution_id = result.get("execution_id")
  41. named_path = DEMAND_DIR / "result" / f"{args.cluster}.json"
  42. items_path = (
  43. DEMAND_DIR
  44. / "result"
  45. / str(execution_id)
  46. / f"execution_id_{execution_id}_demand_items.json"
  47. )
  48. print(f"[local] execution_id={execution_id}", flush=True)
  49. print(f"[local] 需求文件: {named_path}", flush=True)
  50. print(f"[local] Agent 原始结果: {items_path}", flush=True)
  51. if not execution_id:
  52. raise SystemExit("执行失败:未拿到 execution_id(请检查品类数据和数据库连接)")
  53. if __name__ == "__main__":
  54. asyncio.run(_run())