Files
assist/LLM配置统一说明.md

3.2 KiB
Raw Blame History

LLM配置统一管理说明

? 概述

本项目已将LLM配置统一管理确保整个项目只在一个地方配置千问模型所有地方都从统一配置获取。

?? 配置架构

1. 核心配置文件:config/llm_config.py

这是唯一的LLM配置源定义了千问模型的所有配置

QWEN_CONFIG = LLMConfig(
    provider="qwen",
    api_key="sk-c0dbefa1718d46eaa897199135066f00",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-plus-latest",
    temperature=0.7,
    max_tokens=2000
)

# 默认使用千问模型
DEFAULT_CONFIG = QWEN_CONFIG

2. 统一配置管理器:src/config/unified_config.py

统一配置管理器在初始化时自动从 config/llm_config.py 加载配置:

def _load_default_llm_config(self) -> LLMConfig:
    """加载默认LLM配置"""
    try:
        from config.llm_config import DEFAULT_CONFIG
        # 转换配置格式
        return LLMConfig(...)
    except Exception as e:
        logger.warning(f"无法加载默认LLM配置使用内置默认值: {e}")
        return LLMConfig()

3. 全局配置实例

通过 get_config() 函数获取全局配置实例:

from src.config.unified_config import get_config

config = get_config()
llm_config = config.llm  # 获取LLM配置

? 如何使用

在任何需要使用LLM的地方

from src.config.unified_config import get_config

# 获取LLM配置
llm_config = get_config().llm

# 使用配置
print(f"Provider: {llm_config.provider}")
print(f"Model: {llm_config.model}")
print(f"API Key: {llm_config.api_key}")

示例AI建议服务

class AISuggestionService:
    def __init__(self):
        # 从统一配置管理器获取LLM配置
        self.llm_config = get_config().llm
        logger.info(f"使用LLM配置: {self.llm_config.provider} - {self.llm_config.model}")

? 配置优先级

  1. 第一优先级:统一配置管理器中的配置(可通过配置文件或环境变量设置)
  2. 第二优先级config/llm_config.py 中的 DEFAULT_CONFIG
  3. 最后备选:内置的默认值

? 修改配置

方法1修改配置文件推荐

直接编辑 config/llm_config.py修改API密钥或模型

QWEN_CONFIG = LLMConfig(
    provider="qwen",
    api_key="你的新API密钥",  # 修改这里
    model="qwen-max",  # 或修改模型
    ...
)

方法2通过统一配置文件

编辑 config/unified_config.json(如果存在):

{
  "llm": {
    "provider": "qwen",
    "api_key": "你的新API密钥",
    "model": "qwen-plus-latest",
    ...
  }
}

方法3环境变量可选

export LLM_API_KEY="你的API密钥"
export LLM_MODEL="qwen-plus-latest"

? 优势

  1. 单一配置源:只需要在 config/llm_config.py 配置一次
  2. 统一管理:所有模块都通过统一配置管理器获取
  3. 易于维护:修改配置不需要修改多处代码
  4. 自动同步:修改配置后,所有使用该配置的地方自动更新
  5. 向后兼容保留fallback机制确保系统稳定运行

? 已更新的文件

  • ? config/llm_config.py - 添加了 get_default_llm_config() 函数
  • ? src/config/unified_config.py - 从 config/llm_config.py 加载默认配置
  • ? src/integrations/ai_suggestion_service.py - 使用统一配置
  • ? src/agent/agent_assistant_core.py - 使用统一配置

? 注意事项

  • 不要在代码中硬编码OpenAI或其他模型的配置
  • 不要直接从 config/llm_config.py 导入除非作为fallback
  • 总是通过 get_config().llm 获取配置
  • 修改配置后,请重启应用使配置生效