Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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