From a2160d135e1d2ee1221f00e0227e61603e6b8166 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Sat, 30 Sep 2023 15:14:42 +0800 Subject: [PATCH] feat: knowledge base miss prefix --- bot/linkai/link_ai_bot.py | 23 ++++++++++++++++++++++- plugins/godcmd/godcmd.py | 1 + plugins/plugin.py | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 4e3b622..775c2e1 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -12,7 +12,7 @@ from bot.session_manager import SessionManager from bridge.context import Context, ContextType from bridge.reply import Reply, ReplyType from common.log import logger -from config import conf +from config import conf, pconf class LinkAIBot(Bot, OpenAIImage): @@ -94,6 +94,9 @@ 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 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) @@ -183,3 +186,21 @@ class LinkAIBot(Bot, OpenAIImage): time.sleep(2) logger.warn(f"[LINKAI] do retry, times={retry_count}") return self.reply_text(session, app_code, retry_count + 1) + + + def _fecth_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"): + search_miss_similarity = plugin_config.get("knowledge_base").get("search_miss_similarity") + search_miss_text = plugin_config.get("knowledge_base").get("search_miss_text") + if not search_hit: + return search_miss_text + if search_miss_similarity and float(search_miss_similarity) > first_similarity: + return search_miss_text + except Exception as e: + logger.exception(e) diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index 88428ad..2c354ff 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -203,6 +203,7 @@ class Godcmd(Plugin): self.password = gconf["password"] self.admin_users = gconf["admin_users"] # 预存的管理员账号,这些账号不需要认证。itchat的用户名每次都会变,不可用 + global_config["admin_users"] = self.admin_users self.isrunning = True # 机器人是否运行中 self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context diff --git a/plugins/plugin.py b/plugins/plugin.py index 9e2a92e..801997b 100644 --- a/plugins/plugin.py +++ b/plugins/plugin.py @@ -21,6 +21,9 @@ class Plugin: if os.path.exists(plugin_config_path): with open(plugin_config_path, "r", encoding="utf-8") as f: plugin_conf = json.load(f) + + # 写入全局配置内存 + plugin_config[self.name] = plugin_conf logger.debug(f"loading plugin config, plugin_name={self.name}, conf={plugin_conf}") return plugin_conf