mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-10 07:46:02 +00:00
@@ -68,7 +68,7 @@ sensitive_word_filter = True
|
|||||||
encourage_sponsor_at_start = True
|
encourage_sponsor_at_start = True
|
||||||
|
|
||||||
# 每次向OpenAI接口发送对话记录上下文的字符数
|
# 每次向OpenAI接口发送对话记录上下文的字符数
|
||||||
# 最大不超过(4096 - max_tokens)个字符,max_tokens为上述completion_api_params中的max_tokens
|
# 最大不超过(4096 - max_tokens)个字符,max_tokens为下方completion_api_params中的max_tokens
|
||||||
# 注意:较大的prompt_submit_length会导致OpenAI账户额度消耗更快
|
# 注意:较大的prompt_submit_length会导致OpenAI账户额度消耗更快
|
||||||
prompt_submit_length = 1024
|
prompt_submit_length = 1024
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ prompt_submit_length = 1024
|
|||||||
completion_api_params = {
|
completion_api_params = {
|
||||||
"model": "text-davinci-003",
|
"model": "text-davinci-003",
|
||||||
"temperature": 0.9, # 数值越低得到的回答越理性,取值范围[0, 1]
|
"temperature": 0.9, # 数值越低得到的回答越理性,取值范围[0, 1]
|
||||||
"max_tokens": 512, # 每次向OpenAI请求的最大字符数, 不高于4096
|
"max_tokens": 512, # 每次获取OpenAI接口响应的文字量上限, 不高于4096
|
||||||
"top_p": 1, # 生成的文本的文本与要求的符合度, 取值范围[0, 1]
|
"top_p": 1, # 生成的文本的文本与要求的符合度, 取值范围[0, 1]
|
||||||
"frequency_penalty": 0.2,
|
"frequency_penalty": 0.2,
|
||||||
"presence_penalty": 1.0,
|
"presence_penalty": 1.0,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import traceback
|
|||||||
|
|
||||||
import pkg.utils.context as context
|
import pkg.utils.context as context
|
||||||
import pkg.plugin.switch as switch
|
import pkg.plugin.switch as switch
|
||||||
|
import pkg.plugin.settings as settings
|
||||||
|
|
||||||
from mirai import Mirai
|
from mirai import Mirai
|
||||||
|
|
||||||
@@ -35,6 +36,23 @@ __plugins__ = {}
|
|||||||
}
|
}
|
||||||
}"""
|
}"""
|
||||||
|
|
||||||
|
__plugins_order__ = []
|
||||||
|
"""插件顺序"""
|
||||||
|
|
||||||
|
|
||||||
|
def generate_plugin_order():
|
||||||
|
""" 根据__plugin__生成插件初始顺序,无视是否启用 """
|
||||||
|
global __plugins_order__
|
||||||
|
__plugins_order__ = []
|
||||||
|
for plugin_name in __plugins__:
|
||||||
|
__plugins_order__.append(plugin_name)
|
||||||
|
|
||||||
|
|
||||||
|
def iter_plugins():
|
||||||
|
""" 按照顺序迭代插件 """
|
||||||
|
for plugin_name in __plugins_order__:
|
||||||
|
yield __plugins__[plugin_name]
|
||||||
|
|
||||||
|
|
||||||
__current_module_path__ = ""
|
__current_module_path__ = ""
|
||||||
|
|
||||||
@@ -48,11 +66,15 @@ def walk_plugin_path(module, prefix='', path_prefix=''):
|
|||||||
walk_plugin_path(__import__(module.__name__ + '.' + item.name, fromlist=['']),
|
walk_plugin_path(__import__(module.__name__ + '.' + item.name, fromlist=['']),
|
||||||
prefix + item.name + '.', path_prefix + item.name + '/')
|
prefix + item.name + '.', path_prefix + item.name + '/')
|
||||||
else:
|
else:
|
||||||
logging.debug("扫描插件模块: plugins/{}".format(path_prefix + item.name + '.py'))
|
try:
|
||||||
logging.info('加载模块: plugins/{}'.format(path_prefix + item.name + '.py'))
|
logging.debug("扫描插件模块: plugins/{}".format(path_prefix + item.name + '.py'))
|
||||||
__current_module_path__ = "plugins/"+path_prefix + item.name + '.py'
|
__current_module_path__ = "plugins/"+path_prefix + item.name + '.py'
|
||||||
|
|
||||||
importlib.import_module(module.__name__ + '.' + item.name)
|
importlib.import_module(module.__name__ + '.' + item.name)
|
||||||
|
logging.info('加载模块: plugins/{} 成功'.format(path_prefix + item.name + '.py'))
|
||||||
|
except:
|
||||||
|
logging.error('加载模块: plugins/{} 失败: {}'.format(path_prefix + item.name + '.py', sys.exc_info()))
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
def load_plugins():
|
def load_plugins():
|
||||||
@@ -66,11 +88,16 @@ def load_plugins():
|
|||||||
# 加载开关数据
|
# 加载开关数据
|
||||||
switch.load_switch()
|
switch.load_switch()
|
||||||
|
|
||||||
|
# 生成初始顺序
|
||||||
|
generate_plugin_order()
|
||||||
|
# 加载插件顺序
|
||||||
|
settings.load_settings()
|
||||||
|
|
||||||
|
|
||||||
def initialize_plugins():
|
def initialize_plugins():
|
||||||
""" 初始化插件 """
|
""" 初始化插件 """
|
||||||
logging.info("初始化插件")
|
logging.info("初始化插件")
|
||||||
for plugin in __plugins__.values():
|
for plugin in iter_plugins():
|
||||||
if not plugin['enabled']:
|
if not plugin['enabled']:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
@@ -239,7 +266,7 @@ class PluginHost:
|
|||||||
""" 触发事件 """
|
""" 触发事件 """
|
||||||
event_context = EventContext(event_name)
|
event_context = EventContext(event_name)
|
||||||
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
||||||
for plugin in __plugins__.values():
|
for plugin in iter_plugins():
|
||||||
|
|
||||||
if not plugin['enabled']:
|
if not plugin['enabled']:
|
||||||
continue
|
continue
|
||||||
|
|||||||
84
pkg/plugin/settings.py
Normal file
84
pkg/plugin/settings.py
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pkg.plugin.host as host
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
def wrapper_dict_from_runtime_context() -> dict:
|
||||||
|
"""从变量中包装settings.json的数据字典"""
|
||||||
|
settings = {
|
||||||
|
"order": []
|
||||||
|
}
|
||||||
|
|
||||||
|
for plugin_name in host.__plugins_order__:
|
||||||
|
settings["order"].append(plugin_name)
|
||||||
|
|
||||||
|
return settings
|
||||||
|
|
||||||
|
|
||||||
|
def apply_settings(settings: dict):
|
||||||
|
"""将settings.json数据应用到变量中"""
|
||||||
|
if "order" in settings:
|
||||||
|
host.__plugins_order__ = settings["order"]
|
||||||
|
|
||||||
|
|
||||||
|
def dump_settings():
|
||||||
|
"""保存settings.json数据"""
|
||||||
|
logging.debug("保存plugins/settings.json数据")
|
||||||
|
|
||||||
|
settings = wrapper_dict_from_runtime_context()
|
||||||
|
|
||||||
|
with open("plugins/settings.json", "w", encoding="utf-8") as f:
|
||||||
|
json.dump(settings, f, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
|
def load_settings():
|
||||||
|
"""加载settings.json数据"""
|
||||||
|
logging.debug("加载plugins/settings.json数据")
|
||||||
|
|
||||||
|
# 读取plugins/settings.json
|
||||||
|
settings = {
|
||||||
|
}
|
||||||
|
|
||||||
|
# 检查文件是否存在
|
||||||
|
if not os.path.exists("plugins/settings.json"):
|
||||||
|
# 不存在则创建
|
||||||
|
with open("plugins/settings.json", "w", encoding="utf-8") as f:
|
||||||
|
json.dump(wrapper_dict_from_runtime_context(), f, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
with open("plugins/settings.json", "r", encoding="utf-8") as f:
|
||||||
|
settings = json.load(f)
|
||||||
|
|
||||||
|
if settings is None:
|
||||||
|
settings = {
|
||||||
|
}
|
||||||
|
|
||||||
|
# 检查每个设置项
|
||||||
|
if "order" not in settings:
|
||||||
|
settings["order"] = []
|
||||||
|
|
||||||
|
settings_modified = False
|
||||||
|
|
||||||
|
settings_copy = settings.copy()
|
||||||
|
|
||||||
|
# 检查settings中多余的插件项
|
||||||
|
|
||||||
|
# order
|
||||||
|
for plugin_name in settings_copy["order"]:
|
||||||
|
if plugin_name not in host.__plugins_order__:
|
||||||
|
settings["order"].remove(plugin_name)
|
||||||
|
settings_modified = True
|
||||||
|
|
||||||
|
# 检查settings中缺少的插件项
|
||||||
|
|
||||||
|
# order
|
||||||
|
for plugin_name in host.__plugins_order__:
|
||||||
|
if plugin_name not in settings_copy["order"]:
|
||||||
|
settings["order"].append(plugin_name)
|
||||||
|
settings_modified = True
|
||||||
|
|
||||||
|
if settings_modified:
|
||||||
|
dump_settings()
|
||||||
|
|
||||||
|
apply_settings(settings)
|
||||||
Reference in New Issue
Block a user