您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

session.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. from .base import Base
  18. class Session(Base):
  19. def __init__(self, rag, res_dict):
  20. self.id = None
  21. self.name = "New session"
  22. self.messages = [{"role": "assistant", "content": "Hi! I am your assistant, can I help you?"}]
  23. for key, value in res_dict.items():
  24. if key == "chat_id" and value is not None:
  25. self.chat_id = None
  26. self.__session_type = "chat"
  27. if key == "agent_id" and value is not None:
  28. self.agent_id = None
  29. self.__session_type = "agent"
  30. super().__init__(rag, res_dict)
  31. def ask(self, question="", stream=True, **kwargs):
  32. if self.__session_type == "agent":
  33. res = self._ask_agent(question, stream)
  34. elif self.__session_type == "chat":
  35. res = self._ask_chat(question, stream, **kwargs)
  36. if stream:
  37. for line in res.iter_lines():
  38. line = line.decode("utf-8")
  39. if line.startswith("{"):
  40. json_data = json.loads(line)
  41. raise Exception(json_data["message"])
  42. if not line.startswith("data:"):
  43. continue
  44. json_data = json.loads(line[5:])
  45. if json_data["data"] is True or json_data["data"].get("running_status"):
  46. continue
  47. answer = json_data["data"]["answer"]
  48. reference = json_data["data"].get("reference", {})
  49. temp_dict = {
  50. "content": answer,
  51. "role": "assistant"
  52. }
  53. if reference and "chunks" in reference:
  54. chunks = reference["chunks"]
  55. temp_dict["reference"] = chunks
  56. message = Message(self.rag, temp_dict)
  57. yield message
  58. else:
  59. try:
  60. json_data = json.loads(res.text)
  61. except ValueError:
  62. raise Exception(f"Invalid response {res}")
  63. answer = json_data["data"]["answer"]
  64. reference = json_data["data"].get("reference", {})
  65. temp_dict = {
  66. "content": answer,
  67. "role": "assistant"
  68. }
  69. if reference and "chunks" in reference:
  70. chunks = reference["chunks"]
  71. temp_dict["reference"] = chunks
  72. message = Message(self.rag, temp_dict)
  73. return message
  74. def _ask_chat(self, question: str, stream: bool, **kwargs):
  75. json_data = {"question": question, "stream": stream, "session_id": self.id}
  76. json_data.update(kwargs)
  77. res = self.post(f"/chats/{self.chat_id}/completions",
  78. json_data, stream=stream)
  79. return res
  80. def _ask_agent(self, question: str, stream: bool):
  81. res = self.post(f"/agents/{self.agent_id}/completions",
  82. {"question": question, "stream": stream, "session_id": self.id}, stream=stream)
  83. return res
  84. def update(self, update_message):
  85. res = self.put(f"/chats/{self.chat_id}/sessions/{self.id}",
  86. update_message)
  87. res = res.json()
  88. if res.get("code") != 0:
  89. raise Exception(res.get("message"))
  90. class Message(Base):
  91. def __init__(self, rag, res_dict):
  92. self.content = "Hi! I am your assistant, can I help you?"
  93. self.reference = None
  94. self.role = "assistant"
  95. self.prompt = None
  96. self.id = None
  97. super().__init__(rag, res_dict)