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.

chat.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. from .base import Base
  17. from .session import Session
  18. class Chat(Base):
  19. def __init__(self, rag, res_dict):
  20. self.id = ""
  21. self.name = "assistant"
  22. self.avatar = "path/to/avatar"
  23. self.llm = Chat.LLM(rag, {})
  24. self.prompt = Chat.Prompt(rag, {})
  25. super().__init__(rag, res_dict)
  26. class LLM(Base):
  27. def __init__(self, rag, res_dict):
  28. self.model_name = None
  29. self.temperature = 0.1
  30. self.top_p = 0.3
  31. self.presence_penalty = 0.4
  32. self.frequency_penalty = 0.7
  33. self.max_tokens = 512
  34. super().__init__(rag, res_dict)
  35. class Prompt(Base):
  36. def __init__(self, rag, res_dict):
  37. self.similarity_threshold = 0.2
  38. self.keywords_similarity_weight = 0.7
  39. self.top_n = 8
  40. self.top_k = 1024
  41. self.variables = [{"key": "knowledge", "optional": True}]
  42. self.rerank_model = ""
  43. self.empty_response = None
  44. self.opener = "Hi! I'm your assistant. What can I do for you?"
  45. self.show_quote = True
  46. self.prompt = (
  47. "You are an intelligent assistant. Please summarize the content of the knowledge base to answer the question. "
  48. "Please list the data in the knowledge base and answer in detail. When all knowledge base content is irrelevant to the question, "
  49. "your answer must include the sentence 'The answer you are looking for is not found in the knowledge base!' "
  50. "Answers need to consider chat history.\nHere is the knowledge base:\n{knowledge}\nThe above is the knowledge base."
  51. )
  52. super().__init__(rag, res_dict)
  53. def update(self, update_message: dict):
  54. res = self.put(f"/chats/{self.id}", update_message)
  55. res = res.json()
  56. if res.get("code") != 0:
  57. raise Exception(res["message"])
  58. def create_session(self, name: str = "New session") -> Session:
  59. res = self.post(f"/chats/{self.id}/sessions", {"name": name})
  60. res = res.json()
  61. if res.get("code") == 0:
  62. return Session(self.rag, res["data"])
  63. raise Exception(res["message"])
  64. def list_sessions(self, page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True, id: str = None, name: str = None) -> list[Session]:
  65. res = self.get(f"/chats/{self.id}/sessions", {"page": page, "page_size": page_size, "orderby": orderby, "desc": desc, "id": id, "name": name})
  66. res = res.json()
  67. if res.get("code") == 0:
  68. result_list = []
  69. for data in res["data"]:
  70. result_list.append(Session(self.rag, data))
  71. return result_list
  72. raise Exception(res["message"])
  73. def delete_sessions(self, ids: list[str] | None = None):
  74. res = self.rm(f"/chats/{self.id}/sessions", {"ids": ids})
  75. res = res.json()
  76. if res.get("code") != 0:
  77. raise Exception(res.get("message"))