Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. message = self._structure_answer(json_data)
  48. yield message
  49. else:
  50. try:
  51. json_data = json.loads(res.text)
  52. except ValueError:
  53. raise Exception(f"Invalid response {res}")
  54. return self._structure_answer(json_data)
  55. def _structure_answer(self, json_data):
  56. answer = json_data["data"]["answer"]
  57. reference = json_data["data"].get("reference", {})
  58. temp_dict = {
  59. "content": answer,
  60. "role": "assistant"
  61. }
  62. if reference and "chunks" in reference:
  63. chunks = reference["chunks"]
  64. temp_dict["reference"] = chunks
  65. message = Message(self.rag, temp_dict)
  66. return message
  67. def _ask_chat(self, question: str, stream: bool, **kwargs):
  68. json_data = {"question": question, "stream": stream, "session_id": self.id}
  69. json_data.update(kwargs)
  70. res = self.post(f"/chats/{self.chat_id}/completions",
  71. json_data, stream=stream)
  72. return res
  73. def _ask_agent(self, question: str, stream: bool):
  74. res = self.post(f"/agents/{self.agent_id}/completions",
  75. {"question": question, "stream": stream, "session_id": self.id}, stream=stream)
  76. return res
  77. def update(self, update_message):
  78. res = self.put(f"/chats/{self.chat_id}/sessions/{self.id}",
  79. update_message)
  80. res = res.json()
  81. if res.get("code") != 0:
  82. raise Exception(res.get("message"))
  83. class Message(Base):
  84. def __init__(self, rag, res_dict):
  85. self.content = "Hi! I am your assistant, can I help you?"
  86. self.reference = None
  87. self.role = "assistant"
  88. self.prompt = None
  89. self.id = None
  90. super().__init__(rag, res_dict)