mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-22 12:26:08 +00:00
初步追加通过json导入messages数组的方式进行情景预设
This commit is contained in:
+63
-14
@@ -4,6 +4,7 @@
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import json
|
||||
@@ -20,6 +21,8 @@ import pkg.plugin.models as plugin_models
|
||||
sessions = {}
|
||||
|
||||
|
||||
|
||||
|
||||
class SessionOfflineStatus:
|
||||
ON_GOING = 'on_going'
|
||||
EXPLICITLY_CLOSED = 'explicitly_closed'
|
||||
@@ -128,25 +131,56 @@ class Session:
|
||||
logging.debug('{},lock release successfully,{}'.format(self.name, self.response_lock))
|
||||
|
||||
# 从配置文件获取会话预设信息
|
||||
def get_default_prompt(self, use_default: str = None):
|
||||
config = pkg.utils.context.get_config()
|
||||
# get_only: 仅返回初始prompt, 不更改该session的bot_name和bot_filter
|
||||
def get_default_prompt(self, use_default: str = None, get_only = False):
|
||||
|
||||
config = pkg.utils.context.get_config()
|
||||
import pkg.openai.dprompt as dprompt
|
||||
|
||||
if use_default is None:
|
||||
current_default_prompt = dprompt.get_prompt(dprompt.get_current())
|
||||
use_default = dprompt.get_current()
|
||||
|
||||
current_default_prompt = \
|
||||
[
|
||||
{
|
||||
'role': 'user',
|
||||
'content': '如果我之后想获取帮助,请你说“输入!help获取帮助”'
|
||||
}, {
|
||||
'role': 'assistant',
|
||||
'content': 'ok'
|
||||
}
|
||||
]
|
||||
|
||||
# 根据设置进行prompt预设模式
|
||||
|
||||
if config.preset_mode == "full_scenario":
|
||||
|
||||
##
|
||||
dir = os.path.join(os.getcwd(), config.full_prompt_dir)
|
||||
json_file = os.path.join(dir, use_default) + '.json'
|
||||
|
||||
logging.info("try to load json: {}".format(json_file))
|
||||
|
||||
try:
|
||||
with open(json_file, 'r', encoding ='utf-8') as f:
|
||||
json_content = json.load(f)
|
||||
current_default_prompt = json_content['prompt']
|
||||
|
||||
if not get_only:
|
||||
self.bot_name = json_content['name'] # 读取机器人名字,用于响应信息
|
||||
self.bot_filter = json_content['filter'] # 过滤掉不符合人格的警告
|
||||
logging.debug("first bot filter: {}".format(self.bot_filter))
|
||||
|
||||
except FileNotFoundError:
|
||||
logging.info("couldn't find file {}".format(json_file))
|
||||
|
||||
# logging.info("json: {}".format(current_default_prompt))
|
||||
##
|
||||
|
||||
else:
|
||||
current_default_prompt = dprompt.get_prompt(use_default)
|
||||
|
||||
return [
|
||||
{
|
||||
'role': 'user',
|
||||
'content': current_default_prompt
|
||||
}, {
|
||||
'role': 'assistant',
|
||||
'content': 'ok'
|
||||
}
|
||||
]
|
||||
return current_default_prompt
|
||||
|
||||
def __init__(self, name: str):
|
||||
self.name = name
|
||||
@@ -155,6 +189,8 @@ class Session:
|
||||
self.schedule()
|
||||
|
||||
self.response_lock = threading.Lock()
|
||||
self.bot_name = 'ai'
|
||||
self.bot_filter = None
|
||||
self.prompt = self.get_default_prompt()
|
||||
|
||||
# 设定检查session最后一次对话是否超过过期时间的计时器
|
||||
@@ -199,7 +235,7 @@ class Session:
|
||||
self.last_interact_timestamp = int(time.time())
|
||||
|
||||
# 触发插件事件
|
||||
if self.prompt == self.get_default_prompt():
|
||||
if self.prompt == self.get_default_prompt(get_only=True):
|
||||
args = {
|
||||
'session_name': self.name,
|
||||
'session': self,
|
||||
@@ -228,10 +264,23 @@ class Session:
|
||||
del (res_ans_spt[0])
|
||||
res_ans = '\n\n'.join(res_ans_spt)
|
||||
|
||||
#检测是否包含ai人格否定
|
||||
logging.debug('bot_filter: {}'.format(self.bot_filter))
|
||||
if config.filter_ai_warning and self.bot_filter:
|
||||
import re
|
||||
match = re.search(self.bot_filter['reg'], res_ans)
|
||||
logging.debug(self.bot_filter)
|
||||
logging.debug(res_ans)
|
||||
if match:
|
||||
logging.debug('回复:{}, 检测到人格否定,替换中。。'.format(res_ans))
|
||||
res_ans = self.bot_filter['replace']
|
||||
logging.debug('替换为: {}'.format(res_ans))
|
||||
|
||||
# 将此次对话的双方内容加入到prompt中
|
||||
self.prompt.append({'role': 'user', 'content': text})
|
||||
self.prompt.append({'role': 'assistant', 'content': res_ans})
|
||||
|
||||
|
||||
if self.just_switched_to_exist_session:
|
||||
self.just_switched_to_exist_session = False
|
||||
self.set_ongoing()
|
||||
@@ -280,7 +329,7 @@ class Session:
|
||||
|
||||
# 持久化session
|
||||
def persistence(self):
|
||||
if self.prompt == self.get_default_prompt():
|
||||
if self.prompt == self.get_default_prompt(get_only=True):
|
||||
return
|
||||
|
||||
db_inst = pkg.utils.context.get_database_manager()
|
||||
|
||||
Reference in New Issue
Block a user