Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

link_ai_bot.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # access LinkAI knowledge base platform
  2. # docs: https://link-ai.tech/platform/link-app/wechat
  3. import time
  4. import requests
  5. from bot.bot import Bot
  6. from bot.chatgpt.chat_gpt_session import ChatGPTSession
  7. from bot.openai.open_ai_image import OpenAIImage
  8. from bot.session_manager import SessionManager
  9. from bridge.context import Context, ContextType
  10. from bridge.reply import Reply, ReplyType
  11. from common.log import logger
  12. from config import conf
  13. class LinkAIBot(Bot, OpenAIImage):
  14. # authentication failed
  15. AUTH_FAILED_CODE = 401
  16. NO_QUOTA_CODE = 406
  17. def __init__(self):
  18. super().__init__()
  19. self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "gpt-3.5-turbo")
  20. def reply(self, query, context: Context = None) -> Reply:
  21. if context.type == ContextType.TEXT:
  22. return self._chat(query, context)
  23. elif context.type == ContextType.IMAGE_CREATE:
  24. ok, res = self.create_img(query, 0)
  25. if ok:
  26. reply = Reply(ReplyType.IMAGE_URL, res)
  27. else:
  28. reply = Reply(ReplyType.ERROR, res)
  29. return reply
  30. else:
  31. reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type))
  32. return reply
  33. def _chat(self, query, context, retry_count=0) -> Reply:
  34. """
  35. 发起对话请求
  36. :param query: 请求提示词
  37. :param context: 对话上下文
  38. :param retry_count: 当前递归重试次数
  39. :return: 回复
  40. """
  41. if retry_count >= 2:
  42. # exit from retry 2 times
  43. logger.warn("[LINKAI] failed after maximum number of retry times")
  44. return Reply(ReplyType.ERROR, "请再问我一次吧")
  45. try:
  46. # load config
  47. if context.get("generate_breaked_by"):
  48. logger.info(f"[LINKAI] won't set appcode because a plugin ({context['generate_breaked_by']}) affected the context")
  49. app_code = None
  50. else:
  51. app_code = context.kwargs.get("app_code") or conf().get("linkai_app_code")
  52. linkai_api_key = conf().get("linkai_api_key")
  53. session_id = context["session_id"]
  54. session = self.sessions.session_query(query, session_id)
  55. model = conf().get("model") or "gpt-3.5-turbo"
  56. # remove system message
  57. if session.messages[0].get("role") == "system":
  58. if app_code or model == "wenxin":
  59. session.messages.pop(0)
  60. body = {
  61. "app_code": app_code,
  62. "messages": session.messages,
  63. "model": model, # 对话模型的名称, 支持 gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-4, wenxin
  64. "temperature": conf().get("temperature"),
  65. "top_p": conf().get("top_p", 1),
  66. "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  67. "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  68. }
  69. logger.info(f"[LINKAI] query={query}, app_code={app_code}, mode={body.get('model')}")
  70. headers = {"Authorization": "Bearer " + linkai_api_key}
  71. # do http request
  72. base_url = conf().get("linkai_api_base", "https://api.link-ai.chat")
  73. res = requests.post(url=base_url + "/v1/chat/completions", json=body, headers=headers,
  74. timeout=conf().get("request_timeout", 180))
  75. if res.status_code == 200:
  76. # execute success
  77. response = res.json()
  78. reply_content = response["choices"][0]["message"]["content"]
  79. total_tokens = response["usage"]["total_tokens"]
  80. logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}")
  81. self.sessions.session_reply(reply_content, session_id, total_tokens)
  82. return Reply(ReplyType.TEXT, reply_content)
  83. else:
  84. response = res.json()
  85. error = response.get("error")
  86. logger.error(f"[LINKAI] chat failed, status_code={res.status_code}, "
  87. f"msg={error.get('message')}, type={error.get('type')}")
  88. if res.status_code >= 500:
  89. # server error, need retry
  90. time.sleep(2)
  91. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  92. return self._chat(query, context, retry_count + 1)
  93. return Reply(ReplyType.ERROR, "提问太快啦,请休息一下再问我吧")
  94. except Exception as e:
  95. logger.exception(e)
  96. # retry
  97. time.sleep(2)
  98. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  99. return self._chat(query, context, retry_count + 1)