You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

476 line
21KB

  1. # access LinkAI knowledge base platform
  2. # docs: https://link-ai.tech/platform/link-app/wechat
  3. import re
  4. import time
  5. import requests
  6. import config
  7. from bot.bot import Bot
  8. from bot.chatgpt.chat_gpt_session import ChatGPTSession
  9. from bot.gemini.google_gemini_bot import GoogleGeminiBot
  10. from bot.session_manager import SessionManager
  11. from bridge.context import Context, ContextType
  12. from bridge.reply import Reply, ReplyType
  13. from common.log import logger
  14. from config import conf, pconf
  15. import threading
  16. from common import memory, utils
  17. import base64
  18. import os
  19. class LinkAIBot(Bot):
  20. # authentication failed
  21. AUTH_FAILED_CODE = 401
  22. NO_QUOTA_CODE = 406
  23. def __init__(self):
  24. super().__init__()
  25. self.sessions = LinkAISessionManager(LinkAISession, model=conf().get("model") or "gpt-3.5-turbo")
  26. self.args = {}
  27. def reply(self, query, context: Context = None) -> Reply:
  28. if context.type == ContextType.TEXT:
  29. return self._chat(query, context)
  30. elif context.type == ContextType.IMAGE_CREATE:
  31. if not conf().get("text_to_image"):
  32. logger.warn("[LinkAI] text_to_image is not enabled, ignore the IMAGE_CREATE request")
  33. return Reply(ReplyType.TEXT, "")
  34. ok, res = self.create_img(query, 0)
  35. if ok:
  36. reply = Reply(ReplyType.IMAGE_URL, res)
  37. else:
  38. reply = Reply(ReplyType.ERROR, res)
  39. return reply
  40. else:
  41. reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type))
  42. return reply
  43. def _chat(self, query, context, retry_count=0) -> Reply:
  44. """
  45. 发起对话请求
  46. :param query: 请求提示词
  47. :param context: 对话上下文
  48. :param retry_count: 当前递归重试次数
  49. :return: 回复
  50. """
  51. if retry_count > 2:
  52. # exit from retry 2 times
  53. logger.warn("[LINKAI] failed after maximum number of retry times")
  54. return Reply(ReplyType.TEXT, "请再问我一次吧")
  55. try:
  56. # load config
  57. if context.get("generate_breaked_by"):
  58. logger.info(f"[LINKAI] won't set appcode because a plugin ({context['generate_breaked_by']}) affected the context")
  59. app_code = None
  60. else:
  61. plugin_app_code = self._find_group_mapping_code(context)
  62. app_code = context.kwargs.get("app_code") or plugin_app_code or conf().get("linkai_app_code")
  63. linkai_api_key = conf().get("linkai_api_key")
  64. session_id = context["session_id"]
  65. session_message = self.sessions.session_msg_query(query, session_id)
  66. logger.debug(f"[LinkAI] session={session_message}, session_id={session_id}")
  67. # image process
  68. img_cache = memory.USER_IMAGE_CACHE.get(session_id)
  69. if img_cache:
  70. messages = self._process_image_msg(app_code=app_code, session_id=session_id, query=query, img_cache=img_cache)
  71. if messages:
  72. session_message = messages
  73. model = conf().get("model")
  74. # remove system message
  75. if session_message[0].get("role") == "system":
  76. if app_code or model == "wenxin":
  77. session_message.pop(0)
  78. body = {
  79. "app_code": app_code,
  80. "messages": session_message,
  81. "model": model, # 对话模型的名称, 支持 gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-4, wenxin, xunfei
  82. "temperature": conf().get("temperature"),
  83. "top_p": conf().get("top_p", 1),
  84. "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  85. "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  86. "session_id": session_id,
  87. "sender_id": session_id,
  88. "channel_type": conf().get("channel_type", "wx")
  89. }
  90. try:
  91. from linkai import LinkAIClient
  92. client_id = LinkAIClient.fetch_client_id()
  93. if client_id:
  94. body["client_id"] = client_id
  95. # start: client info deliver
  96. if context.kwargs.get("msg"):
  97. body["session_id"] = context.kwargs.get("msg").from_user_id
  98. if context.kwargs.get("msg").is_group:
  99. body["is_group"] = True
  100. body["group_name"] = context.kwargs.get("msg").from_user_nickname
  101. body["sender_name"] = context.kwargs.get("msg").actual_user_nickname
  102. else:
  103. if body.get("channel_type") in ["wechatcom_app"]:
  104. body["sender_name"] = context.kwargs.get("msg").from_user_id
  105. else:
  106. body["sender_name"] = context.kwargs.get("msg").from_user_nickname
  107. except Exception as e:
  108. pass
  109. file_id = context.kwargs.get("file_id")
  110. if file_id:
  111. body["file_id"] = file_id
  112. logger.info(f"[LINKAI] query={query}, app_code={app_code}, model={body.get('model')}, file_id={file_id}")
  113. headers = {"Authorization": "Bearer " + linkai_api_key}
  114. # do http request
  115. base_url = conf().get("linkai_api_base", "https://api.link-ai.chat")
  116. res = requests.post(url=base_url + "/v1/chat/completions", json=body, headers=headers,
  117. timeout=conf().get("request_timeout", 180))
  118. if res.status_code == 200:
  119. # execute success
  120. response = res.json()
  121. reply_content = response["choices"][0]["message"]["content"]
  122. total_tokens = response["usage"]["total_tokens"]
  123. res_code = response.get('code')
  124. logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}, res_code={res_code}")
  125. if res_code == 429:
  126. logger.warn(f"[LINKAI] 用户访问超出限流配置,sender_id={body.get('sender_id')}")
  127. else:
  128. self.sessions.session_reply(reply_content, session_id, total_tokens, query=query)
  129. agent_suffix = self._fetch_agent_suffix(response)
  130. if agent_suffix:
  131. reply_content += agent_suffix
  132. if not agent_suffix:
  133. knowledge_suffix = self._fetch_knowledge_search_suffix(response)
  134. if knowledge_suffix:
  135. reply_content += knowledge_suffix
  136. # image process
  137. if response["choices"][0].get("img_urls"):
  138. thread = threading.Thread(target=self._send_image, args=(context.get("channel"), context, response["choices"][0].get("img_urls")))
  139. thread.start()
  140. if response["choices"][0].get("text_content"):
  141. reply_content = response["choices"][0].get("text_content")
  142. reply_content = self._process_url(reply_content)
  143. return Reply(ReplyType.TEXT, reply_content)
  144. else:
  145. response = res.json()
  146. error = response.get("error")
  147. logger.error(f"[LINKAI] chat failed, status_code={res.status_code}, "
  148. f"msg={error.get('message')}, type={error.get('type')}")
  149. if res.status_code >= 500:
  150. # server error, need retry
  151. time.sleep(2)
  152. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  153. return self._chat(query, context, retry_count + 1)
  154. error_reply = "提问太快啦,请休息一下再问我吧"
  155. if res.status_code == 409:
  156. error_reply = "这个问题我还没有学会,请问我其它问题吧"
  157. return Reply(ReplyType.TEXT, error_reply)
  158. except Exception as e:
  159. logger.exception(e)
  160. # retry
  161. time.sleep(2)
  162. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  163. return self._chat(query, context, retry_count + 1)
  164. def _process_image_msg(self, app_code: str, session_id: str, query:str, img_cache: dict):
  165. try:
  166. enable_image_input = False
  167. app_info = self._fetch_app_info(app_code)
  168. if not app_info:
  169. logger.debug(f"[LinkAI] not found app, can't process images, app_code={app_code}")
  170. return None
  171. plugins = app_info.get("data").get("plugins")
  172. for plugin in plugins:
  173. if plugin.get("input_type") and "IMAGE" in plugin.get("input_type"):
  174. enable_image_input = True
  175. if not enable_image_input:
  176. return
  177. msg = img_cache.get("msg")
  178. path = img_cache.get("path")
  179. msg.prepare()
  180. logger.info(f"[LinkAI] query with images, path={path}")
  181. messages = self._build_vision_msg(query, path)
  182. memory.USER_IMAGE_CACHE[session_id] = None
  183. return messages
  184. except Exception as e:
  185. logger.exception(e)
  186. def _find_group_mapping_code(self, context):
  187. try:
  188. if context.kwargs.get("isgroup"):
  189. group_name = context.kwargs.get("msg").from_user_nickname
  190. if config.plugin_config and config.plugin_config.get("linkai"):
  191. linkai_config = config.plugin_config.get("linkai")
  192. group_mapping = linkai_config.get("group_app_map")
  193. if group_mapping and group_name:
  194. return group_mapping.get(group_name)
  195. except Exception as e:
  196. logger.exception(e)
  197. return None
  198. def _build_vision_msg(self, query: str, path: str):
  199. try:
  200. suffix = utils.get_path_suffix(path)
  201. with open(path, "rb") as file:
  202. base64_str = base64.b64encode(file.read()).decode('utf-8')
  203. messages = [{
  204. "role": "user",
  205. "content": [
  206. {
  207. "type": "text",
  208. "text": query
  209. },
  210. {
  211. "type": "image_url",
  212. "image_url": {
  213. "url": f"data:image/{suffix};base64,{base64_str}"
  214. }
  215. }
  216. ]
  217. }]
  218. return messages
  219. except Exception as e:
  220. logger.exception(e)
  221. def reply_text(self, session: ChatGPTSession, app_code="", retry_count=0) -> dict:
  222. if retry_count >= 2:
  223. # exit from retry 2 times
  224. logger.warn("[LINKAI] failed after maximum number of retry times")
  225. return {
  226. "total_tokens": 0,
  227. "completion_tokens": 0,
  228. "content": "请再问我一次吧"
  229. }
  230. try:
  231. body = {
  232. "app_code": app_code,
  233. "messages": session.messages,
  234. "model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称, 支持 gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-4, wenxin, xunfei
  235. "temperature": conf().get("temperature"),
  236. "top_p": conf().get("top_p", 1),
  237. "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  238. "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
  239. }
  240. if self.args.get("max_tokens"):
  241. body["max_tokens"] = self.args.get("max_tokens")
  242. headers = {"Authorization": "Bearer " + conf().get("linkai_api_key")}
  243. # do http request
  244. base_url = conf().get("linkai_api_base", "https://api.link-ai.chat")
  245. res = requests.post(url=base_url + "/v1/chat/completions", json=body, headers=headers,
  246. timeout=conf().get("request_timeout", 180))
  247. if res.status_code == 200:
  248. # execute success
  249. response = res.json()
  250. reply_content = response["choices"][0]["message"]["content"]
  251. total_tokens = response["usage"]["total_tokens"]
  252. logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}")
  253. return {
  254. "total_tokens": total_tokens,
  255. "completion_tokens": response["usage"]["completion_tokens"],
  256. "content": reply_content,
  257. }
  258. else:
  259. response = res.json()
  260. error = response.get("error")
  261. logger.error(f"[LINKAI] chat failed, status_code={res.status_code}, "
  262. f"msg={error.get('message')}, type={error.get('type')}")
  263. if res.status_code >= 500:
  264. # server error, need retry
  265. time.sleep(2)
  266. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  267. return self.reply_text(session, app_code, retry_count + 1)
  268. return {
  269. "total_tokens": 0,
  270. "completion_tokens": 0,
  271. "content": "提问太快啦,请休息一下再问我吧"
  272. }
  273. except Exception as e:
  274. logger.exception(e)
  275. # retry
  276. time.sleep(2)
  277. logger.warn(f"[LINKAI] do retry, times={retry_count}")
  278. return self.reply_text(session, app_code, retry_count + 1)
  279. def _fetch_app_info(self, app_code: str):
  280. headers = {"Authorization": "Bearer " + conf().get("linkai_api_key")}
  281. # do http request
  282. base_url = conf().get("linkai_api_base", "https://api.link-ai.chat")
  283. params = {"app_code": app_code}
  284. res = requests.get(url=base_url + "/v1/app/info", params=params, headers=headers, timeout=(5, 10))
  285. if res.status_code == 200:
  286. return res.json()
  287. else:
  288. logger.warning(f"[LinkAI] find app info exception, res={res}")
  289. def create_img(self, query, retry_count=0, api_key=None):
  290. try:
  291. logger.info("[LinkImage] image_query={}".format(query))
  292. headers = {
  293. "Content-Type": "application/json",
  294. "Authorization": f"Bearer {conf().get('linkai_api_key')}"
  295. }
  296. data = {
  297. "prompt": query,
  298. "n": 1,
  299. "model": conf().get("text_to_image") or "dall-e-2",
  300. "response_format": "url",
  301. "img_proxy": conf().get("image_proxy")
  302. }
  303. url = conf().get("linkai_api_base", "https://api.link-ai.chat") + "/v1/images/generations"
  304. res = requests.post(url, headers=headers, json=data, timeout=(5, 90))
  305. t2 = time.time()
  306. image_url = res.json()["data"][0]["url"]
  307. logger.info("[OPEN_AI] image_url={}".format(image_url))
  308. return True, image_url
  309. except Exception as e:
  310. logger.error(format(e))
  311. return False, "画图出现问题,请休息一下再问我吧"
  312. def _fetch_knowledge_search_suffix(self, response) -> str:
  313. try:
  314. if response.get("knowledge_base"):
  315. search_hit = response.get("knowledge_base").get("search_hit")
  316. first_similarity = response.get("knowledge_base").get("first_similarity")
  317. logger.info(f"[LINKAI] knowledge base, search_hit={search_hit}, first_similarity={first_similarity}")
  318. plugin_config = pconf("linkai")
  319. if plugin_config and plugin_config.get("knowledge_base") and plugin_config.get("knowledge_base").get("search_miss_text_enabled"):
  320. search_miss_similarity = plugin_config.get("knowledge_base").get("search_miss_similarity")
  321. search_miss_text = plugin_config.get("knowledge_base").get("search_miss_suffix")
  322. if not search_hit:
  323. return search_miss_text
  324. if search_miss_similarity and float(search_miss_similarity) > first_similarity:
  325. return search_miss_text
  326. except Exception as e:
  327. logger.exception(e)
  328. def _fetch_agent_suffix(self, response):
  329. try:
  330. plugin_list = []
  331. logger.debug(f"[LinkAgent] res={response}")
  332. if response.get("agent") and response.get("agent").get("chain") and response.get("agent").get("need_show_plugin"):
  333. chain = response.get("agent").get("chain")
  334. suffix = "\n\n- - - - - - - - - - - -"
  335. i = 0
  336. for turn in chain:
  337. plugin_name = turn.get('plugin_name')
  338. suffix += "\n"
  339. need_show_thought = response.get("agent").get("need_show_thought")
  340. if turn.get("thought") and plugin_name and need_show_thought:
  341. suffix += f"{turn.get('thought')}\n"
  342. if plugin_name:
  343. plugin_list.append(turn.get('plugin_name'))
  344. if turn.get('plugin_icon'):
  345. suffix += f"{turn.get('plugin_icon')} "
  346. suffix += f"{turn.get('plugin_name')}"
  347. if turn.get('plugin_input'):
  348. suffix += f":{turn.get('plugin_input')}"
  349. if i < len(chain) - 1:
  350. suffix += "\n"
  351. i += 1
  352. logger.info(f"[LinkAgent] use plugins: {plugin_list}")
  353. return suffix
  354. except Exception as e:
  355. logger.exception(e)
  356. def _process_url(self, text):
  357. try:
  358. url_pattern = re.compile(r'\[(.*?)\]\((http[s]?://.*?)\)')
  359. def replace_markdown_url(match):
  360. return f"{match.group(2)}"
  361. return url_pattern.sub(replace_markdown_url, text)
  362. except Exception as e:
  363. logger.error(e)
  364. def _send_image(self, channel, context, image_urls):
  365. if not image_urls:
  366. return
  367. max_send_num = conf().get("max_media_send_count")
  368. send_interval = conf().get("media_send_interval")
  369. try:
  370. i = 0
  371. for url in image_urls:
  372. if max_send_num and i >= max_send_num:
  373. continue
  374. i += 1
  375. if url.endswith(".mp4"):
  376. reply_type = ReplyType.VIDEO_URL
  377. elif url.endswith(".pdf") or url.endswith(".doc") or url.endswith(".docx") or url.endswith(".csv"):
  378. reply_type = ReplyType.FILE
  379. url = _download_file(url)
  380. if not url:
  381. continue
  382. else:
  383. reply_type = ReplyType.IMAGE_URL
  384. reply = Reply(reply_type, url)
  385. channel.send(reply, context)
  386. if send_interval:
  387. time.sleep(send_interval)
  388. except Exception as e:
  389. logger.error(e)
  390. def _download_file(url: str):
  391. try:
  392. file_path = "tmp"
  393. if not os.path.exists(file_path):
  394. os.makedirs(file_path)
  395. file_name = url.split("/")[-1] # 获取文件名
  396. file_path = os.path.join(file_path, file_name)
  397. response = requests.get(url)
  398. with open(file_path, "wb") as f:
  399. f.write(response.content)
  400. return file_path
  401. except Exception as e:
  402. logger.warn(e)
  403. class LinkAISessionManager(SessionManager):
  404. def session_msg_query(self, query, session_id):
  405. session = self.build_session(session_id)
  406. messages = session.messages + [{"role": "user", "content": query}]
  407. return messages
  408. def session_reply(self, reply, session_id, total_tokens=None, query=None):
  409. session = self.build_session(session_id)
  410. if query:
  411. session.add_query(query)
  412. session.add_reply(reply)
  413. try:
  414. max_tokens = conf().get("conversation_max_tokens", 2500)
  415. tokens_cnt = session.discard_exceeding(max_tokens, total_tokens)
  416. logger.debug(f"[LinkAI] chat history, before tokens={total_tokens}, now tokens={tokens_cnt}")
  417. except Exception as e:
  418. logger.warning("Exception when counting tokens precisely for session: {}".format(str(e)))
  419. return session
  420. class LinkAISession(ChatGPTSession):
  421. def calc_tokens(self):
  422. if not self.messages:
  423. return 0
  424. return len(str(self.messages))
  425. def discard_exceeding(self, max_tokens, cur_tokens=None):
  426. cur_tokens = self.calc_tokens()
  427. if cur_tokens > max_tokens:
  428. for i in range(0, len(self.messages)):
  429. if i > 0 and self.messages[i].get("role") == "assistant" and self.messages[i - 1].get("role") == "user":
  430. self.messages.pop(i)
  431. self.messages.pop(i - 1)
  432. return self.calc_tokens()
  433. return cur_tokens