選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

session_manager.py 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from common.expired_dict import ExpiredDict
  2. from common.log import logger
  3. from config import conf
  4. class Session(object):
  5. def __init__(self, session_id, system_prompt=None):
  6. self.session_id = session_id
  7. self.messages = []
  8. if system_prompt is None:
  9. self.system_prompt = conf().get("character_desc", "")
  10. else:
  11. self.system_prompt = system_prompt
  12. # 重置会话
  13. def reset(self):
  14. system_item = {"role": "system", "content": self.system_prompt}
  15. self.messages = [system_item]
  16. def set_system_prompt(self, system_prompt):
  17. self.system_prompt = system_prompt
  18. self.reset()
  19. def add_query(self, query):
  20. user_item = {"role": "user", "content": query}
  21. self.messages.append(user_item)
  22. def add_reply(self, reply):
  23. assistant_item = {"role": "assistant", "content": reply}
  24. self.messages.append(assistant_item)
  25. def discard_exceeding(self, max_tokens=None, cur_tokens=None):
  26. raise NotImplementedError
  27. def calc_tokens(self):
  28. raise NotImplementedError
  29. class SessionManager(object):
  30. def __init__(self, sessioncls, **session_args):
  31. if conf().get("expires_in_seconds"):
  32. sessions = ExpiredDict(conf().get("expires_in_seconds"))
  33. else:
  34. sessions = dict()
  35. self.sessions = sessions
  36. self.sessioncls = sessioncls
  37. self.session_args = session_args
  38. def build_session(self, session_id, system_prompt=None):
  39. """
  40. 如果session_id不在sessions中,创建一个新的session并添加到sessions中
  41. 如果system_prompt不会空,会更新session的system_prompt并重置session
  42. """
  43. if session_id is None:
  44. return self.sessioncls(session_id, system_prompt, **self.session_args)
  45. if session_id not in self.sessions:
  46. self.sessions[session_id] = self.sessioncls(session_id, system_prompt, **self.session_args)
  47. elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session
  48. self.sessions[session_id].set_system_prompt(system_prompt)
  49. session = self.sessions[session_id]
  50. return session
  51. def session_query(self, query, session_id):
  52. session = self.build_session(session_id)
  53. session.add_query(query)
  54. try:
  55. max_tokens = conf().get("conversation_max_tokens", 1000)
  56. total_tokens = session.discard_exceeding(max_tokens, None)
  57. logger.debug("prompt tokens used={}".format(total_tokens))
  58. except Exception as e:
  59. logger.warning("Exception when counting tokens precisely for prompt: {}".format(str(e)))
  60. return session
  61. def session_reply(self, reply, session_id, total_tokens=None):
  62. session = self.build_session(session_id)
  63. session.add_reply(reply)
  64. try:
  65. max_tokens = conf().get("conversation_max_tokens", 1000)
  66. tokens_cnt = session.discard_exceeding(max_tokens, total_tokens)
  67. logger.debug("raw total_tokens={}, savesession tokens={}".format(total_tokens, tokens_cnt))
  68. except Exception as e:
  69. logger.warning("Exception when counting tokens precisely for session: {}".format(str(e)))
  70. return session
  71. def clear_session(self, session_id):
  72. if session_id in self.sessions:
  73. del self.sessions[session_id]
  74. def clear_all_session(self):
  75. self.sessions.clear()