mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-17 18:06:06 +00:00
feat: 支持使用文件存放情景预设 (#167)
This commit is contained in:
@@ -8,3 +8,4 @@ banlist.py
|
|||||||
plugins/
|
plugins/
|
||||||
!plugins/__init__.py
|
!plugins/__init__.py
|
||||||
/revcfg.py
|
/revcfg.py
|
||||||
|
prompts/
|
||||||
@@ -82,6 +82,12 @@ def reset_logging():
|
|||||||
def main(first_time_init=False):
|
def main(first_time_init=False):
|
||||||
global known_exception_caught
|
global known_exception_caught
|
||||||
|
|
||||||
|
# 检查并创建plugins、prompts目录
|
||||||
|
check_path = ["plugins", "prompts"]
|
||||||
|
for path in check_path:
|
||||||
|
if not os.path.exists(path):
|
||||||
|
os.mkdir(path)
|
||||||
|
|
||||||
known_exception_caught = False
|
known_exception_caught = False
|
||||||
try:
|
try:
|
||||||
# 导入config.py
|
# 导入config.py
|
||||||
|
|||||||
+27
-2
@@ -2,17 +2,39 @@
|
|||||||
|
|
||||||
__current__ = "default"
|
__current__ = "default"
|
||||||
|
|
||||||
|
__prompts_from_files__ = {}
|
||||||
|
|
||||||
|
|
||||||
|
def read_prompt_from_file() -> str:
|
||||||
|
"""从文件读取预设值"""
|
||||||
|
# 读取prompts/目录下的所有文件,以文件名为键,文件内容为值
|
||||||
|
# 保存在__prompts_from_files__中
|
||||||
|
global __prompts_from_files__
|
||||||
|
import os
|
||||||
|
|
||||||
|
__prompts_from_files__ = {}
|
||||||
|
for file in os.listdir("prompts"):
|
||||||
|
with open(os.path.join("prompts", file), encoding="utf-8") as f:
|
||||||
|
__prompts_from_files__[file] = f.read()
|
||||||
|
|
||||||
|
|
||||||
def get_prompt_dict() -> dict:
|
def get_prompt_dict() -> dict:
|
||||||
"""获取预设值字典"""
|
"""获取预设值字典"""
|
||||||
import config
|
import config
|
||||||
default_prompt = config.default_prompt
|
default_prompt = config.default_prompt
|
||||||
if type(default_prompt) == str:
|
if type(default_prompt) == str:
|
||||||
return {"default": default_prompt}
|
default_prompt = {"default": default_prompt}
|
||||||
elif type(default_prompt) == dict:
|
elif type(default_prompt) == dict:
|
||||||
return default_prompt
|
pass
|
||||||
else:
|
else:
|
||||||
raise TypeError("default_prompt must be str or dict")
|
raise TypeError("default_prompt must be str or dict")
|
||||||
|
|
||||||
|
# 将文件中的预设值合并到default_prompt中
|
||||||
|
for key in __prompts_from_files__:
|
||||||
|
default_prompt[key] = __prompts_from_files__[key]
|
||||||
|
|
||||||
|
return default_prompt
|
||||||
|
|
||||||
|
|
||||||
def set_current(name):
|
def set_current(name):
|
||||||
global __current__
|
global __current__
|
||||||
@@ -48,4 +70,7 @@ def get_prompt(name: str = None) -> str:
|
|||||||
for key in default_dict:
|
for key in default_dict:
|
||||||
if key.lower().startswith(name.lower()):
|
if key.lower().startswith(name.lower()):
|
||||||
return default_dict[key]
|
return default_dict[key]
|
||||||
|
|
||||||
raise KeyError("未找到情景预设: " + name)
|
raise KeyError("未找到情景预设: " + name)
|
||||||
|
|
||||||
|
read_prompt_from_file()
|
||||||
|
|||||||
Reference in New Issue
Block a user