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.0KB

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