feat: knowledge base miss prefix

This commit is contained in:
zhayujie
2023-09-30 15:14:42 +08:00
parent 16d7836369
commit a2160d135e
3 changed files with 26 additions and 1 deletions

View File

@@ -12,7 +12,7 @@ from bot.session_manager import SessionManager
from bridge.context import Context, ContextType from bridge.context import Context, ContextType
from bridge.reply import Reply, ReplyType from bridge.reply import Reply, ReplyType
from common.log import logger from common.log import logger
from config import conf from config import conf, pconf
class LinkAIBot(Bot, OpenAIImage): class LinkAIBot(Bot, OpenAIImage):
@@ -94,6 +94,9 @@ class LinkAIBot(Bot, OpenAIImage):
response = res.json() response = res.json()
reply_content = response["choices"][0]["message"]["content"] reply_content = response["choices"][0]["message"]["content"]
total_tokens = response["usage"]["total_tokens"] 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}") logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}")
self.sessions.session_reply(reply_content, session_id, total_tokens) self.sessions.session_reply(reply_content, session_id, total_tokens)
return Reply(ReplyType.TEXT, reply_content) return Reply(ReplyType.TEXT, reply_content)
@@ -183,3 +186,21 @@ class LinkAIBot(Bot, OpenAIImage):
time.sleep(2) time.sleep(2)
logger.warn(f"[LINKAI] do retry, times={retry_count}") logger.warn(f"[LINKAI] do retry, times={retry_count}")
return self.reply_text(session, app_code, retry_count + 1) 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)

View File

@@ -203,6 +203,7 @@ class Godcmd(Plugin):
self.password = gconf["password"] self.password = gconf["password"]
self.admin_users = gconf["admin_users"] # 预存的管理员账号这些账号不需要认证。itchat的用户名每次都会变不可用 self.admin_users = gconf["admin_users"] # 预存的管理员账号这些账号不需要认证。itchat的用户名每次都会变不可用
global_config["admin_users"] = self.admin_users
self.isrunning = True # 机器人是否运行中 self.isrunning = True # 机器人是否运行中
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context

View File

@@ -21,6 +21,9 @@ class Plugin:
if os.path.exists(plugin_config_path): if os.path.exists(plugin_config_path):
with open(plugin_config_path, "r", encoding="utf-8") as f: with open(plugin_config_path, "r", encoding="utf-8") as f:
plugin_conf = json.load(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}") logger.debug(f"loading plugin config, plugin_name={self.name}, conf={plugin_conf}")
return plugin_conf return plugin_conf