open_ai_bot.py 5.6KB

1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
1 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # encoding:utf-8
  2. import time
  3. import openai
  4. import openai.error
  5. from bot.bot import Bot
  6. from bot.openai.open_ai_image import OpenAIImage
  7. from bot.openai.open_ai_session import OpenAISession
  8. from bot.session_manager import SessionManager
  9. from bridge.context import ContextType
  10. from bridge.reply import Reply, ReplyType
  11. from common.log import logger
  12. from config import conf
  13. user_session = dict()
  14. # OpenAI对话模型API (可用)
  15. class OpenAIBot(Bot, OpenAIImage):
  16. def __init__(self):
  17. super().__init__()
  18. openai.api_key = conf().get("open_ai_api_key")
  19. if conf().get("open_ai_api_base"):
  20. openai.api_base = conf().get("open_ai_api_base")
  21. proxy = conf().get("proxy")
  22. if proxy:
  23. openai.proxy = proxy
  24. self.sessions = SessionManager(OpenAISession, model=conf().get("model") or "text-davinci-003")
  25. self.args = {
  26. "model": conf().get("model") or "text-davinci-003", # 对话模型的名称
  27. "temperature": conf().get("temperature", 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性
  28. "max_tokens": 1200, # 回复最大的字符数
  29. "top_p": 1,
  30. "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  31. "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  32. "request_timeout": conf().get("request_timeout", None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间
  33. "timeout": conf().get("request_timeout", None), # 重试超时时间,在这个时间内,将会自动重试
  34. "stop": ["\n\n\n"],
  35. }
  36. def reply(self, query, context=None):
  37. # acquire reply content
  38. if context and context.type:
  39. if context.type == ContextType.TEXT:
  40. logger.info("[OPEN_AI] query={}".format(query))
  41. session_id = context["session_id"]
  42. reply = None
  43. if query == "#清除记忆":
  44. self.sessions.clear_session(session_id)
  45. reply = Reply(ReplyType.INFO, "记忆已清除")
  46. elif query == "#清除所有":
  47. self.sessions.clear_all_session()
  48. reply = Reply(ReplyType.INFO, "所有人记忆已清除")
  49. else:
  50. session = self.sessions.session_query(query, session_id)
  51. result = self.reply_text(session)
  52. total_tokens, completion_tokens, reply_content = (
  53. result["total_tokens"],
  54. result["completion_tokens"],
  55. result["content"],
  56. )
  57. logger.debug(
  58. "[OPEN_AI] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format(str(session), session_id, reply_content, completion_tokens)
  59. )
  60. if total_tokens == 0:
  61. reply = Reply(ReplyType.ERROR, reply_content)
  62. else:
  63. self.sessions.session_reply(reply_content, session_id, total_tokens)
  64. reply = Reply(ReplyType.TEXT, reply_content)
  65. return reply
  66. elif context.type == ContextType.IMAGE_CREATE:
  67. ok, retstring = self.create_img(query, 0)
  68. reply = None
  69. if ok:
  70. reply = Reply(ReplyType.IMAGE_URL, retstring)
  71. else:
  72. reply = Reply(ReplyType.ERROR, retstring)
  73. return reply
  74. def reply_text(self, session: OpenAISession, retry_count=0):
  75. try:
  76. response = openai.Completion.create(prompt=str(session), **self.args)
  77. res_content = response.choices[0]["text"].strip().replace("<|endoftext|>", "")
  78. total_tokens = response["usage"]["total_tokens"]
  79. completion_tokens = response["usage"]["completion_tokens"]
  80. logger.info("[OPEN_AI] reply={}".format(res_content))
  81. return {
  82. "total_tokens": total_tokens,
  83. "completion_tokens": completion_tokens,
  84. "content": res_content,
  85. }
  86. except Exception as e:
  87. need_retry = retry_count < 2
  88. result = {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"}
  89. if isinstance(e, openai.error.RateLimitError):
  90. logger.warn("[OPEN_AI] RateLimitError: {}".format(e))
  91. result["content"] = "提问太快啦,请休息一下再问我吧"
  92. if need_retry:
  93. time.sleep(20)
  94. elif isinstance(e, openai.error.Timeout):
  95. logger.warn("[OPEN_AI] Timeout: {}".format(e))
  96. result["content"] = "我没有收到你的消息"
  97. if need_retry:
  98. time.sleep(5)
  99. elif isinstance(e, openai.error.APIConnectionError):
  100. logger.warn("[OPEN_AI] APIConnectionError: {}".format(e))
  101. need_retry = False
  102. result["content"] = "我连接不到你的网络"
  103. else:
  104. logger.warn("[OPEN_AI] Exception: {}".format(e))
  105. need_retry = False
  106. self.sessions.clear_session(session.session_id)
  107. if need_retry:
  108. logger.warn("[OPEN_AI] 第{}次重试".format(retry_count + 1))
  109. return self.reply_text(session, retry_count + 1)
  110. else:
  111. return result