Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

session_manager.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if system_prompt is None:
  8. self.system_prompt = conf().get("character_desc", "")
  9. else:
  10. self.system_prompt = system_prompt
  11. # 重置会话
  12. def reset(self):
  13. raise NotImplementedError
  14. def set_system_prompt(self, system_prompt):
  15. self.system_prompt = system_prompt
  16. self.reset()
  17. def add_query(self, query):
  18. raise NotImplementedError
  19. def add_reply(self, reply):
  20. raise NotImplementedError
  21. def discard_exceeding(self, max_tokens=None, cur_tokens=None):
  22. raise NotImplementedError
  23. class SessionManager(object):
  24. def __init__(self, sessioncls, **session_args):
  25. if conf().get('expires_in_seconds'):
  26. sessions = ExpiredDict(conf().get('expires_in_seconds'))
  27. else:
  28. sessions = dict()
  29. self.sessions = sessions
  30. self.sessioncls = sessioncls
  31. self.session_args = session_args
  32. def build_session(self, session_id, system_prompt=None):
  33. '''
  34. 如果session_id不在sessions中,创建一个新的session并添加到sessions中
  35. 如果system_prompt不会空,会更新session的system_prompt并重置session
  36. '''
  37. if session_id not in self.sessions:
  38. self.sessions[session_id] = self.sessioncls(session_id, system_prompt, **self.session_args)
  39. elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session
  40. self.sessions[session_id].set_system_prompt(system_prompt)
  41. session = self.sessions[session_id]
  42. return session
  43. def session_query(self, query, session_id):
  44. session = self.build_session(session_id)
  45. session.add_query(query)
  46. try:
  47. max_tokens = conf().get("conversation_max_tokens", 1000)
  48. total_tokens = session.discard_exceeding(max_tokens, None)
  49. logger.debug("prompt tokens used={}".format(total_tokens))
  50. except Exception as e:
  51. logger.debug("Exception when counting tokens precisely for prompt: {}".format(str(e)))
  52. return session
  53. def session_reply(self, reply, session_id, total_tokens = None):
  54. session = self.build_session(session_id)
  55. session.add_reply(reply)
  56. try:
  57. max_tokens = conf().get("conversation_max_tokens", 1000)
  58. tokens_cnt = session.discard_exceeding(max_tokens, total_tokens)
  59. logger.debug("raw total_tokens={}, savesession tokens={}".format(total_tokens, tokens_cnt))
  60. except Exception as e:
  61. logger.debug("Exception when counting tokens precisely for session: {}".format(str(e)))
  62. return session
  63. def clear_session(self, session_id):
  64. if session_id in self.sessions:
  65. del(self.sessions[session_id])
  66. def clear_all_session(self):
  67. self.sessions.clear()