Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

86 lignes
3.2KB

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