大修LLMReq
This commit is contained in:
@@ -1,180 +1,128 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Dict, Union
|
||||
import threading
|
||||
import time
|
||||
|
||||
from packaging.version import Version
|
||||
|
||||
NEWEST_VER = "0.2.1" # 当前支持的最新版本
|
||||
|
||||
@dataclass
|
||||
class APIProvider:
|
||||
name: str = "" # API提供商名称
|
||||
base_url: str = "" # API基础URL
|
||||
api_key: str = field(repr=False, default="") # API密钥(向后兼容)
|
||||
api_keys: List[str] = field(repr=False, default_factory=list) # API密钥列表(新格式)
|
||||
client_type: str = "openai" # 客户端类型(如openai/google等,默认为openai)
|
||||
|
||||
# 多API Key管理相关属性
|
||||
_current_key_index: int = field(default=0, init=False, repr=False) # 当前使用的key索引
|
||||
_key_failure_count: Dict[int, int] = field(default_factory=dict, init=False, repr=False) # 每个key的失败次数
|
||||
_key_last_failure_time: Dict[int, float] = field(default_factory=dict, init=False, repr=False) # 每个key最后失败时间
|
||||
_lock: threading.Lock = field(default_factory=threading.Lock, init=False, repr=False) # 线程锁
|
||||
|
||||
def __post_init__(self):
|
||||
"""初始化后处理,确保API keys列表正确"""
|
||||
# 向后兼容:如果只设置了api_key,将其添加到api_keys列表
|
||||
if self.api_key and not self.api_keys:
|
||||
self.api_keys = [self.api_key]
|
||||
# 如果api_keys不为空但api_key为空,设置api_key为第一个
|
||||
elif self.api_keys and not self.api_key:
|
||||
self.api_key = self.api_keys[0]
|
||||
|
||||
# 初始化失败计数器
|
||||
for i in range(len(self.api_keys)):
|
||||
self._key_failure_count[i] = 0
|
||||
self._key_last_failure_time[i] = 0
|
||||
|
||||
def get_current_api_key(self) -> str:
|
||||
"""获取当前应该使用的API Key"""
|
||||
with self._lock:
|
||||
if not self.api_keys:
|
||||
return ""
|
||||
|
||||
# 确保索引在有效范围内
|
||||
if self._current_key_index >= len(self.api_keys):
|
||||
self._current_key_index = 0
|
||||
|
||||
return self.api_keys[self._current_key_index]
|
||||
|
||||
def get_next_api_key(self) -> Union[str, None]:
|
||||
"""获取下一个可用的API Key(负载均衡)"""
|
||||
with self._lock:
|
||||
if not self.api_keys:
|
||||
return None
|
||||
|
||||
# 如果只有一个key,直接返回
|
||||
if len(self.api_keys) == 1:
|
||||
return self.api_keys[0]
|
||||
|
||||
# 轮询到下一个key
|
||||
self._current_key_index = (self._current_key_index + 1) % len(self.api_keys)
|
||||
return self.api_keys[self._current_key_index]
|
||||
|
||||
def mark_key_failed(self, api_key: str) -> Union[str, None]:
|
||||
"""标记某个API Key失败,返回下一个可用的key"""
|
||||
with self._lock:
|
||||
if not self.api_keys or api_key not in self.api_keys:
|
||||
return None
|
||||
|
||||
key_index = self.api_keys.index(api_key)
|
||||
self._key_failure_count[key_index] += 1
|
||||
self._key_last_failure_time[key_index] = time.time()
|
||||
|
||||
# 寻找下一个可用的key
|
||||
current_time = time.time()
|
||||
for _ in range(len(self.api_keys)):
|
||||
self._current_key_index = (self._current_key_index + 1) % len(self.api_keys)
|
||||
next_key_index = self._current_key_index
|
||||
|
||||
# 检查该key是否最近失败过(5分钟内失败超过3次则暂时跳过)
|
||||
if (self._key_failure_count[next_key_index] <= 3 or
|
||||
current_time - self._key_last_failure_time[next_key_index] > 300): # 5分钟后重试
|
||||
return self.api_keys[next_key_index]
|
||||
|
||||
# 如果所有key都不可用,返回当前key(让上层处理)
|
||||
return api_key
|
||||
|
||||
def reset_key_failures(self, api_key: str | None = None):
|
||||
"""重置失败计数(成功调用后调用)"""
|
||||
with self._lock:
|
||||
if api_key and api_key in self.api_keys:
|
||||
key_index = self.api_keys.index(api_key)
|
||||
self._key_failure_count[key_index] = 0
|
||||
self._key_last_failure_time[key_index] = 0
|
||||
else:
|
||||
# 重置所有key的失败计数
|
||||
for i in range(len(self.api_keys)):
|
||||
self._key_failure_count[i] = 0
|
||||
self._key_last_failure_time[i] = 0
|
||||
|
||||
def get_api_key_stats(self) -> Dict[str, Dict[str, Union[int, float]]]:
|
||||
"""获取API Key使用统计"""
|
||||
with self._lock:
|
||||
stats = {}
|
||||
for i, key in enumerate(self.api_keys):
|
||||
# 只显示key的前8位和后4位,中间用*代替
|
||||
masked_key = f"{key[:8]}***{key[-4:]}" if len(key) > 12 else "***"
|
||||
stats[masked_key] = {
|
||||
"failure_count": self._key_failure_count.get(i, 0),
|
||||
"last_failure_time": self._key_last_failure_time.get(i, 0),
|
||||
"is_current": i == self._current_key_index
|
||||
}
|
||||
return stats
|
||||
from .config_base import ConfigBase
|
||||
|
||||
|
||||
@dataclass
|
||||
class ModelInfo:
|
||||
model_identifier: str = "" # 模型标识符(用于URL调用)
|
||||
name: str = "" # 模型名称(用于模块调用)
|
||||
api_provider: str = "" # API提供商(如OpenAI、Azure等)
|
||||
class APIProvider(ConfigBase):
|
||||
"""API提供商配置类"""
|
||||
|
||||
# 以下用于模型计费
|
||||
price_in: float = 0.0 # 每M token输入价格
|
||||
price_out: float = 0.0 # 每M token输出价格
|
||||
name: str
|
||||
"""API提供商名称"""
|
||||
|
||||
force_stream_mode: bool = False # 是否强制使用流式输出模式
|
||||
|
||||
# 新增:任务类型和能力字段
|
||||
task_type: str = "" # 任务类型:llm_normal, llm_reasoning, vision, embedding, speech
|
||||
capabilities: List[str] = field(default_factory=list) # 模型能力:text, vision, embedding, speech, tool_calling, reasoning
|
||||
base_url: str
|
||||
"""API基础URL"""
|
||||
|
||||
api_key: str = field(default_factory=str, repr=False)
|
||||
"""API密钥列表"""
|
||||
|
||||
client_type: str = field(default="openai")
|
||||
"""客户端类型(如openai/google等,默认为openai)"""
|
||||
|
||||
max_retry: int = 2
|
||||
"""最大重试次数(单个模型API调用失败,最多重试的次数)"""
|
||||
|
||||
timeout: int = 10
|
||||
"""API调用的超时时长(超过这个时长,本次请求将被视为“请求超时”,单位:秒)"""
|
||||
|
||||
retry_interval: int = 10
|
||||
"""重试间隔(如果API调用失败,重试的间隔时间,单位:秒)"""
|
||||
|
||||
def get_api_key(self) -> str:
|
||||
return self.api_key
|
||||
|
||||
|
||||
@dataclass
|
||||
class RequestConfig:
|
||||
max_retry: int = 2 # 最大重试次数(单个模型API调用失败,最多重试的次数)
|
||||
timeout: int = (
|
||||
10 # API调用的超时时长(超过这个时长,本次请求将被视为“请求超时”,单位:秒)
|
||||
)
|
||||
retry_interval: int = 10 # 重试间隔(如果API调用失败,重试的间隔时间,单位:秒)
|
||||
default_temperature: float = 0.7 # 默认的温度(如果bot_config.toml中没有设置temperature参数,默认使用这个值)
|
||||
default_max_tokens: int = 1024 # 默认的最大输出token数(如果bot_config.toml中没有设置max_tokens参数,默认使用这个值)
|
||||
class ModelInfo(ConfigBase):
|
||||
"""单个模型信息配置类"""
|
||||
|
||||
model_identifier: str
|
||||
"""模型标识符(用于URL调用)"""
|
||||
|
||||
name: str
|
||||
"""模型名称(用于模块调用)"""
|
||||
|
||||
api_provider: str
|
||||
"""API提供商(如OpenAI、Azure等)"""
|
||||
|
||||
price_in: float = field(default=0.0)
|
||||
"""每M token输入价格"""
|
||||
|
||||
price_out: float = field(default=0.0)
|
||||
"""每M token输出价格"""
|
||||
|
||||
force_stream_mode: bool = field(default=False)
|
||||
"""是否强制使用流式输出模式"""
|
||||
|
||||
has_thinking: bool = field(default=False)
|
||||
"""是否有思考参数"""
|
||||
|
||||
enable_thinking: bool = field(default=False)
|
||||
"""是否启用思考"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class ModelUsageArgConfigItem:
|
||||
"""模型使用的配置类
|
||||
该类用于加载和存储子任务模型使用的配置
|
||||
"""
|
||||
class TaskConfig(ConfigBase):
|
||||
"""任务配置类"""
|
||||
|
||||
name: str = "" # 模型名称
|
||||
temperature: float | None = None # 温度
|
||||
max_tokens: int | None = None # 最大token数
|
||||
max_retry: int | None = None # 调用失败时的最大重试次数
|
||||
model_list: list[str] = field(default_factory=list)
|
||||
"""任务使用的模型列表"""
|
||||
|
||||
max_tokens: int = 1024
|
||||
"""任务最大输出token数"""
|
||||
|
||||
temperature: float = 0.3
|
||||
"""模型温度"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class ModelUsageArgConfig:
|
||||
"""子任务使用模型的配置类
|
||||
该类用于加载和存储子任务使用的模型配置
|
||||
"""
|
||||
class ModelTaskConfig(ConfigBase):
|
||||
"""模型配置类"""
|
||||
|
||||
name: str = "" # 任务名称
|
||||
usage: List[ModelUsageArgConfigItem] = field(
|
||||
default_factory=lambda: []
|
||||
) # 任务使用的模型列表
|
||||
utils: TaskConfig
|
||||
"""组件模型配置"""
|
||||
|
||||
utils_small: TaskConfig
|
||||
"""组件小模型配置"""
|
||||
|
||||
replyer_1: TaskConfig
|
||||
"""normal_chat首要回复模型模型配置"""
|
||||
|
||||
@dataclass
|
||||
class ModuleConfig:
|
||||
INNER_VERSION: Version | None = None # 配置文件版本
|
||||
replyer_2: TaskConfig
|
||||
"""normal_chat次要回复模型配置"""
|
||||
|
||||
req_conf: RequestConfig = field(default_factory=lambda: RequestConfig()) # 请求配置
|
||||
api_providers: Dict[str, APIProvider] = field(
|
||||
default_factory=lambda: {}
|
||||
) # API提供商列表
|
||||
models: Dict[str, ModelInfo] = field(default_factory=lambda: {}) # 模型列表
|
||||
task_model_arg_map: Dict[str, ModelUsageArgConfig] = field(
|
||||
default_factory=lambda: {}
|
||||
)
|
||||
memory: TaskConfig
|
||||
"""记忆模型配置"""
|
||||
|
||||
emotion: TaskConfig
|
||||
"""情绪模型配置"""
|
||||
|
||||
vlm: TaskConfig
|
||||
"""视觉语言模型配置"""
|
||||
|
||||
voice: TaskConfig
|
||||
"""语音识别模型配置"""
|
||||
|
||||
tool_use: TaskConfig
|
||||
"""专注工具使用模型配置"""
|
||||
|
||||
planner: TaskConfig
|
||||
"""规划模型配置"""
|
||||
|
||||
embedding: TaskConfig
|
||||
"""嵌入模型配置"""
|
||||
|
||||
lpmm_entity_extract: TaskConfig
|
||||
"""LPMM实体提取模型配置"""
|
||||
|
||||
lpmm_rdf_build: TaskConfig
|
||||
"""LPMM RDF构建模型配置"""
|
||||
|
||||
lpmm_qa: TaskConfig
|
||||
"""LPMM问答模型配置"""
|
||||
|
||||
def get_task(self, task_name: str) -> TaskConfig:
|
||||
"""获取指定任务的配置"""
|
||||
if hasattr(self, task_name):
|
||||
return getattr(self, task_name)
|
||||
raise ValueError(f"任务 '{task_name}' 未找到对应的配置")
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import os
|
||||
import tomlkit
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
from datetime import datetime
|
||||
from tomlkit import TOMLDocument
|
||||
from tomlkit.items import Table, KeyType
|
||||
from dataclasses import field, dataclass
|
||||
from rich.traceback import install
|
||||
from packaging import version
|
||||
from packaging.specifiers import SpecifierSet
|
||||
from packaging.version import Version, InvalidVersion
|
||||
from typing import Any, Dict, List
|
||||
from typing import List, Optional
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config_base import ConfigBase
|
||||
@@ -29,7 +27,6 @@ from src.config.official_configs import (
|
||||
ResponseSplitterConfig,
|
||||
TelemetryConfig,
|
||||
ExperimentalConfig,
|
||||
ModelConfig,
|
||||
MessageReceiveConfig,
|
||||
MaimMessageConfig,
|
||||
LPMMKnowledgeConfig,
|
||||
@@ -41,16 +38,12 @@ from src.config.official_configs import (
|
||||
)
|
||||
|
||||
from .api_ada_configs import (
|
||||
ModelUsageArgConfigItem,
|
||||
ModelUsageArgConfig,
|
||||
APIProvider,
|
||||
ModelTaskConfig,
|
||||
ModelInfo,
|
||||
NEWEST_VER,
|
||||
ModuleConfig,
|
||||
APIProvider,
|
||||
)
|
||||
|
||||
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
|
||||
@@ -64,275 +57,270 @@ TEMPLATE_DIR = os.path.join(PROJECT_ROOT, "template")
|
||||
|
||||
# 考虑到,实际上配置文件中的mai_version是不会自动更新的,所以采用硬编码
|
||||
# 对该字段的更新,请严格参照语义化版本规范:https://semver.org/lang/zh-CN/
|
||||
MMC_VERSION = "0.10.0-snapshot1"
|
||||
MMC_VERSION = "0.10.0-snapshot.2"
|
||||
|
||||
|
||||
# def _get_config_version(toml: Dict) -> Version:
|
||||
# """提取配置文件的 SpecifierSet 版本数据
|
||||
# Args:
|
||||
# toml[dict]: 输入的配置文件字典
|
||||
# Returns:
|
||||
# Version
|
||||
# """
|
||||
|
||||
# if "inner" in toml and "version" in toml["inner"]:
|
||||
# config_version: str = toml["inner"]["version"]
|
||||
# else:
|
||||
# raise InvalidVersion("配置文件缺少版本信息,请检查配置文件。")
|
||||
|
||||
# try:
|
||||
# return version.parse(config_version)
|
||||
# except InvalidVersion as e:
|
||||
# logger.error(
|
||||
# "配置文件中 inner段 的 version 键是错误的版本描述\n"
|
||||
# f"请检查配置文件,当前 version 键: {config_version}\n"
|
||||
# f"错误信息: {e}"
|
||||
# )
|
||||
# raise e
|
||||
|
||||
|
||||
def _get_config_version(toml: Dict) -> Version:
|
||||
"""提取配置文件的 SpecifierSet 版本数据
|
||||
Args:
|
||||
toml[dict]: 输入的配置文件字典
|
||||
Returns:
|
||||
Version
|
||||
"""
|
||||
|
||||
if "inner" in toml and "version" in toml["inner"]:
|
||||
config_version: str = toml["inner"]["version"]
|
||||
else:
|
||||
config_version = "0.0.0" # 默认版本
|
||||
|
||||
try:
|
||||
ver = version.parse(config_version)
|
||||
except InvalidVersion as e:
|
||||
logger.error(
|
||||
"配置文件中 inner段 的 version 键是错误的版本描述\n"
|
||||
f"请检查配置文件,当前 version 键: {config_version}\n"
|
||||
f"错误信息: {e}"
|
||||
)
|
||||
raise InvalidVersion(
|
||||
"配置文件中 inner段 的 version 键是错误的版本描述\n"
|
||||
) from e
|
||||
|
||||
return ver
|
||||
# def _request_conf(parent: Dict, config: ModuleConfig):
|
||||
# request_conf_config = parent.get("request_conf")
|
||||
# config.req_conf.max_retry = request_conf_config.get(
|
||||
# "max_retry", config.req_conf.max_retry
|
||||
# )
|
||||
# config.req_conf.timeout = request_conf_config.get(
|
||||
# "timeout", config.req_conf.timeout
|
||||
# )
|
||||
# config.req_conf.retry_interval = request_conf_config.get(
|
||||
# "retry_interval", config.req_conf.retry_interval
|
||||
# )
|
||||
# config.req_conf.default_temperature = request_conf_config.get(
|
||||
# "default_temperature", config.req_conf.default_temperature
|
||||
# )
|
||||
# config.req_conf.default_max_tokens = request_conf_config.get(
|
||||
# "default_max_tokens", config.req_conf.default_max_tokens
|
||||
# )
|
||||
|
||||
|
||||
def _request_conf(parent: Dict, config: ModuleConfig):
|
||||
request_conf_config = parent.get("request_conf")
|
||||
config.req_conf.max_retry = request_conf_config.get(
|
||||
"max_retry", config.req_conf.max_retry
|
||||
)
|
||||
config.req_conf.timeout = request_conf_config.get(
|
||||
"timeout", config.req_conf.timeout
|
||||
)
|
||||
config.req_conf.retry_interval = request_conf_config.get(
|
||||
"retry_interval", config.req_conf.retry_interval
|
||||
)
|
||||
config.req_conf.default_temperature = request_conf_config.get(
|
||||
"default_temperature", config.req_conf.default_temperature
|
||||
)
|
||||
config.req_conf.default_max_tokens = request_conf_config.get(
|
||||
"default_max_tokens", config.req_conf.default_max_tokens
|
||||
)
|
||||
# def _api_providers(parent: Dict, config: ModuleConfig):
|
||||
# api_providers_config = parent.get("api_providers")
|
||||
# for provider in api_providers_config:
|
||||
# name = provider.get("name", None)
|
||||
# base_url = provider.get("base_url", None)
|
||||
# api_key = provider.get("api_key", None)
|
||||
# api_keys = provider.get("api_keys", []) # 新增:支持多个API Key
|
||||
# client_type = provider.get("client_type", "openai")
|
||||
|
||||
# if name in config.api_providers: # 查重
|
||||
# logger.error(f"重复的API提供商名称: {name},请检查配置文件。")
|
||||
# raise KeyError(f"重复的API提供商名称: {name},请检查配置文件。")
|
||||
|
||||
# if name and base_url:
|
||||
# # 处理API Key配置:支持单个api_key或多个api_keys
|
||||
# if api_keys:
|
||||
# # 使用新格式:api_keys列表
|
||||
# logger.debug(f"API提供商 '{name}' 配置了 {len(api_keys)} 个API Key")
|
||||
# elif api_key:
|
||||
# # 向后兼容:使用单个api_key
|
||||
# api_keys = [api_key]
|
||||
# logger.debug(f"API提供商 '{name}' 使用单个API Key(向后兼容模式)")
|
||||
# else:
|
||||
# logger.warning(f"API提供商 '{name}' 没有配置API Key,某些功能可能不可用")
|
||||
|
||||
# config.api_providers[name] = APIProvider(
|
||||
# name=name,
|
||||
# base_url=base_url,
|
||||
# api_key=api_key, # 保留向后兼容
|
||||
# api_keys=api_keys, # 新格式
|
||||
# client_type=client_type,
|
||||
# )
|
||||
# else:
|
||||
# logger.error(f"API提供商 '{name}' 的配置不完整,请检查配置文件。")
|
||||
# raise ValueError(f"API提供商 '{name}' 的配置不完整,请检查配置文件。")
|
||||
|
||||
|
||||
def _api_providers(parent: Dict, config: ModuleConfig):
|
||||
api_providers_config = parent.get("api_providers")
|
||||
for provider in api_providers_config:
|
||||
name = provider.get("name", None)
|
||||
base_url = provider.get("base_url", None)
|
||||
api_key = provider.get("api_key", None)
|
||||
api_keys = provider.get("api_keys", []) # 新增:支持多个API Key
|
||||
client_type = provider.get("client_type", "openai")
|
||||
# def _models(parent: Dict, config: ModuleConfig):
|
||||
# models_config = parent.get("models")
|
||||
# for model in models_config:
|
||||
# model_identifier = model.get("model_identifier", None)
|
||||
# name = model.get("name", model_identifier)
|
||||
# api_provider = model.get("api_provider", None)
|
||||
# price_in = model.get("price_in", 0.0)
|
||||
# price_out = model.get("price_out", 0.0)
|
||||
# force_stream_mode = model.get("force_stream_mode", False)
|
||||
# task_type = model.get("task_type", "")
|
||||
# capabilities = model.get("capabilities", [])
|
||||
|
||||
if name in config.api_providers: # 查重
|
||||
logger.error(f"重复的API提供商名称: {name},请检查配置文件。")
|
||||
raise KeyError(f"重复的API提供商名称: {name},请检查配置文件。")
|
||||
# if name in config.models: # 查重
|
||||
# logger.error(f"重复的模型名称: {name},请检查配置文件。")
|
||||
# raise KeyError(f"重复的模型名称: {name},请检查配置文件。")
|
||||
|
||||
if name and base_url:
|
||||
# 处理API Key配置:支持单个api_key或多个api_keys
|
||||
if api_keys:
|
||||
# 使用新格式:api_keys列表
|
||||
logger.debug(f"API提供商 '{name}' 配置了 {len(api_keys)} 个API Key")
|
||||
elif api_key:
|
||||
# 向后兼容:使用单个api_key
|
||||
api_keys = [api_key]
|
||||
logger.debug(f"API提供商 '{name}' 使用单个API Key(向后兼容模式)")
|
||||
else:
|
||||
logger.warning(f"API提供商 '{name}' 没有配置API Key,某些功能可能不可用")
|
||||
|
||||
config.api_providers[name] = APIProvider(
|
||||
name=name,
|
||||
base_url=base_url,
|
||||
api_key=api_key, # 保留向后兼容
|
||||
api_keys=api_keys, # 新格式
|
||||
client_type=client_type,
|
||||
)
|
||||
else:
|
||||
logger.error(f"API提供商 '{name}' 的配置不完整,请检查配置文件。")
|
||||
raise ValueError(f"API提供商 '{name}' 的配置不完整,请检查配置文件。")
|
||||
# if model_identifier and api_provider:
|
||||
# # 检查API提供商是否存在
|
||||
# if api_provider not in config.api_providers:
|
||||
# logger.error(f"未声明的API提供商 '{api_provider}' ,请检查配置文件。")
|
||||
# raise ValueError(
|
||||
# f"未声明的API提供商 '{api_provider}' ,请检查配置文件。"
|
||||
# )
|
||||
# config.models[name] = ModelInfo(
|
||||
# name=name,
|
||||
# model_identifier=model_identifier,
|
||||
# api_provider=api_provider,
|
||||
# price_in=price_in,
|
||||
# price_out=price_out,
|
||||
# force_stream_mode=force_stream_mode,
|
||||
# task_type=task_type,
|
||||
# capabilities=capabilities,
|
||||
# )
|
||||
# else:
|
||||
# logger.error(f"模型 '{name}' 的配置不完整,请检查配置文件。")
|
||||
# raise ValueError(f"模型 '{name}' 的配置不完整,请检查配置文件。")
|
||||
|
||||
|
||||
def _models(parent: Dict, config: ModuleConfig):
|
||||
models_config = parent.get("models")
|
||||
for model in models_config:
|
||||
model_identifier = model.get("model_identifier", None)
|
||||
name = model.get("name", model_identifier)
|
||||
api_provider = model.get("api_provider", None)
|
||||
price_in = model.get("price_in", 0.0)
|
||||
price_out = model.get("price_out", 0.0)
|
||||
force_stream_mode = model.get("force_stream_mode", False)
|
||||
task_type = model.get("task_type", "")
|
||||
capabilities = model.get("capabilities", [])
|
||||
# def _task_model_usage(parent: Dict, config: ModuleConfig):
|
||||
# model_usage_configs = parent.get("task_model_usage")
|
||||
# config.task_model_arg_map = {}
|
||||
# for task_name, item in model_usage_configs.items():
|
||||
# if task_name in config.task_model_arg_map:
|
||||
# logger.error(f"子任务 {task_name} 已存在,请检查配置文件。")
|
||||
# raise KeyError(f"子任务 {task_name} 已存在,请检查配置文件。")
|
||||
|
||||
if name in config.models: # 查重
|
||||
logger.error(f"重复的模型名称: {name},请检查配置文件。")
|
||||
raise KeyError(f"重复的模型名称: {name},请检查配置文件。")
|
||||
# usage = []
|
||||
# if isinstance(item, Dict):
|
||||
# if "model" in item:
|
||||
# usage.append(
|
||||
# ModelUsageArgConfigItem(
|
||||
# name=item["model"],
|
||||
# temperature=item.get("temperature", None),
|
||||
# max_tokens=item.get("max_tokens", None),
|
||||
# max_retry=item.get("max_retry", None),
|
||||
# )
|
||||
# )
|
||||
# else:
|
||||
# logger.error(f"子任务 {task_name} 的模型配置不合法,请检查配置文件。")
|
||||
# raise ValueError(
|
||||
# f"子任务 {task_name} 的模型配置不合法,请检查配置文件。"
|
||||
# )
|
||||
# elif isinstance(item, List):
|
||||
# for model in item:
|
||||
# if isinstance(model, Dict):
|
||||
# usage.append(
|
||||
# ModelUsageArgConfigItem(
|
||||
# name=model["model"],
|
||||
# temperature=model.get("temperature", None),
|
||||
# max_tokens=model.get("max_tokens", None),
|
||||
# max_retry=model.get("max_retry", None),
|
||||
# )
|
||||
# )
|
||||
# elif isinstance(model, str):
|
||||
# usage.append(
|
||||
# ModelUsageArgConfigItem(
|
||||
# name=model,
|
||||
# temperature=None,
|
||||
# max_tokens=None,
|
||||
# max_retry=None,
|
||||
# )
|
||||
# )
|
||||
# else:
|
||||
# logger.error(
|
||||
# f"子任务 {task_name} 的模型配置不合法,请检查配置文件。"
|
||||
# )
|
||||
# raise ValueError(
|
||||
# f"子任务 {task_name} 的模型配置不合法,请检查配置文件。"
|
||||
# )
|
||||
# elif isinstance(item, str):
|
||||
# usage.append(
|
||||
# ModelUsageArgConfigItem(
|
||||
# name=item,
|
||||
# temperature=None,
|
||||
# max_tokens=None,
|
||||
# max_retry=None,
|
||||
# )
|
||||
# )
|
||||
|
||||
if model_identifier and api_provider:
|
||||
# 检查API提供商是否存在
|
||||
if api_provider not in config.api_providers:
|
||||
logger.error(f"未声明的API提供商 '{api_provider}' ,请检查配置文件。")
|
||||
raise ValueError(
|
||||
f"未声明的API提供商 '{api_provider}' ,请检查配置文件。"
|
||||
)
|
||||
config.models[name] = ModelInfo(
|
||||
name=name,
|
||||
model_identifier=model_identifier,
|
||||
api_provider=api_provider,
|
||||
price_in=price_in,
|
||||
price_out=price_out,
|
||||
force_stream_mode=force_stream_mode,
|
||||
task_type=task_type,
|
||||
capabilities=capabilities,
|
||||
)
|
||||
else:
|
||||
logger.error(f"模型 '{name}' 的配置不完整,请检查配置文件。")
|
||||
raise ValueError(f"模型 '{name}' 的配置不完整,请检查配置文件。")
|
||||
# config.task_model_arg_map[task_name] = ModelUsageArgConfig(
|
||||
# name=task_name,
|
||||
# usage=usage,
|
||||
# )
|
||||
|
||||
|
||||
def _task_model_usage(parent: Dict, config: ModuleConfig):
|
||||
model_usage_configs = parent.get("task_model_usage")
|
||||
config.task_model_arg_map = {}
|
||||
for task_name, item in model_usage_configs.items():
|
||||
if task_name in config.task_model_arg_map:
|
||||
logger.error(f"子任务 {task_name} 已存在,请检查配置文件。")
|
||||
raise KeyError(f"子任务 {task_name} 已存在,请检查配置文件。")
|
||||
# def api_ada_load_config(config_path: str) -> ModuleConfig:
|
||||
# """从TOML配置文件加载配置"""
|
||||
# config = ModuleConfig()
|
||||
|
||||
usage = []
|
||||
if isinstance(item, Dict):
|
||||
if "model" in item:
|
||||
usage.append(
|
||||
ModelUsageArgConfigItem(
|
||||
name=item["model"],
|
||||
temperature=item.get("temperature", None),
|
||||
max_tokens=item.get("max_tokens", None),
|
||||
max_retry=item.get("max_retry", None),
|
||||
)
|
||||
)
|
||||
else:
|
||||
logger.error(f"子任务 {task_name} 的模型配置不合法,请检查配置文件。")
|
||||
raise ValueError(
|
||||
f"子任务 {task_name} 的模型配置不合法,请检查配置文件。"
|
||||
)
|
||||
elif isinstance(item, List):
|
||||
for model in item:
|
||||
if isinstance(model, Dict):
|
||||
usage.append(
|
||||
ModelUsageArgConfigItem(
|
||||
name=model["model"],
|
||||
temperature=model.get("temperature", None),
|
||||
max_tokens=model.get("max_tokens", None),
|
||||
max_retry=model.get("max_retry", None),
|
||||
)
|
||||
)
|
||||
elif isinstance(model, str):
|
||||
usage.append(
|
||||
ModelUsageArgConfigItem(
|
||||
name=model,
|
||||
temperature=None,
|
||||
max_tokens=None,
|
||||
max_retry=None,
|
||||
)
|
||||
)
|
||||
else:
|
||||
logger.error(
|
||||
f"子任务 {task_name} 的模型配置不合法,请检查配置文件。"
|
||||
)
|
||||
raise ValueError(
|
||||
f"子任务 {task_name} 的模型配置不合法,请检查配置文件。"
|
||||
)
|
||||
elif isinstance(item, str):
|
||||
usage.append(
|
||||
ModelUsageArgConfigItem(
|
||||
name=item,
|
||||
temperature=None,
|
||||
max_tokens=None,
|
||||
max_retry=None,
|
||||
)
|
||||
)
|
||||
# include_configs: Dict[str, Dict[str, Any]] = {
|
||||
# "request_conf": {
|
||||
# "func": _request_conf,
|
||||
# "support": ">=0.0.0",
|
||||
# "necessary": False,
|
||||
# },
|
||||
# "api_providers": {"func": _api_providers, "support": ">=0.0.0"},
|
||||
# "models": {"func": _models, "support": ">=0.0.0"},
|
||||
# "task_model_usage": {"func": _task_model_usage, "support": ">=0.0.0"},
|
||||
# }
|
||||
|
||||
config.task_model_arg_map[task_name] = ModelUsageArgConfig(
|
||||
name=task_name,
|
||||
usage=usage,
|
||||
)
|
||||
# if os.path.exists(config_path):
|
||||
# with open(config_path, "rb") as f:
|
||||
# try:
|
||||
# toml_dict = tomlkit.load(f)
|
||||
# except tomlkit.TOMLDecodeError as e:
|
||||
# logger.critical(
|
||||
# f"配置文件model_list.toml填写有误,请检查第{e.lineno}行第{e.colno}处:{e.msg}"
|
||||
# )
|
||||
# exit(1)
|
||||
|
||||
# # 获取配置文件版本
|
||||
# config.INNER_VERSION = _get_config_version(toml_dict)
|
||||
|
||||
def api_ada_load_config(config_path: str) -> ModuleConfig:
|
||||
"""从TOML配置文件加载配置"""
|
||||
config = ModuleConfig()
|
||||
# # 检查版本
|
||||
# if config.INNER_VERSION > Version(NEWEST_VER):
|
||||
# logger.warning(
|
||||
# f"当前配置文件版本 {config.INNER_VERSION} 高于支持的最新版本 {NEWEST_VER},可能导致异常,建议更新依赖。"
|
||||
# )
|
||||
|
||||
include_configs: Dict[str, Dict[str, Any]] = {
|
||||
"request_conf": {
|
||||
"func": _request_conf,
|
||||
"support": ">=0.0.0",
|
||||
"necessary": False,
|
||||
},
|
||||
"api_providers": {"func": _api_providers, "support": ">=0.0.0"},
|
||||
"models": {"func": _models, "support": ">=0.0.0"},
|
||||
"task_model_usage": {"func": _task_model_usage, "support": ">=0.0.0"},
|
||||
}
|
||||
# # 解析配置文件
|
||||
# # 如果在配置中找到了需要的项,调用对应项的闭包函数处理
|
||||
# for key in include_configs:
|
||||
# if key in toml_dict:
|
||||
# group_specifier_set: SpecifierSet = SpecifierSet(
|
||||
# include_configs[key]["support"]
|
||||
# )
|
||||
|
||||
if os.path.exists(config_path):
|
||||
with open(config_path, "rb") as f:
|
||||
try:
|
||||
toml_dict = tomlkit.load(f)
|
||||
except tomlkit.TOMLDecodeError as e:
|
||||
logger.critical(
|
||||
f"配置文件model_list.toml填写有误,请检查第{e.lineno}行第{e.colno}处:{e.msg}"
|
||||
)
|
||||
exit(1)
|
||||
# # 检查配置文件版本是否在支持范围内
|
||||
# if config.INNER_VERSION in group_specifier_set:
|
||||
# # 如果版本在支持范围内,检查是否存在通知
|
||||
# if "notice" in include_configs[key]:
|
||||
# logger.warning(include_configs[key]["notice"])
|
||||
# # 调用闭包函数处理配置
|
||||
# (include_configs[key]["func"])(toml_dict, config)
|
||||
# else:
|
||||
# # 如果版本不在支持范围内,崩溃并提示用户
|
||||
# logger.error(
|
||||
# f"配置文件中的 '{key}' 字段的版本 ({config.INNER_VERSION}) 不在支持范围内。\n"
|
||||
# f"当前程序仅支持以下版本范围: {group_specifier_set}"
|
||||
# )
|
||||
# raise InvalidVersion(
|
||||
# f"当前程序仅支持以下版本范围: {group_specifier_set}"
|
||||
# )
|
||||
|
||||
# 获取配置文件版本
|
||||
config.INNER_VERSION = _get_config_version(toml_dict)
|
||||
# # 如果 necessary 项目存在,而且显式声明是 False,进入特殊处理
|
||||
# elif (
|
||||
# "necessary" in include_configs[key]
|
||||
# and include_configs[key].get("necessary") is False
|
||||
# ):
|
||||
# # 通过 pass 处理的项虽然直接忽略也是可以的,但是为了不增加理解困难,依然需要在这里显式处理
|
||||
# if key == "keywords_reaction":
|
||||
# pass
|
||||
# else:
|
||||
# # 如果用户根本没有需要的配置项,提示缺少配置
|
||||
# logger.error(f"配置文件中缺少必需的字段: '{key}'")
|
||||
# raise KeyError(f"配置文件中缺少必需的字段: '{key}'")
|
||||
|
||||
# 检查版本
|
||||
if config.INNER_VERSION > Version(NEWEST_VER):
|
||||
logger.warning(
|
||||
f"当前配置文件版本 {config.INNER_VERSION} 高于支持的最新版本 {NEWEST_VER},可能导致异常,建议更新依赖。"
|
||||
)
|
||||
# logger.info(f"成功加载配置文件: {config_path}")
|
||||
|
||||
# 解析配置文件
|
||||
# 如果在配置中找到了需要的项,调用对应项的闭包函数处理
|
||||
for key in include_configs:
|
||||
if key in toml_dict:
|
||||
group_specifier_set: SpecifierSet = SpecifierSet(
|
||||
include_configs[key]["support"]
|
||||
)
|
||||
# return config
|
||||
|
||||
# 检查配置文件版本是否在支持范围内
|
||||
if config.INNER_VERSION in group_specifier_set:
|
||||
# 如果版本在支持范围内,检查是否存在通知
|
||||
if "notice" in include_configs[key]:
|
||||
logger.warning(include_configs[key]["notice"])
|
||||
# 调用闭包函数处理配置
|
||||
(include_configs[key]["func"])(toml_dict, config)
|
||||
else:
|
||||
# 如果版本不在支持范围内,崩溃并提示用户
|
||||
logger.error(
|
||||
f"配置文件中的 '{key}' 字段的版本 ({config.INNER_VERSION}) 不在支持范围内。\n"
|
||||
f"当前程序仅支持以下版本范围: {group_specifier_set}"
|
||||
)
|
||||
raise InvalidVersion(
|
||||
f"当前程序仅支持以下版本范围: {group_specifier_set}"
|
||||
)
|
||||
|
||||
# 如果 necessary 项目存在,而且显式声明是 False,进入特殊处理
|
||||
elif (
|
||||
"necessary" in include_configs[key]
|
||||
and include_configs[key].get("necessary") is False
|
||||
):
|
||||
# 通过 pass 处理的项虽然直接忽略也是可以的,但是为了不增加理解困难,依然需要在这里显式处理
|
||||
if key == "keywords_reaction":
|
||||
pass
|
||||
else:
|
||||
# 如果用户根本没有需要的配置项,提示缺少配置
|
||||
logger.error(f"配置文件中缺少必需的字段: '{key}'")
|
||||
raise KeyError(f"配置文件中缺少必需的字段: '{key}'")
|
||||
|
||||
logger.info(f"成功加载配置文件: {config_path}")
|
||||
|
||||
return config
|
||||
|
||||
def get_key_comment(toml_table, key):
|
||||
# 获取key的注释(如果有)
|
||||
@@ -361,7 +349,7 @@ def compare_dicts(new, old, path=None, logs=None):
|
||||
continue
|
||||
if key not in old:
|
||||
comment = get_key_comment(new, key)
|
||||
logs.append(f"新增: {'.'.join(path + [str(key)])} 注释: {comment if comment else '无'}")
|
||||
logs.append(f"新增: {'.'.join(path + [str(key)])} 注释: {comment or '无'}")
|
||||
elif isinstance(new[key], (dict, Table)) and isinstance(old.get(key), (dict, Table)):
|
||||
compare_dicts(new[key], old[key], path + [str(key)], logs)
|
||||
# 删减项
|
||||
@@ -370,7 +358,7 @@ def compare_dicts(new, old, path=None, logs=None):
|
||||
continue
|
||||
if key not in new:
|
||||
comment = get_key_comment(old, key)
|
||||
logs.append(f"删减: {'.'.join(path + [str(key)])} 注释: {comment if comment else '无'}")
|
||||
logs.append(f"删减: {'.'.join(path + [str(key)])} 注释: {comment or '无'}")
|
||||
return logs
|
||||
|
||||
|
||||
@@ -405,17 +393,13 @@ def compare_default_values(new, old, path=None, logs=None, changes=None):
|
||||
if key in old:
|
||||
if isinstance(new[key], (dict, Table)) and isinstance(old[key], (dict, Table)):
|
||||
compare_default_values(new[key], old[key], path + [str(key)], logs, changes)
|
||||
else:
|
||||
# 只要值发生变化就记录
|
||||
if new[key] != old[key]:
|
||||
logs.append(
|
||||
f"默认值变化: {'.'.join(path + [str(key)])} 旧默认值: {old[key]} 新默认值: {new[key]}"
|
||||
)
|
||||
changes.append((path + [str(key)], old[key], new[key]))
|
||||
elif new[key] != old[key]:
|
||||
logs.append(f"默认值变化: {'.'.join(path + [str(key)])} 旧默认值: {old[key]} 新默认值: {new[key]}")
|
||||
changes.append((path + [str(key)], old[key], new[key]))
|
||||
return logs, changes
|
||||
|
||||
|
||||
def _get_version_from_toml(toml_path):
|
||||
def _get_version_from_toml(toml_path) -> Optional[str]:
|
||||
"""从TOML文件中获取版本号"""
|
||||
if not os.path.exists(toml_path):
|
||||
return None
|
||||
@@ -459,14 +443,13 @@ def _update_dict(target: TOMLDocument | dict | Table, source: TOMLDocument | dic
|
||||
target[key] = value
|
||||
|
||||
|
||||
def _update_config_generic(config_name: str, template_name: str, should_quit_on_new: bool = True):
|
||||
def _update_config_generic(config_name: str, template_name: str):
|
||||
"""
|
||||
通用的配置文件更新函数
|
||||
|
||||
|
||||
Args:
|
||||
config_name: 配置文件名(不含扩展名),如 'bot_config' 或 'model_config'
|
||||
template_name: 模板文件名(不含扩展名),如 'bot_config_template' 或 'model_config_template'
|
||||
should_quit_on_new: 创建新配置文件后是否退出程序
|
||||
"""
|
||||
# 获取根目录路径
|
||||
old_config_dir = os.path.join(CONFIG_DIR, "old")
|
||||
@@ -484,19 +467,30 @@ def _update_config_generic(config_name: str, template_name: str, should_quit_on_
|
||||
template_version = _get_version_from_toml(template_path)
|
||||
compare_version = _get_version_from_toml(compare_path)
|
||||
|
||||
# 检查配置文件是否存在
|
||||
if not os.path.exists(old_config_path):
|
||||
logger.info(f"{config_name}.toml配置文件不存在,从模板创建新配置")
|
||||
os.makedirs(CONFIG_DIR, exist_ok=True) # 创建文件夹
|
||||
shutil.copy2(template_path, old_config_path) # 复制模板文件
|
||||
logger.info(f"已创建新{config_name}配置文件,请填写后重新运行: {old_config_path}")
|
||||
# 新创建配置文件,退出
|
||||
sys.exit(0)
|
||||
|
||||
compare_config = None
|
||||
new_config = None
|
||||
old_config = None
|
||||
|
||||
# 先读取 compare 下的模板(如果有),用于默认值变动检测
|
||||
if os.path.exists(compare_path):
|
||||
with open(compare_path, "r", encoding="utf-8") as f:
|
||||
compare_config = tomlkit.load(f)
|
||||
else:
|
||||
compare_config = None
|
||||
|
||||
# 读取当前模板
|
||||
with open(template_path, "r", encoding="utf-8") as f:
|
||||
new_config = tomlkit.load(f)
|
||||
|
||||
# 检查默认值变化并处理(只有 compare_config 存在时才做)
|
||||
if compare_config is not None:
|
||||
if compare_config:
|
||||
# 读取旧配置
|
||||
with open(old_config_path, "r", encoding="utf-8") as f:
|
||||
old_config = tomlkit.load(f)
|
||||
@@ -515,32 +509,16 @@ def _update_config_generic(config_name: str, template_name: str, should_quit_on_
|
||||
)
|
||||
else:
|
||||
logger.info(f"未检测到{config_name}模板默认值变动")
|
||||
# 保存旧配置的变更(后续合并逻辑会用到 old_config)
|
||||
else:
|
||||
old_config = None
|
||||
|
||||
# 检查 compare 下没有模板,或新模板版本更高,则复制
|
||||
if not os.path.exists(compare_path):
|
||||
shutil.copy2(template_path, compare_path)
|
||||
logger.info(f"已将{config_name}模板文件复制到: {compare_path}")
|
||||
elif _version_tuple(template_version) > _version_tuple(compare_version):
|
||||
shutil.copy2(template_path, compare_path)
|
||||
logger.info(f"{config_name}模板版本较新,已替换compare下的模板: {compare_path}")
|
||||
else:
|
||||
if _version_tuple(template_version) > _version_tuple(compare_version):
|
||||
shutil.copy2(template_path, compare_path)
|
||||
logger.info(f"{config_name}模板版本较新,已替换compare下的模板: {compare_path}")
|
||||
else:
|
||||
logger.debug(f"compare下的{config_name}模板版本不低于当前模板,无需替换: {compare_path}")
|
||||
|
||||
# 检查配置文件是否存在
|
||||
if not os.path.exists(old_config_path):
|
||||
logger.info(f"{config_name}.toml配置文件不存在,从模板创建新配置")
|
||||
os.makedirs(CONFIG_DIR, exist_ok=True) # 创建文件夹
|
||||
shutil.copy2(template_path, old_config_path) # 复制模板文件
|
||||
logger.info(f"已创建新{config_name}配置文件,请填写后重新运行: {old_config_path}")
|
||||
# 如果是新创建的配置文件,根据参数决定是否退出
|
||||
if should_quit_on_new:
|
||||
quit()
|
||||
else:
|
||||
return
|
||||
logger.debug(f"compare下的{config_name}模板版本不低于当前模板,无需替换: {compare_path}")
|
||||
|
||||
# 读取旧配置文件和模板文件(如果前面没读过 old_config,这里再读一次)
|
||||
if old_config is None:
|
||||
@@ -578,8 +556,7 @@ def _update_config_generic(config_name: str, template_name: str, should_quit_on_
|
||||
# 输出新增和删减项及注释
|
||||
if old_config:
|
||||
logger.info(f"{config_name}配置项变动如下:\n----------------------------------------")
|
||||
logs = compare_dicts(new_config, old_config)
|
||||
if logs:
|
||||
if logs := compare_dicts(new_config, old_config):
|
||||
for log in logs:
|
||||
logger.info(log)
|
||||
else:
|
||||
@@ -597,12 +574,12 @@ def _update_config_generic(config_name: str, template_name: str, should_quit_on_
|
||||
|
||||
def update_config():
|
||||
"""更新bot_config.toml配置文件"""
|
||||
_update_config_generic("bot_config", "bot_config_template", should_quit_on_new=True)
|
||||
_update_config_generic("bot_config", "bot_config_template")
|
||||
|
||||
|
||||
def update_model_config():
|
||||
"""更新model_config.toml配置文件"""
|
||||
_update_config_generic("model_config", "model_config_template", should_quit_on_new=False)
|
||||
_update_config_generic("model_config", "model_config_template")
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -627,7 +604,6 @@ class Config(ConfigBase):
|
||||
response_splitter: ResponseSplitterConfig
|
||||
telemetry: TelemetryConfig
|
||||
experimental: ExperimentalConfig
|
||||
model: ModelConfig
|
||||
maim_message: MaimMessageConfig
|
||||
lpmm_knowledge: LPMMKnowledgeConfig
|
||||
tool: ToolConfig
|
||||
@@ -635,11 +611,48 @@ class Config(ConfigBase):
|
||||
custom_prompt: CustomPromptConfig
|
||||
voice: VoiceConfig
|
||||
|
||||
|
||||
@dataclass
|
||||
class APIAdapterConfig(ConfigBase):
|
||||
"""API Adapter配置类"""
|
||||
|
||||
models: List[ModelInfo]
|
||||
"""模型列表"""
|
||||
|
||||
model_task_config: ModelTaskConfig
|
||||
"""模型任务配置"""
|
||||
|
||||
api_providers: List[APIProvider] = field(default_factory=list)
|
||||
"""API提供商列表"""
|
||||
|
||||
def __post_init__(self):
|
||||
self.api_providers_dict = {provider.name: provider for provider in self.api_providers}
|
||||
self.models_dict = {model.name: model for model in self.models}
|
||||
|
||||
def get_model_info(self, model_name: str) -> ModelInfo:
|
||||
"""根据模型名称获取模型信息"""
|
||||
if not model_name:
|
||||
raise ValueError("模型名称不能为空")
|
||||
if model_name not in self.models_dict:
|
||||
raise KeyError(f"模型 '{model_name}' 不存在")
|
||||
return self.models_dict[model_name]
|
||||
|
||||
def get_provider(self, provider_name: str) -> APIProvider:
|
||||
"""根据提供商名称获取API提供商信息"""
|
||||
if not provider_name:
|
||||
raise ValueError("API提供商名称不能为空")
|
||||
if provider_name not in self.api_providers_dict:
|
||||
raise KeyError(f"API提供商 '{provider_name}' 不存在")
|
||||
return self.api_providers_dict[provider_name]
|
||||
|
||||
|
||||
def load_config(config_path: str) -> Config:
|
||||
"""
|
||||
加载配置文件
|
||||
:param config_path: 配置文件路径
|
||||
:return: Config对象
|
||||
Args:
|
||||
config_path: 配置文件路径
|
||||
Returns:
|
||||
Config对象
|
||||
"""
|
||||
# 读取配置文件
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
@@ -653,12 +666,24 @@ def load_config(config_path: str) -> Config:
|
||||
raise e
|
||||
|
||||
|
||||
def get_config_dir() -> str:
|
||||
def api_ada_load_config(config_path: str) -> APIAdapterConfig:
|
||||
"""
|
||||
获取配置目录
|
||||
:return: 配置目录路径
|
||||
加载API适配器配置文件
|
||||
Args:
|
||||
config_path: 配置文件路径
|
||||
Returns:
|
||||
APIAdapterConfig对象
|
||||
"""
|
||||
return CONFIG_DIR
|
||||
# 读取配置文件
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
config_data = tomlkit.load(f)
|
||||
|
||||
# 创建APIAdapterConfig对象
|
||||
try:
|
||||
return APIAdapterConfig.from_dict(config_data)
|
||||
except Exception as e:
|
||||
logger.critical("API适配器配置文件解析失败")
|
||||
raise e
|
||||
|
||||
|
||||
# 获取配置文件路径
|
||||
@@ -669,4 +694,4 @@ update_model_config()
|
||||
logger.info("正在品鉴配置文件...")
|
||||
global_config = load_config(config_path=os.path.join(CONFIG_DIR, "bot_config.toml"))
|
||||
model_config = api_ada_load_config(config_path=os.path.join(CONFIG_DIR, "model_config.toml"))
|
||||
logger.info("非常的新鲜,非常的美味!")
|
||||
logger.info("非常的新鲜,非常的美味!")
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import re
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Literal, Optional
|
||||
from typing import Literal, Optional
|
||||
|
||||
from src.config.config_base import ConfigBase
|
||||
from packaging.version import Version
|
||||
|
||||
"""
|
||||
须知:
|
||||
@@ -599,50 +598,3 @@ class LPMMKnowledgeConfig(ConfigBase):
|
||||
embedding_dimension: int = 1024
|
||||
"""嵌入向量维度,应该与模型的输出维度一致"""
|
||||
|
||||
@dataclass
|
||||
class ModelConfig(ConfigBase):
|
||||
"""模型配置类"""
|
||||
|
||||
model_max_output_length: int = 800 # 最大回复长度
|
||||
|
||||
utils: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""组件模型配置"""
|
||||
|
||||
utils_small: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""组件小模型配置"""
|
||||
|
||||
replyer_1: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""normal_chat首要回复模型模型配置"""
|
||||
|
||||
replyer_2: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""normal_chat次要回复模型配置"""
|
||||
|
||||
memory: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""记忆模型配置"""
|
||||
|
||||
emotion: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""情绪模型配置"""
|
||||
|
||||
vlm: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""视觉语言模型配置"""
|
||||
|
||||
voice: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""语音识别模型配置"""
|
||||
|
||||
tool_use: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""专注工具使用模型配置"""
|
||||
|
||||
planner: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""规划模型配置"""
|
||||
|
||||
embedding: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""嵌入模型配置"""
|
||||
|
||||
lpmm_entity_extract: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""LPMM实体提取模型配置"""
|
||||
|
||||
lpmm_rdf_build: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""LPMM RDF构建模型配置"""
|
||||
|
||||
lpmm_qa: dict[str, Any] = field(default_factory=lambda: {})
|
||||
"""LPMM问答模型配置"""
|
||||
|
||||
Reference in New Issue
Block a user