feat: 模型视觉多模态支持

This commit is contained in:
RockChinQ
2024-05-15 21:40:18 +08:00
parent 8807f02f36
commit d5b5d667a5
32 changed files with 596 additions and 72 deletions
+6 -4
View File
@@ -27,7 +27,7 @@ class JSONConfigFile(file_model.ConfigFile):
else:
raise ValueError("template_file_name or template_data must be provided")
async def load(self) -> dict:
async def load(self, completion: bool=True) -> dict:
if not self.exists():
await self.create()
@@ -39,9 +39,11 @@ class JSONConfigFile(file_model.ConfigFile):
with open(self.config_file_name, "r", encoding="utf-8") as f:
cfg = json.load(f)
for key in self.template_data:
if key not in cfg:
cfg[key] = self.template_data[key]
if completion:
for key in self.template_data:
if key not in cfg:
cfg[key] = self.template_data[key]
return cfg
+11 -10
View File
@@ -25,7 +25,7 @@ class PythonModuleConfigFile(file_model.ConfigFile):
async def create(self):
shutil.copyfile(self.template_file_name, self.config_file_name)
async def load(self) -> dict:
async def load(self, completion: bool=True) -> dict:
module_name = os.path.splitext(os.path.basename(self.config_file_name))[0]
module = importlib.import_module(module_name)
@@ -43,18 +43,19 @@ class PythonModuleConfigFile(file_model.ConfigFile):
cfg[key] = getattr(module, key)
# 从模板模块文件中进行补全
module_name = os.path.splitext(os.path.basename(self.template_file_name))[0]
module = importlib.import_module(module_name)
if completion:
module_name = os.path.splitext(os.path.basename(self.template_file_name))[0]
module = importlib.import_module(module_name)
for key in dir(module):
if key.startswith('__'):
continue
for key in dir(module):
if key.startswith('__'):
continue
if not isinstance(getattr(module, key), allowed_types):
continue
if not isinstance(getattr(module, key), allowed_types):
continue
if key not in cfg:
cfg[key] = getattr(module, key)
if key not in cfg:
cfg[key] = getattr(module, key)
return cfg
+6 -6
View File
@@ -20,8 +20,8 @@ class ConfigManager:
self.file = cfg_file
self.data = {}
async def load_config(self):
self.data = await self.file.load()
async def load_config(self, completion: bool=True):
self.data = await self.file.load(completion=completion)
async def dump_config(self):
await self.file.save(self.data)
@@ -30,7 +30,7 @@ class ConfigManager:
self.file.save_sync(self.data)
async def load_python_module_config(config_name: str, template_name: str) -> ConfigManager:
async def load_python_module_config(config_name: str, template_name: str, completion: bool=True) -> ConfigManager:
"""加载Python模块配置文件"""
cfg_inst = pymodule.PythonModuleConfigFile(
config_name,
@@ -38,12 +38,12 @@ async def load_python_module_config(config_name: str, template_name: str) -> Con
)
cfg_mgr = ConfigManager(cfg_inst)
await cfg_mgr.load_config()
await cfg_mgr.load_config(completion=completion)
return cfg_mgr
async def load_json_config(config_name: str, template_name: str=None, template_data: dict=None) -> ConfigManager:
async def load_json_config(config_name: str, template_name: str=None, template_data: dict=None, completion: bool=True) -> ConfigManager:
"""加载JSON配置文件"""
cfg_inst = json_file.JSONConfigFile(
config_name,
@@ -52,6 +52,6 @@ async def load_json_config(config_name: str, template_name: str=None, template_d
)
cfg_mgr = ConfigManager(cfg_inst)
await cfg_mgr.load_config()
await cfg_mgr.load_config(completion=completion)
return cfg_mgr
@@ -0,0 +1,35 @@
from __future__ import annotations
from .. import migration
@migration.migration_class("vision-and-oss-config", 6)
class VisionAndOSSConfigMigration(migration.Migration):
"""迁移"""
async def need_migrate(self) -> bool:
"""判断当前环境是否需要运行此迁移"""
return "enable-vision" not in self.ap.provider_cfg.data \
or "oss" not in self.ap.system_cfg.data
async def run(self):
"""执行迁移"""
if "enable-vision" not in self.ap.provider_cfg.data:
self.ap.provider_cfg.data["enable-vision"] = False
if "oss" not in self.ap.system_cfg.data:
self.ap.system_cfg.data["oss"] = [
{
"type": "aliyun",
"endpoint": "https://oss-cn-hangzhou.aliyuncs.com",
"public-read-base-url": "https://qchatgpt.oss-cn-hangzhou.aliyuncs.com",
"access-key-id": "LTAI5tJ5Q5J8J6J5J5J5J5J5",
"access-key-secret": "xxxxxx",
"bucket": "qchatgpt",
"prefix": "qchatgpt",
"enable": False,
}
]
await self.ap.provider_cfg.dump_config()
await self.ap.system_cfg.dump_config()
+1 -1
View File
@@ -22,7 +22,7 @@ class ConfigFile(metaclass=abc.ABCMeta):
pass
@abc.abstractmethod
async def load(self) -> dict:
async def load(self, completion: bool=True) -> dict:
pass
@abc.abstractmethod