Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

chat_gpt_bot.py 4.2KB

2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import time
  2. from bot.bot import Bot
  3. from revChatGPT.revChatGPT import Chatbot
  4. from common.log import logger
  5. config = {
  6. "Authorization": "<Your Bearer Token Here>", # This is optional
  7. "session_token": "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..yUEdyIPaNgrKerHa.hQfnBM6Ry2npNvakpj1TL_4wr7fuMeLMWOmy-yOSzJxJw7DNyq5vwKeZBwOzthFBIuSu_CpHvYCK_SvRy2RW0gtjPh1XZMxoXejzwJ8VJaVrj3BjarIJdMKRaIHrFwYlRj6fdWa_nGWeueGf1EDE71aSHf4La-4YjoEX8Ou68XHXEsOYQqMuk06u8Wa_aRq5UAj3Clc99dEw3iHU7xvf8lMmB3T1G1LMaubH21niQj-76pUzlf1Kq278Yl8Q6fOGD_CA7mCvnA1LGYzo7u0P5A8dd-p7K3Oqbxw3Gn2TMyEkzZ2q_rTqSJwnbRG87SEYp5Y6HzYyfNoqM_Ew3OGQqk9PHbv8CjKN6sR53UMNRJeFxkW2owCsR0eCvc8kL-tc5RyHWF3zWVmlOxmzDaHZo_XlA0fgEpjlMZS1ClHCBT6_ZoQRvKan0dkFfhJEdp35aK_v9DLXs46Sfs2rqfN0Fdr698gv0UbGsLdeR00W7M9qMsvXoFDBW3-GnIqsxjjepDPlv4RInMKfSeVdISp4VPWW-GjGyzCB1ooWiyZybaGul1FsdXVSibMq6qsiGUQNr08uf_In3NUPFCKNxJ2iR6A_5-TEiIIjcK6ywbI87L13PFT3oCCXiFxRPjp4f1nUUTGxLcetGzYC_eYmQD004R5M5u1epQdWen4Of1Fzn7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j9tt2tkAO814kng2fZaob8XUmjF3QKevYvY8NKAevp6cpoT7DIDZttT6mEkUy1pW02thh3FMqr_EfsCI5pR84BQCr-LIRDzhudnOxHxXOJXE_zuEt1QNNSAH0eqxA6Mx7N5p3WU7dU0ULGPMtEEXz1IwiGAJ21z8Z77EOrL3vOWUnq3Y1sAPD9PBzaPC8_wezBIXQqI6Ufa6D38xPdZnkoaxMd3PRFu91s9qMoYO4OZ1WfjbQJi51T4M76y1K73eBLGwVgWRguEs8Yqr2F-ctZ-BiSEa2RfOwYcmT5uRbFzZnQCtj32JTNMSIFGQ5It5bR0nPh5BK6LjK2_kbQny6dZ9d_KrBcl15REEKM9XhZOSGWRRwAmf_4iVsy6ceqXMuYMbEGL7xnw6tmBWzuuN21_1RnxY8JS8CtzjPC4LAIRgN7VE4M6LbEcQJiaw9hVsUzLueoP-CCtjBeqQ1ylsaz6C4rOpDISATG-jAQ66FE9P0YHXQXOAip4Bf7KO-IvSvZF_dKv2_RMdAE53dlpup5oCWqPk8qAVNgZXIT6LN8MiqRDVfObQDa-uElVtX8ll7ItOUXRoXUJfxabE6oW.bDrh_KN_-hbGsWTk_0z35g"
  8. }
  9. chatbot = Chatbot(config)
  10. user_session = dict()
  11. last_session_refresh = time.time()
  12. class ChatGPTBot(Bot):
  13. def reply(self, query, context=None):
  14. from_user_id = context['from_user_id']
  15. logger.info("[GPT]query={}, user_id={}, session={}".format(query, from_user_id, user_session))
  16. now = time.time()
  17. global last_session_refresh
  18. if now - last_session_refresh > 60 * 8:
  19. logger.info('[GPT]session refresh, now={}, last={}'.format(now, last_session_refresh))
  20. chatbot.refresh_session()
  21. last_session_refresh = now
  22. if from_user_id in user_session:
  23. if time.time() - user_session[from_user_id]['last_reply_time'] < 60 * 5:
  24. chatbot.conversation_id = user_session[from_user_id]['conversation_id']
  25. chatbot.parent_id = user_session[from_user_id]['parent_id']
  26. else:
  27. chatbot.reset_chat()
  28. else:
  29. chatbot.reset_chat()
  30. logger.info("[GPT]convId={}, parentId={}".format(chatbot.conversation_id, chatbot.parent_id))
  31. try:
  32. res = chatbot.get_chat_response(query, output="text")
  33. logger.info("[GPT]userId={}, res={}".format(from_user_id, res))
  34. user_cache = dict()
  35. user_cache['last_reply_time'] = time.time()
  36. user_cache['conversation_id'] = res['conversation_id']
  37. user_cache['parent_id'] = res['parent_id']
  38. user_session[from_user_id] = user_cache
  39. return res['message']
  40. except Exception as e:
  41. logger.error(e)
  42. return None