You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.3 KiB

5 months ago
#!/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)
def load_env():
if not os.path.isfile(DEFAULT_CONF_PATH):
raise ConfigNotFoundError(f'模型配置文件{DEFAULT_CONF_NAME}不存在')
return toml.load(DEFAULT_CONF_PATH)
conf = load_env()
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) -> LLMConf:
"""
校验并加载配置
:return:
"""
return LLMConf(**conf[item_name])
class BaseConf(BaseModel):
history_chat_store: str
similarity_threshold: float
debug: bool = False
verbose: bool = False
base_conf = BaseConf(**conf['base'])