mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
feat: 支持使用文件存放情景预设 (#167)
This commit is contained in:
parent
48be080fe0
commit
5ada507c2b
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -8,3 +8,4 @@ banlist.py
|
|||
plugins/
|
||||
!plugins/__init__.py
|
||||
/revcfg.py
|
||||
prompts/
|
6
main.py
6
main.py
|
@ -82,6 +82,12 @@ def reset_logging():
|
|||
def main(first_time_init=False):
|
||||
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
|
||||
try:
|
||||
# 导入config.py
|
||||
|
|
|
@ -2,17 +2,39 @@
|
|||
|
||||
__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:
|
||||
"""获取预设值字典"""
|
||||
import config
|
||||
default_prompt = config.default_prompt
|
||||
if type(default_prompt) == str:
|
||||
return {"default": default_prompt}
|
||||
default_prompt = {"default": default_prompt}
|
||||
elif type(default_prompt) == dict:
|
||||
return default_prompt
|
||||
pass
|
||||
else:
|
||||
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):
|
||||
global __current__
|
||||
|
@ -48,4 +70,7 @@ def get_prompt(name: str = None) -> str:
|
|||
for key in default_dict:
|
||||
if key.lower().startswith(name.lower()):
|
||||
return default_dict[key]
|
||||
|
||||
raise KeyError("未找到情景预设: " + name)
|
||||
|
||||
read_prompt_from_file()
|
||||
|
|
Loading…
Reference in New Issue
Block a user