#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2025/3/29 08:49 # @Author : old-tom # @File : llm_config # @Project : llmFunctionCallDemo # @Desc : llm配置文件解析 import os from pydantic import BaseModel import toml # 默认配置文件名 DEFAULT_CONF_NAME = 'env.toml' path = os.path.dirname(__file__) path = os.path.dirname(path) # 默认配置文件位置 DEFAULT_CONF_PATH = os.path.join(path, DEFAULT_CONF_NAME) class ConfigNotFoundError(Exception): """ 配置不存在异常 """ def __init__(self, msg): Exception.__init__(self, msg) class LLMConf(BaseModel): api_key: str model: str base_url: str max_tokens: int temperature: float streaming: bool = True class LLMConfigLoader(object): @staticmethod def load(item_name, conf_path: str = DEFAULT_CONF_PATH) -> LLMConf: """ 校验并加载配置 :return: """ if not os.path.isfile(conf_path): raise ConfigNotFoundError(f'模型配置文件{DEFAULT_CONF_NAME}不存在') conf = toml.load(conf_path) return LLMConf(**conf[item_name])