您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

170 行
6.3KB

  1. # encoding:utf-8
  2. from bot.bot import Bot
  3. from bridge.context import ContextType
  4. from bridge.reply import Reply, ReplyType
  5. from config import conf
  6. from common.log import logger
  7. import openai
  8. import time
  9. user_session = dict()
  10. # OpenAI对话模型API (可用)
  11. class OpenAIBot(Bot):
  12. def __init__(self):
  13. openai.api_key = conf().get('open_ai_api_key')
  14. def reply(self, query, context=None):
  15. # acquire reply content
  16. if context and context.type:
  17. if context.type == ContextType.TEXT:
  18. logger.info("[OPEN_AI] query={}".format(query))
  19. from_user_id = context['session_id']
  20. reply = None
  21. if query == '#清除记忆':
  22. Session.clear_session(from_user_id)
  23. reply = Reply(ReplyType.INFO, '记忆已清除')
  24. elif query == '#清除所有':
  25. Session.clear_all_session()
  26. reply = Reply(ReplyType.INFO, '所有人记忆已清除')
  27. else:
  28. new_query = Session.build_session_query(query, from_user_id)
  29. logger.debug("[OPEN_AI] session query={}".format(new_query))
  30. reply_content = self.reply_text(new_query, from_user_id, 0)
  31. logger.debug("[OPEN_AI] new_query={}, user={}, reply_cont={}".format(new_query, from_user_id, reply_content))
  32. if reply_content and query:
  33. Session.save_session(query, reply_content, from_user_id)
  34. reply = Reply(ReplyType.TEXT, reply_content)
  35. return reply
  36. elif context.type == ContextType.IMAGE_CREATE:
  37. return self.create_img(query, 0)
  38. def reply_text(self, query, user_id, retry_count=0):
  39. try:
  40. response = openai.Completion.create(
  41. model="text-davinci-003", # 对话模型的名称
  42. prompt=query,
  43. temperature=0.9, # 值在[0,1]之间,越大表示回复越具有不确定性
  44. max_tokens=1200, # 回复最大的字符数
  45. top_p=1,
  46. frequency_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容
  47. presence_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容
  48. stop=["\n\n\n"]
  49. )
  50. res_content = response.choices[0]['text'].strip().replace('<|endoftext|>', '')
  51. logger.info("[OPEN_AI] reply={}".format(res_content))
  52. return res_content
  53. except openai.error.RateLimitError as e:
  54. # rate limit exception
  55. logger.warn(e)
  56. if retry_count < 1:
  57. time.sleep(5)
  58. logger.warn("[OPEN_AI] RateLimit exceed, 第{}次重试".format(retry_count+1))
  59. return self.reply_text(query, user_id, retry_count+1)
  60. else:
  61. return "提问太快啦,请休息一下再问我吧"
  62. except Exception as e:
  63. # unknown exception
  64. logger.exception(e)
  65. Session.clear_session(user_id)
  66. return "请再问我一次吧"
  67. def create_img(self, query, retry_count=0):
  68. try:
  69. logger.info("[OPEN_AI] image_query={}".format(query))
  70. response = openai.Image.create(
  71. prompt=query, #图片描述
  72. n=1, #每次生成图片的数量
  73. size="256x256" #图片大小,可选有 256x256, 512x512, 1024x1024
  74. )
  75. image_url = response['data'][0]['url']
  76. logger.info("[OPEN_AI] image_url={}".format(image_url))
  77. return image_url
  78. except openai.error.RateLimitError as e:
  79. logger.warn(e)
  80. if retry_count < 1:
  81. time.sleep(5)
  82. logger.warn("[OPEN_AI] ImgCreate RateLimit exceed, 第{}次重试".format(retry_count+1))
  83. return self.reply_text(query, retry_count+1)
  84. else:
  85. return "提问太快啦,请休息一下再问我吧"
  86. except Exception as e:
  87. logger.exception(e)
  88. return None
  89. class Session(object):
  90. @staticmethod
  91. def build_session_query(query, user_id):
  92. '''
  93. build query with conversation history
  94. e.g. Q: xxx
  95. A: xxx
  96. Q: xxx
  97. :param query: query content
  98. :param user_id: from user id
  99. :return: query content with conversaction
  100. '''
  101. prompt = conf().get("character_desc", "")
  102. if prompt:
  103. prompt += "<|endoftext|>\n\n\n"
  104. session = user_session.get(user_id, None)
  105. if session:
  106. for conversation in session:
  107. prompt += "Q: " + conversation["question"] + "\n\n\nA: " + conversation["answer"] + "<|endoftext|>\n"
  108. prompt += "Q: " + query + "\nA: "
  109. return prompt
  110. else:
  111. return prompt + "Q: " + query + "\nA: "
  112. @staticmethod
  113. def save_session(query, answer, user_id):
  114. max_tokens = conf().get("conversation_max_tokens")
  115. if not max_tokens:
  116. # default 3000
  117. max_tokens = 1000
  118. conversation = dict()
  119. conversation["question"] = query
  120. conversation["answer"] = answer
  121. session = user_session.get(user_id)
  122. logger.debug(conversation)
  123. logger.debug(session)
  124. if session:
  125. # append conversation
  126. session.append(conversation)
  127. else:
  128. # create session
  129. queue = list()
  130. queue.append(conversation)
  131. user_session[user_id] = queue
  132. # discard exceed limit conversation
  133. Session.discard_exceed_conversation(user_session[user_id], max_tokens)
  134. @staticmethod
  135. def discard_exceed_conversation(session, max_tokens):
  136. count = 0
  137. count_list = list()
  138. for i in range(len(session)-1, -1, -1):
  139. # count tokens of conversation list
  140. history_conv = session[i]
  141. count += len(history_conv["question"]) + len(history_conv["answer"])
  142. count_list.append(count)
  143. for c in count_list:
  144. if c > max_tokens:
  145. # pop first conversation
  146. session.pop(0)
  147. @staticmethod
  148. def clear_session(user_id):
  149. user_session[user_id] = []
  150. @staticmethod
  151. def clear_all_session():
  152. user_session.clear()