test_liblibai_tool.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """测试 liblibai_controlnet 工具 — 通过 Router API 调用
  2. 用法:
  3. 1. 先启动 Router:uv run python -m tool_agent
  4. 2. 运行测试:python tests/test_liblibai_tool.py
  5. """
  6. import sys
  7. import time
  8. if sys.platform == 'win32':
  9. sys.stdout.reconfigure(encoding='utf-8')
  10. import httpx
  11. ROUTER_URL = "http://127.0.0.1:8001"
  12. IMAGE_URL = "https://liblibai-airship-temp.oss-cn-beijing.aliyuncs.com/aliyun-cn-prod/73ed6ae42b144d21bf566e05b5a6c138.png"
  13. def main():
  14. print("=" * 50)
  15. print("测试 liblibai_controlnet 工具")
  16. print("=" * 50)
  17. # 1. 检查 Router 是否在线
  18. try:
  19. resp = httpx.get(f"{ROUTER_URL}/health", timeout=3)
  20. print(f"Router 状态: {resp.json()}")
  21. except httpx.ConnectError:
  22. print(f"无法连接 Router ({ROUTER_URL})")
  23. print("请先启动: uv run python -m tool_agent")
  24. sys.exit(1)
  25. # 2. 搜索工具,确认已注册
  26. print("\n--- 获取工具 ---")
  27. resp = httpx.get(f"{ROUTER_URL}/tools")
  28. tools = resp.json()
  29. liblib_tools = [t for t in tools["tools"] if "liblib" in t["tool_id"] or "liblib" in t["name"].lower()]
  30. print(f"找到 {len(liblib_tools)} 个匹配 liblib 的工具")
  31. for t in liblib_tools:
  32. print(f" {t['tool_id']}: {t['name']} (state={t['state']})")
  33. # 3. 调用 liblibai_controlnet 工具
  34. print("\n--- 调用 ControlNet 生图 ---")
  35. print(f"图片: {IMAGE_URL}")
  36. print(f"提示词: simple white line art, cat, black background")
  37. print("提交中...")
  38. resp = httpx.post(
  39. f"{ROUTER_URL}/run_tool",
  40. json={
  41. "tool_id": "liblibai_controlnet",
  42. "params": {
  43. "image": IMAGE_URL,
  44. "prompt": "simple white line art, cat, black background",
  45. "negative_prompt": "lowres, bad anatomy, text, error",
  46. "width": 512,
  47. "height": 512,
  48. "steps": 20,
  49. "cfg_scale": 7,
  50. "img_count": 1,
  51. "control_weight": 1.0,
  52. "preprocessor": 1,
  53. "canny_low": 100,
  54. "canny_high": 200
  55. }
  56. },
  57. timeout=300 # 生图可能需要几分钟
  58. )
  59. result = resp.json()
  60. print(f"\n响应状态: {result.get('status')}")
  61. if result.get("status") == "success":
  62. data = result.get("result", {})
  63. print(f"任务状态: {data.get('status')}")
  64. print(f"任务 ID: {data.get('task_id')}")
  65. images = data.get("images", [])
  66. if images:
  67. print(f"生成图片数: {len(images)}")
  68. for i, url in enumerate(images):
  69. print(f" [{i+1}] {url}")
  70. print("\n测试通过!")
  71. else:
  72. print("未返回图片,可能任务仍在处理中")
  73. else:
  74. print(f"错误: {result.get('error')}")
  75. if __name__ == "__main__":
  76. main()