test_liblibai_tool.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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://43.106.118.91: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.post(f"{ROUTER_URL}/search_tools", json={"keyword": "liblib"})
  28. tools = resp.json()
  29. print(f"找到 {tools['total']} 个工具")
  30. for t in tools["tools"]:
  31. print(f" {t['tool_id']}: {t['name']} (state={t['state']})")
  32. # 3. 调用 liblibai_controlnet 工具
  33. print("\n--- 调用 ControlNet 生图 ---")
  34. print(f"图片: {IMAGE_URL}")
  35. print(f"提示词: simple white line art, cat, black background")
  36. print("提交中...")
  37. resp = httpx.post(
  38. f"{ROUTER_URL}/select_tool",
  39. json={
  40. "tool_id": "liblibai_controlnet",
  41. "params": {
  42. "image": IMAGE_URL,
  43. "prompt": "simple white line art, cat, black background",
  44. "negative_prompt": "lowres, bad anatomy, text, error",
  45. "width": 512,
  46. "height": 512,
  47. "steps": 20,
  48. "cfg_scale": 7,
  49. "img_count": 1,
  50. "control_weight": 1.0,
  51. "preprocessor": 1,
  52. "canny_low": 100,
  53. "canny_high": 200
  54. }
  55. },
  56. timeout=300 # 生图可能需要几分钟
  57. )
  58. result = resp.json()
  59. print(f"\n响应状态: {result.get('status')}")
  60. if result.get("status") == "success":
  61. data = result.get("result", {})
  62. print(f"任务状态: {data.get('status')}")
  63. print(f"任务 ID: {data.get('task_id')}")
  64. images = data.get("images", [])
  65. if images:
  66. print(f"生成图片数: {len(images)}")
  67. for i, url in enumerate(images):
  68. print(f" [{i+1}] {url}")
  69. print("\n测试通过!")
  70. else:
  71. print("未返回图片,可能任务仍在处理中")
  72. else:
  73. print(f"错误: {result.get('error')}")
  74. if __name__ == "__main__":
  75. main()