Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

56 rindas
2.0KB

  1. import time
  2. from bot.bot import Bot
  3. from revChatGPT.revChatGPT import Chatbot
  4. from common.log import logger
  5. from config import conf
  6. user_session = dict()
  7. last_session_refresh = time.time()
  8. # ChatGPT web接口 (暂时不可用)
  9. class ChatGPTBot(Bot):
  10. def __init__(self):
  11. config = {
  12. "Authorization": "<Your Bearer Token Here>", # This is optional
  13. "session_token": conf().get("session_token")
  14. }
  15. self.chatbot = Chatbot(config)
  16. def reply(self, query, context=None):
  17. from_user_id = context['from_user_id']
  18. logger.info("[GPT]query={}, user_id={}, session={}".format(query, from_user_id, user_session))
  19. now = time.time()
  20. global last_session_refresh
  21. if now - last_session_refresh > 60 * 8:
  22. logger.info('[GPT]session refresh, now={}, last={}'.format(now, last_session_refresh))
  23. self.chatbot.refresh_session()
  24. last_session_refresh = now
  25. if from_user_id in user_session:
  26. if time.time() - user_session[from_user_id]['last_reply_time'] < 60 * 5:
  27. self.chatbot.conversation_id = user_session[from_user_id]['conversation_id']
  28. self.chatbot.parent_id = user_session[from_user_id]['parent_id']
  29. else:
  30. self.chatbot.reset_chat()
  31. else:
  32. self.chatbot.reset_chat()
  33. logger.info("[GPT]convId={}, parentId={}".format(self.chatbot.conversation_id, self.chatbot.parent_id))
  34. try:
  35. res = self.chatbot.get_chat_response(query, output="text")
  36. logger.info("[GPT]userId={}, res={}".format(from_user_id, res))
  37. user_cache = dict()
  38. user_cache['last_reply_time'] = time.time()
  39. user_cache['conversation_id'] = res['conversation_id']
  40. user_cache['parent_id'] = res['parent_id']
  41. user_session[from_user_id] = user_cache
  42. return res['message']
  43. except Exception as e:
  44. logger.error(e)
  45. return None