agent_service.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from typing import Dict, Any, List, Optional
  2. class AgentService:
  3. def __init__(self, http_client):
  4. self.http_client = http_client
  5. def create_agent(self, name: str, llm: Dict[str, Any], description: str = None) -> Dict[str, Any]:
  6. endpoint = "/api/v1/agents"
  7. data = {"name": name, "llm": llm}
  8. if description is not None:
  9. data["description"] = description
  10. response = self.http_client.post(endpoint, json_data=data)
  11. if response.get("code") == 0 and response.get("data"):
  12. return response["data"]
  13. else:
  14. raise Exception(f"创建代理失败: {response.get('message', '未知错误')}")
  15. def update_agent(self, agent_id: str, name: str = None, llm: Dict[str, Any] = None,
  16. description: str = None) -> Dict[str, Any]:
  17. endpoint = f"/api/v1/agents/{agent_id}"
  18. data = {}
  19. if name is not None:
  20. data["name"] = name
  21. if llm is not None:
  22. data["llm"] = llm
  23. if description is not None:
  24. data["description"] = description
  25. response = self.http_client.post(endpoint, json=data)
  26. if response.get("code") == 0 and response.get("data"):
  27. return response["data"]
  28. else:
  29. raise Exception(f"更新代理失败: {response.get('message', '未知错误')}")
  30. def delete_agent(self, agent_id: str) -> bool:
  31. endpoint = f"/api/v1/agents/{agent_id}"
  32. response = self.http_client.post(endpoint, json_data={})
  33. if response.get("code") == 0:
  34. return True
  35. else:
  36. raise Exception(f"删除代理失败: {response.get('message', '未知错误')}")
  37. def list_agents(self, page: int = 1, size: int = 20, orderby: str = "create_time",
  38. desc: bool = True, name: str = None, agent_id: str = None) -> List[Dict[str, Any]]:
  39. endpoint = "/api/v1/agents"
  40. params = {"page": page, "page_size": size, "orderby": orderby, "desc": int(desc)}
  41. if name is not None:
  42. params["name"] = name
  43. if agent_id is not None:
  44. params["id"] = agent_id
  45. response = self.http_client.get(endpoint, params=params)
  46. if response.get("code") == 0 and response.get("data"):
  47. return response["data"]
  48. else:
  49. raise Exception(f"列出代理失败: {response.get('message', '未知错误')}")
  50. def create_agent_session(self, agent_id: str, name: str = None) -> Dict[str, Any]:
  51. endpoint = f"/api/v1/agents/{agent_id}/sessions"
  52. data = {}
  53. if name is not None:
  54. data["name"] = name
  55. response = self.http_client.post(endpoint, json=data)
  56. if response.get("code") == 0 and response.get("data"):
  57. return response["data"]
  58. else:
  59. raise Exception(f"创建代理会话失败: {response.get('message', '未知错误')}")
  60. def list_agent_sessions(self, agent_id: str, page: int = 1, size: int = 20,
  61. orderby: str = "create_time", desc: bool = True,
  62. session_id: str = None, user_id: str = None,
  63. dsl: str = None) -> List[Dict[str, Any]]:
  64. endpoint = f"/api/v1/agents/{agent_id}/sessions"
  65. params = {"page": page, "page_size": size, "orderby": orderby, "desc": int(desc)}
  66. if session_id is not None:
  67. params["id"] = session_id
  68. if user_id is not None:
  69. params["user_id"] = user_id
  70. if dsl is not None:
  71. params["dsl"] = dsl
  72. response = self.http_client.get(endpoint, params=params)
  73. if response.get("code") == 0 and response.get("data"):
  74. return response["data"]
  75. else:
  76. raise Exception(f"列出代理会话失败: {response.get('message', '未知错误')}")
  77. def delete_agent_session(self, agent_id: str, session_id: str) -> bool:
  78. endpoint = f"/api/v1/agents/{agent_id}/sessions"
  79. response = self.http_client.post(endpoint, json_data={"session_ids": [session_id]})
  80. if response.get("code") == 0:
  81. return True
  82. else:
  83. raise Exception(f"删除代理会话失败: {response.get('message', '未知错误')}")
  84. def agent_completion(self, agent_id: str, query: str, stream: bool = False,
  85. session_id: str = None) -> Dict[str, Any]:
  86. endpoint = f"/api/v1/agents/{agent_id}/completions"
  87. data = {"query": query, "stream": stream}
  88. if session_id is not None:
  89. data["session_id"] = session_id
  90. response = self.http_client.post(endpoint, json=data)
  91. if response.get("code") == 0:
  92. return response.get("data", {})
  93. else:
  94. raise Exception(f"代理完成失败: {response.get('message', '未知错误')}")
  95. def get_related_questions(self, dataset_id: str, question: str, top: int = 10) -> List[str]:
  96. endpoint = "/api/v1/sessions/related_questions"
  97. response = self.http_client.post(endpoint, json_data={
  98. "dataset_id": dataset_id,
  99. "question": question,
  100. "top": top
  101. })
  102. if response.get("code") == 0 and response.get("data"):
  103. return response["data"]
  104. else:
  105. raise Exception(f"获取相关问题失败: {response.get('message', '未知错误')}")