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

session.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if stream:
  57. yield message
  58. if not stream:
  59. return message
  60. def _ask_chat(self, question: str, stream: bool, **kwargs):
  61. json_data = {"question": question, "stream": stream, "session_id": self.id}
  62. json_data.update(kwargs)
  63. res = self.post(f"/chats/{self.chat_id}/completions",
  64. json_data, stream=stream)
  65. return res
  66. def _ask_agent(self, question: str, stream: bool):
  67. res = self.post(f"/agents/{self.agent_id}/completions",
  68. {"question": question, "stream": stream, "session_id": self.id}, stream=stream)
  69. return res
  70. def update(self, update_message):
  71. res = self.put(f"/chats/{self.chat_id}/sessions/{self.id}",
  72. update_message)
  73. res = res.json()
  74. if res.get("code") != 0:
  75. raise Exception(res.get("message"))
  76. class Message(Base):
  77. def __init__(self, rag, res_dict):
  78. self.content = "Hi! I am your assistant,can I help you?"
  79. self.reference = None
  80. self.role = "assistant"
  81. self.prompt = None
  82. self.id = None
  83. super().__init__(rag, res_dict)