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.

open_ai_session.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = num_tokens_from_string(str(self), self.model)
  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 = num_tokens_from_string(str(self), self.model)
  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 = num_tokens_from_string(str(self), self.model)
  53. else:
  54. cur_tokens = len(str(self))
  55. return cur_tokens
  56. # refer to https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
  57. def num_tokens_from_string(string: str, model: str) -> int:
  58. """Returns the number of tokens in a text string."""
  59. import tiktoken
  60. encoding = tiktoken.encoding_for_model(model)
  61. num_tokens = len(encoding.encode(string,disallowed_special=()))
  62. return num_tokens