| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- """Router Agent 命令行交互客户端
- 用法:
- uv run python chat_with_router.py
- """
- import asyncio
- import sys
- import httpx
- # 修复 Windows 控制台编码问题
- if sys.platform == 'win32':
- import io
- sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
- sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
- BASE_URL = "http://127.0.0.1:8001"
- async def chat_loop():
- """交互式对话循环"""
- print("=" * 60)
- print("Router Agent - 命令行客户端")
- print("=" * 60)
- print("提示:")
- print(" - 输入你的问题或请求")
- print(" - 输入 'exit' 或 'quit' 退出")
- print(" - 输入 'help' 查看可用命令")
- print("-" * 60)
- session_id = None
- async with httpx.AsyncClient(timeout=120.0) as client:
- # 检查服务是否可用
- try:
- await client.get(f"{BASE_URL}/health")
- except Exception as e:
- print(f"\n错误: 无法连接到 Router Agent 服务")
- print(f"请确保服务已启动: uv run python -m tool_agent")
- print(f"详细错误: {e}")
- return
- while True:
- try:
- # 读取用户输入
- user_input = await asyncio.to_thread(input, "\n[You] > ")
- user_input = user_input.strip()
- if not user_input:
- continue
- # 处理退出命令
- if user_input.lower() in ("exit", "quit"):
- print("\n再见!")
- break
- # 处理帮助命令
- if user_input.lower() == "help":
- print("\n可用命令:")
- print(" list - 列出所有工具")
- print(" search <关键词> - 搜索工具")
- print(" status - 查看工具状态")
- print(" exit/quit - 退出")
- print("\n你也可以直接用自然语言提问,例如:")
- print(" - 我需要一个图片压缩工具")
- print(" - image_stitcher 怎么用?")
- print(" - 帮我调用 XXX 工具")
- continue
- # 发送消息给 Router Agent
- print("\n[Router Agent] 思考中...")
- response = await client.post(
- f"{BASE_URL}/chat",
- json={
- "message": user_input,
- "session_id": session_id
- }
- )
- if response.status_code == 200:
- result = response.json()
- session_id = result.get("session_id")
- print(f"\n[Router Agent]\n{result['response']}")
- else:
- print(f"\n错误: {response.status_code} - {response.text}")
- except KeyboardInterrupt:
- print("\n\n按 Ctrl+C 退出,或输入 'exit'")
- except EOFError:
- print("\n\n再见!")
- break
- except Exception as e:
- print(f"\n错误: {e}")
- if __name__ == "__main__":
- asyncio.run(chat_loop())
|