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.

63 lines
2.5KB

  1. from bot.session_manager import Session
  2. from common.log import logger
  3. """
  4. e.g.
  5. [
  6. {"role": "system", "content": "You are a helpful assistant."},
  7. {"role": "user", "content": "Who won the world series in 2020?"},
  8. {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
  9. {"role": "user", "content": "Where was it played?"}
  10. ]
  11. """
  12. class AliQwenSession(Session):
  13. def __init__(self, session_id, system_prompt=None, model="qianwen"):
  14. super().__init__(session_id, system_prompt)
  15. self.model = model
  16. self.reset()
  17. def discard_exceeding(self, max_tokens, cur_tokens=None):
  18. precise = True
  19. try:
  20. cur_tokens = self.calc_tokens()
  21. except Exception as e:
  22. precise = False
  23. if cur_tokens is None:
  24. raise e
  25. logger.debug("Exception when counting tokens precisely for query: {}".format(e))
  26. while cur_tokens > max_tokens:
  27. if len(self.messages) > 2:
  28. self.messages.pop(1)
  29. elif len(self.messages) == 2 and self.messages[1]["role"] == "assistant":
  30. self.messages.pop(1)
  31. if precise:
  32. cur_tokens = self.calc_tokens()
  33. else:
  34. cur_tokens = cur_tokens - max_tokens
  35. break
  36. elif len(self.messages) == 2 and self.messages[1]["role"] == "user":
  37. logger.warn("user message exceed max_tokens. total_tokens={}".format(cur_tokens))
  38. break
  39. else:
  40. logger.debug("max_tokens={}, total_tokens={}, len(messages)={}".format(max_tokens, cur_tokens, len(self.messages)))
  41. break
  42. if precise:
  43. cur_tokens = self.calc_tokens()
  44. else:
  45. cur_tokens = cur_tokens - max_tokens
  46. return cur_tokens
  47. def calc_tokens(self):
  48. return num_tokens_from_messages(self.messages, self.model)
  49. def num_tokens_from_messages(messages, model):
  50. """Returns the number of tokens used by a list of messages."""
  51. # 官方token计算规则:"对于中文文本来说,1个token通常对应一个汉字;对于英文文本来说,1个token通常对应3至4个字母或1个单词"
  52. # 详情请产看文档:https://help.aliyun.com/document_detail/2586397.html
  53. # 目前根据字符串长度粗略估计token数,不影响正常使用
  54. tokens = 0
  55. for msg in messages:
  56. tokens += len(msg["content"])
  57. return tokens