You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

session.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. for line in res.iter_lines():
  37. line = line.decode("utf-8")
  38. if line.startswith("{"):
  39. json_data = json.loads(line)
  40. raise Exception(json_data["message"])
  41. if not line.startswith("data:"):
  42. continue
  43. json_data = json.loads(line[5:])
  44. if json_data["data"] is True or json_data["data"].get("running_status"):
  45. continue
  46. answer = json_data["data"]["answer"]
  47. reference = json_data["data"].get("reference", {})
  48. temp_dict = {
  49. "content": answer,
  50. "role": "assistant"
  51. }
  52. if reference and "chunks" in reference:
  53. chunks = reference["chunks"]
  54. temp_dict["reference"] = chunks
  55. message = Message(self.rag, temp_dict)
  56. yield message
  57. def _ask_chat(self, question: str, stream: bool, **kwargs):
  58. json_data = {"question": question, "stream": True, "session_id": self.id}
  59. json_data.update(kwargs)
  60. res = self.post(f"/chats/{self.chat_id}/completions",
  61. json_data, stream=stream)
  62. return res
  63. def _ask_agent(self, question: str, stream: bool):
  64. res = self.post(f"/agents/{self.agent_id}/completions",
  65. {"question": question, "stream": True, "session_id": self.id}, stream=stream)
  66. return res
  67. def update(self, update_message):
  68. res = self.put(f"/chats/{self.chat_id}/sessions/{self.id}",
  69. update_message)
  70. res = res.json()
  71. if res.get("code") != 0:
  72. raise Exception(res.get("message"))
  73. class Message(Base):
  74. def __init__(self, rag, res_dict):
  75. self.content = "Hi! I am your assistant,can I help you?"
  76. self.reference = None
  77. self.role = "assistant"
  78. self.prompt = None
  79. self.id = None
  80. super().__init__(rag, res_dict)