From 701daedf4975909c6582e0f711bd480b3b2c84a0 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 13 Oct 2023 15:36:20 +0800 Subject: [PATCH 1/2] feat: multi agent plugin --- bot/linkai/link_ai_bot.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 18898aa..2369e84 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -94,9 +94,13 @@ class LinkAIBot(Bot, OpenAIImage): response = res.json() reply_content = response["choices"][0]["message"]["content"] total_tokens = response["usage"]["total_tokens"] - suffix = self._fecth_knowledge_search_suffix(response) - if suffix: - reply_content += suffix + agent_suffix = self._fetch_agent_suffix(response) + if agent_suffix: + reply_content += agent_suffix + if not agent_suffix: + knowledge_suffix = self._fetch_knowledge_search_suffix(response) + if knowledge_suffix: + reply_content += knowledge_suffix logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}") self.sessions.session_reply(reply_content, session_id, total_tokens) return Reply(ReplyType.TEXT, reply_content) @@ -188,14 +192,14 @@ class LinkAIBot(Bot, OpenAIImage): return self.reply_text(session, app_code, retry_count + 1) - def _fecth_knowledge_search_suffix(self, response) -> str: + def _fetch_knowledge_search_suffix(self, response) -> str: try: if response.get("knowledge_base"): search_hit = response.get("knowledge_base").get("search_hit") first_similarity = response.get("knowledge_base").get("first_similarity") logger.info(f"[LINKAI] knowledge base, search_hit={search_hit}, first_similarity={first_similarity}") plugin_config = pconf("linkai") - if plugin_config.get("knowledge_base") and plugin_config.get("knowledge_base").get("search_miss_text_enabled"): + if plugin_config and plugin_config.get("knowledge_base") and plugin_config.get("knowledge_base").get("search_miss_text_enabled"): search_miss_similarity = plugin_config.get("knowledge_base").get("search_miss_similarity") search_miss_text = plugin_config.get("knowledge_base").get("search_miss_suffix") if not search_hit: @@ -204,3 +208,20 @@ class LinkAIBot(Bot, OpenAIImage): return search_miss_text except Exception as e: logger.exception(e) + + def _fetch_agent_suffix(self, response): + try: + if response.get("agent") and response.get("agent").get("chain"): + chain = response.get("agent").get("chain") + suffix = "\n\n---------\n🧠思考过程" + for turn in chain: + suffix += "\n\n" + if turn.get("thought"): + suffix += f"{turn.get('thought')}" + if turn.get('plugin_name'): + suffix += f"\n{turn.get('plugin_icon')} 使用 {turn.get('plugin_name')}" + if turn.get('plugin_input'): + suffix += f",输入 {turn.get('plugin_input')}" + return suffix + except Exception as e: + logger.exception(e) From 34e06fcbf83bc0b862a9a983eb1ae45f116c2d65 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 27 Oct 2023 12:28:34 +0800 Subject: [PATCH 2/2] feat: show thought and plugin in agent process --- bot/linkai/link_ai_bot.py | 26 ++++++++++++++++++-------- plugins/linkai/config.json.template | 5 ----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 2369e84..0593495 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -211,17 +211,27 @@ class LinkAIBot(Bot, OpenAIImage): def _fetch_agent_suffix(self, response): try: - if response.get("agent") and response.get("agent").get("chain"): + plugin_list = [] + logger.debug(f"[LinkAgent] res={response}") + if response.get("agent") and response.get("agent").get("chain") and response.get("agent").get("need_show_plugin"): chain = response.get("agent").get("chain") - suffix = "\n\n---------\n🧠思考过程" + suffix = "\n\n- - - - - - - - - - - -" + i = 0 for turn in chain: - suffix += "\n\n" - if turn.get("thought"): - suffix += f"{turn.get('thought')}" - if turn.get('plugin_name'): - suffix += f"\n{turn.get('plugin_icon')} 使用 {turn.get('plugin_name')}" + plugin_name = turn.get('plugin_name') + suffix += "\n" + need_show_thought = response.get("agent").get("need_show_thought") + if turn.get("thought") and plugin_name and need_show_thought: + suffix += f"{turn.get('thought')}\n" + if plugin_name: + plugin_list.append(turn.get('plugin_name')) + suffix += f"{turn.get('plugin_icon')} {turn.get('plugin_name')}" if turn.get('plugin_input'): - suffix += f",输入 {turn.get('plugin_input')}" + suffix += f":{turn.get('plugin_input')}" + if i < len(chain) - 1: + suffix += "\n" + i += 1 + logger.info(f"[LinkAgent] use plugins: {plugin_list}") return suffix except Exception as e: logger.exception(e) diff --git a/plugins/linkai/config.json.template b/plugins/linkai/config.json.template index 697d0cc..ccd8967 100644 --- a/plugins/linkai/config.json.template +++ b/plugins/linkai/config.json.template @@ -15,10 +15,5 @@ "enabled": true, "group_enabled": true, "max_file_size": 5000 - }, - "knowledge_base": { - "search_miss_text_enabled": false, - "search_miss_similarity": 0.8, - "search_miss_suffix": "\n-------------\n回复仅供参考,请以官方文档为准" } }