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

69 行
2.7KB

  1. from bot.session_manager import Session
  2. from common.log import logger
  3. class OpenAISession(Session):
  4. def __init__(self, session_id, system_prompt=None, model= "text-davinci-003"):
  5. super().__init__(session_id, system_prompt)
  6. self.model = model
  7. self.reset()
  8. def __str__(self):
  9. # 构造对话模型的输入
  10. '''
  11. e.g. Q: xxx
  12. A: xxx
  13. Q: xxx
  14. '''
  15. prompt = ""
  16. for item in self.messages:
  17. if item['role'] == 'system':
  18. prompt += item['content'] + "<|endoftext|>\n\n\n"
  19. elif item['role'] == 'user':
  20. prompt += "Q: " + item['content'] + "\n"
  21. elif item['role'] == 'assistant':
  22. prompt += "\n\nA: " + item['content'] + "<|endoftext|>\n"
  23. if len(self.messages) > 0 and self.messages[-1]['role'] == 'user':
  24. prompt += "A: "
  25. return prompt
  26. def discard_exceeding(self, max_tokens, cur_tokens= None):
  27. precise = True
  28. try:
  29. cur_tokens = self.calc_tokens()
  30. except Exception as e:
  31. precise = False
  32. if cur_tokens is None:
  33. raise e
  34. logger.debug("Exception when counting tokens precisely for query: {}".format(e))
  35. while cur_tokens > max_tokens:
  36. if len(self.messages) > 1:
  37. self.messages.pop(0)
  38. elif len(self.messages) == 1 and self.messages[0]["role"] == "assistant":
  39. self.messages.pop(0)
  40. if precise:
  41. cur_tokens = self.calc_tokens()
  42. else:
  43. cur_tokens = len(str(self))
  44. break
  45. elif len(self.messages) == 1 and self.messages[0]["role"] == "user":
  46. logger.warn("user question exceed max_tokens. total_tokens={}".format(cur_tokens))
  47. break
  48. else:
  49. logger.debug("max_tokens={}, total_tokens={}, len(conversation)={}".format(max_tokens, cur_tokens, len(self.messages)))
  50. break
  51. if precise:
  52. cur_tokens = self.calc_tokens()
  53. else:
  54. cur_tokens = len(str(self))
  55. return cur_tokens
  56. def calc_tokens(self):
  57. return num_tokens_from_string(str(self), self.model)
  58. # refer to https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
  59. def num_tokens_from_string(string: str, model: str) -> int:
  60. """Returns the number of tokens in a text string."""
  61. import tiktoken
  62. encoding = tiktoken.encoding_for_model(model)
  63. num_tokens = len(encoding.encode(string,disallowed_special=()))
  64. return num_tokens