chat_service.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from typing import Dict, Any, List, Optional
  2. class ChatService:
  3. def __init__(self, http_client):
  4. self.http_client = http_client
  5. def create_chat(self, name: str, dataset_ids: List[str], llm: Dict[str, Any],
  6. prompt: str = None) -> Dict[str, Any]:
  7. endpoint = "/api/v1/chats"
  8. data = {
  9. "name": name,
  10. "dataset_ids": dataset_ids,
  11. "llm": llm
  12. }
  13. if prompt is not None:
  14. data["prompt"] = prompt
  15. response = self.http_client.post(endpoint, json=data)
  16. if response.get("code") == 0 and response.get("data"):
  17. return response["data"]
  18. else:
  19. raise Exception(f"创建聊天失败: {response.get('message', '未知错误')}")
  20. def update_chat(self, chat_id: str, name: str = None, dataset_ids: List[str] = None,
  21. llm: Dict[str, Any] = None, prompt: str = None) -> Dict[str, Any]:
  22. endpoint = f"/api/v1/chats/{chat_id}"
  23. data = {}
  24. if name is not None:
  25. data["name"] = name
  26. if dataset_ids is not None:
  27. data["dataset_ids"] = dataset_ids
  28. if llm is not None:
  29. data["llm"] = llm
  30. if prompt is not None:
  31. data["prompt"] = prompt
  32. response = self.http_client.post(endpoint, json=data)
  33. if response.get("code") == 0 and response.get("data"):
  34. return response["data"]
  35. else:
  36. raise Exception(f"更新聊天失败: {response.get('message', '未知错误')}")
  37. def delete_chats(self, chat_ids: List[str]) -> bool:
  38. endpoint = "/api/v1/chats"
  39. response = self.http_client.post(endpoint, json={"chat_ids": chat_ids})
  40. if response.get("code") == 0:
  41. return True
  42. else:
  43. raise Exception(f"删除聊天失败: {response.get('message', '未知错误')}")
  44. def list_chats(self, page: int = 1, size: int = 20, orderby: str = "create_time",
  45. desc: bool = True, name: str = None, chat_id: str = None) -> List[Dict[str, Any]]:
  46. endpoint = "/api/v1/chats"
  47. params = {"page": page, "page_size": size, "orderby": orderby, "desc": int(desc)}
  48. if name is not None:
  49. params["name"] = name
  50. if chat_id is not None:
  51. params["id"] = chat_id
  52. response = self.http_client.get(endpoint, params=params)
  53. if response.get("code") == 0 and response.get("data"):
  54. return response["data"]
  55. else:
  56. raise Exception(f"列出聊天失败: {response.get('message', '未知错误')}")
  57. def create_chat_session(self, chat_id: str, name: str = None) -> Dict[str, Any]:
  58. endpoint = f"/api/v1/chats/{chat_id}/sessions"
  59. data = {}
  60. if name is not None:
  61. data["name"] = name
  62. response = self.http_client.post(endpoint, json=data)
  63. if response.get("code") == 0 and response.get("data"):
  64. return response["data"]
  65. else:
  66. raise Exception(f"创建会话失败: {response.get('message', '未知错误')}")
  67. def update_chat_session(self, chat_id: str, session_id: str,
  68. name: str = None, message: List[Dict] = None) -> Dict[str, Any]:
  69. endpoint = f"/api/v1/chats/{chat_id}/sessions/{session_id}"
  70. data = {}
  71. if name is not None:
  72. data["name"] = name
  73. if message is not None:
  74. data["message"] = message
  75. response = self.http_client.post(endpoint, json=data)
  76. if response.get("code") == 0 and response.get("data"):
  77. return response["data"]
  78. else:
  79. raise Exception(f"更新会话失败: {response.get('message', '未知错误')}")
  80. def list_chat_sessions(self, chat_id: str, page: int = 1, size: int = 20,
  81. orderby: str = "create_time", desc: bool = True,
  82. session_id: str = None, session_name: str = None) -> List[Dict[str, Any]]:
  83. endpoint = f"/api/v1/chats/{chat_id}/sessions"
  84. params = {"page": page, "page_size": size, "orderby": orderby, "desc": int(desc)}
  85. if session_id is not None:
  86. params["id"] = session_id
  87. if session_name is not None:
  88. params["name"] = session_name
  89. response = self.http_client.get(endpoint, params=params)
  90. if response.get("code") == 0 and response.get("data"):
  91. return response["data"]
  92. else:
  93. raise Exception(f"列出会话失败: {response.get('message', '未知错误')}")
  94. def delete_chat_session(self, chat_id: str, session_id: str) -> bool:
  95. endpoint = f"/api/v1/chats/{chat_id}/sessions"
  96. response = self.http_client.post(endpoint, json={"session_ids": [session_id]})
  97. if response.get("code") == 0:
  98. return True
  99. else:
  100. raise Exception(f"删除会话失败: {response.get('message', '未知错误')}")
  101. def chat_completion(self, chat_id: str, query: str, stream: bool = False,
  102. session_id: str = None) -> Dict[str, Any]:
  103. endpoint = f"/api/v1/chats/{chat_id}/completions"
  104. data = {"query": query, "stream": stream}
  105. if session_id is not None:
  106. data["session_id"] = session_id
  107. response = self.http_client.post(endpoint, json=data)
  108. if response.get("code") == 0:
  109. return response.get("data", {})
  110. else:
  111. raise Exception(f"聊天完成失败: {response.get('message', '未知错误')}")