選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

link_ai_bot.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # access LinkAI knowledge base platform
  2. # docs: https://link-ai.tech/platform/link-app/wechat
  3. from bot.bot import Bot
  4. from bridge.reply import Reply, ReplyType
  5. from common.log import logger
  6. from bridge.context import Context
  7. from bot.chatgpt.chat_gpt_session import ChatGPTSession
  8. from bot.session_manager import SessionManager
  9. from config import conf
  10. import requests
  11. import time
  12. class LinkAIBot(Bot):
  13. # authentication failed
  14. AUTH_FAILED_CODE = 401
  15. NO_QUOTA_CODE = 406
  16. def __init__(self):
  17. self.base_url = "https://api.link-ai.chat/v1"
  18. self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "gpt-3.5-turbo")
  19. def reply(self, query, context: Context = None) -> Reply:
  20. return self._chat(query, context)
  21. def _chat(self, query, context, retry_count=0):
  22. if retry_count >= 2:
  23. # exit from retry 2 times
  24. logger.warn("[LINKAI] failed after maximum number of retry times")
  25. return Reply(ReplyType.ERROR, "请再问我一次吧")
  26. try:
  27. session_id = context["session_id"]
  28. session = self.sessions.session_query(query, session_id)
  29. # remove system message
  30. if session.messages[0].get("role") == "system":
  31. session.messages.pop(0)
  32. # load config
  33. app_code = conf().get("linkai_app_code")
  34. linkai_api_key = conf().get("linkai_api_key")
  35. logger.info(f"[LINKAI] query={query}, app_code={app_code}")
  36. body = {
  37. "appCode": app_code,
  38. "messages": session.messages
  39. }
  40. headers = {"Authorization": "Bearer " + linkai_api_key}
  41. # do http request
  42. res = requests.post(url=self.base_url + "/chat/completion", json=body, headers=headers).json()
  43. if not res or not res["success"]:
  44. if res.get("code") == self.AUTH_FAILED_CODE:
  45. logger.exception(f"[LINKAI] please check your linkai_api_key, res={res}")
  46. return Reply(ReplyType.ERROR, "请再问我一次吧")
  47. elif res.get("code") == self.NO_QUOTA_CODE:
  48. logger.exception(f"[LINKAI] please check your account quota, https://link-ai.chat/console/account")
  49. return Reply(ReplyType.ERROR, "提问太快啦,请休息一下再问我吧")
  50. else:
  51. # retry
  52. time.sleep(2)
  53. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  54. return self._chat(query, context, retry_count + 1)
  55. # execute success
  56. reply_content = res["data"]["content"]
  57. logger.info(f"[LINKAI] reply={reply_content}")
  58. self.sessions.session_reply(reply_content, session_id)
  59. return Reply(ReplyType.TEXT, reply_content)
  60. except Exception as e:
  61. logger.exception(e)
  62. # retry
  63. time.sleep(2)
  64. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  65. return self._chat(query, context, retry_count + 1)