test_router_chat_api.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """测试 Router Agent 的 HTTP 对话接口"""
  2. import asyncio
  3. import httpx
  4. import sys
  5. # 修复 Windows 控制台编码问题
  6. if sys.platform == 'win32':
  7. import io
  8. sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
  9. sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
  10. async def test_chat_api():
  11. """测试 Router Agent 的对话接口"""
  12. base_url = "http://127.0.0.1:8001"
  13. print("=" * 60)
  14. print("Testing Router Agent Chat API")
  15. print("=" * 60)
  16. async with httpx.AsyncClient(timeout=60.0) as client:
  17. # 测试 1: 健康检查
  18. print("\n[Test 1] Health check...")
  19. try:
  20. response = await client.get(f"{base_url}/health")
  21. print(f"✓ Health: {response.json()}")
  22. except Exception as e:
  23. print(f"✗ Health check failed: {e}")
  24. return
  25. # 测试 2: 对话 - 列出工具
  26. print("\n[Test 2] Chat - List tools...")
  27. try:
  28. response = await client.post(
  29. f"{base_url}/chat",
  30. json={"message": "列出所有可用的工具"}
  31. )
  32. result = response.json()
  33. print(f"✓ Response:\n{result.get('response', result)}")
  34. except Exception as e:
  35. print(f"✗ Chat failed: {e}")
  36. # 测试 3: 对话 - 询问工具使用
  37. print("\n[Test 3] Chat - Ask about tool usage...")
  38. try:
  39. response = await client.post(
  40. f"{base_url}/chat",
  41. json={"message": "image_stitcher 工具怎么用?"}
  42. )
  43. result = response.json()
  44. print(f"✓ Response:\n{result['response']}")
  45. except Exception as e:
  46. print(f"✗ Chat failed: {e}")
  47. # 测试 4: 对话 - 请求创建工具
  48. print("\n[Test 4] Chat - Request tool creation...")
  49. try:
  50. response = await client.post(
  51. f"{base_url}/chat",
  52. json={"message": "我需要一个 PDF 转图片的工具"}
  53. )
  54. result = response.json()
  55. print(f"✓ Response:\n{result['response']}")
  56. except Exception as e:
  57. print(f"✗ Chat failed: {e}")
  58. print("\n" + "=" * 60)
  59. print("Tests completed")
  60. print("=" * 60)
  61. if __name__ == "__main__":
  62. asyncio.run(test_chat_api())