优化统一视觉模型的配置加载,修正模型类型引用,更新文档以包含配置验证工具说明

This commit is contained in:
leoleexh
2025-06-17 21:52:29 +08:00
parent 2c6ad307fb
commit dbc4db2ef0
4 changed files with 134 additions and 18 deletions
+16 -17
View File
@@ -6,6 +6,8 @@ import torch
from PIL import Image
import io
import base64
# 导入统一的配置加载函数
from .settings import load_settings
class LLMs_Vision_Unified:
"""统一的视觉模型节点"""
@@ -14,14 +16,14 @@ class LLMs_Vision_Unified:
self.config = self._load_config()
self.model_lists = {}
# 预先加载所有模型类型的子模型列表
for model_type in self.config['chatllmleoleexh']['vision_models']:
self.model_lists[model_type] = self.config['chatllmleoleexh']['vision_models'][model_type]['model_list']
for model_type in self.config['vision_models']:
self.model_lists[model_type] = self.config['vision_models'][model_type]['model_list']
@classmethod
def INPUT_TYPES(cls):
"""定义节点输入类型"""
config = cls._load_config_static()
vision_models = config['chatllmleoleexh']['vision_models']
vision_models = config['vision_models']
# 获取所有模型类型
model_types = list(vision_models.keys())
@@ -45,22 +47,19 @@ class LLMs_Vision_Unified:
@classmethod
def _load_config_static(cls):
"""静态方法加载配置"""
config_path = os.path.join(folder_paths.base_path, "custom_nodes", "ComfyUI-LLMs", "settings.yaml")
if not os.path.exists(config_path):
"""静态方法加载配置 - 使用统一的配置加载函数"""
try:
return load_settings()
except Exception:
# 如果加载失败,返回默认配置
return {
"chatllmleoleexh": {
"vision_models": {
"openai": {"model_list": ["gpt-4-vision-preview"]},
"glm4": {"model_list": ["glm-4v"]},
"ali": {"model_list": ["qwen-vl-plus"]},
"gemini": {"model_list": ["gemini-pro-vision"]}
}
"vision_models": {
"openai": {"model_list": ["gpt-4-vision-preview"]},
"glm4": {"model_list": ["glm-4v"]},
"ali": {"model_list": ["qwen-vl-plus"]},
"gemini": {"model_list": ["gemini-pro-vision"]}
}
}
with open(config_path, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
RETURN_TYPES = ("STRING",)
FUNCTION = "process_image"
@@ -73,7 +72,7 @@ class LLMs_Vision_Unified:
def _get_vision_config(self, model_type):
"""获取指定视觉模型的配置"""
try:
return self.config['chatllmleoleexh']['vision_models'][model_type]
return self.config['vision_models'][model_type]
except KeyError:
raise ValueError(f"未找到模型类型 {model_type} 的配置")
+29 -1
View File
@@ -44,6 +44,20 @@ cp settings.yaml.sample settings.yaml
2. Edit `settings.yaml` to configure your API keys and model settings
### Configuration Validation
After setting up your configuration, you can use the validation tool to check if everything is configured correctly:
```bash
python validate_config.py
```
This tool will:
- ✅ Check if `settings.yaml` exists
- ✅ Validate configuration file structure
- ✅ Display current API endpoints and model configurations
- ⚠️ Warn about default API keys that need to be updated
### Detailed Configuration
The configuration file structure is as follows:
@@ -196,6 +210,20 @@ cp settings.yaml.sample settings.yaml
2. 编辑 `settings.yaml` 文件,配置您的API密钥和模型设置
### Configuration Validation
After setting up your configuration, you can use the validation tool to check if everything is configured correctly:
```bash
python validate_config.py
```
This tool will:
- ✅ Check if `settings.yaml` exists
- ✅ Validate configuration file structure
- ✅ Display current API endpoints and model configurations
- ⚠️ Warn about default API keys that need to be updated
### 详细配置说明
配置文件结构如下:
@@ -279,7 +307,7 @@ chatllmleoleexh:
### 图像理解功能
1. 在节点列表中找到 `🎯 LLMs Vision | 图像理解`
2. 选择���使用的视觉模型
2. 选择使用的视觉模型
3. 连接图像输入
4. 运行获取图像描述
+8
View File
@@ -30,6 +30,14 @@ DEFAULT_SETTINGS = {
"api_key": "AIxxxxxx",
"model_list": ["gemini-pro-vision"]
}
},
"example_user_prompt": "your user prompt here",
"prompt_templates": {
"default": {
"system": "act as prompt generator, I will give you text and you describe an image that matches that text in details, answer with one response only.if I input in Chinese to communicate with you, but it is crucial that your response be in English.",
"prefix": "make a list of the things you see. do not explain why you see them.",
"suffix": "describe what you see in your mind."
}
}
}
}
+81
View File
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
配置验证脚本
用于检查 settings.yaml 文件是否正确配置
"""
import os
import sys
from settings import load_settings, get_chat_settings, get_vision_settings
def validate_config():
"""验证配置文件"""
print("🔍 正在验证配置文件...")
try:
# 1. 检查配置文件是否存在
config_path = os.path.join(os.path.dirname(__file__), "settings.yaml")
if not os.path.exists(config_path):
print("❌ 错误: settings.yaml 文件不存在")
print("💡 请先运行: cp settings.yaml.sample settings.yaml")
return False
# 2. 测试配置加载
config = load_settings()
print("✅ 配置文件加载成功")
# 3. 验证必需的配置节
required_sections = ['openai_compatible', 'vision_models', 'example_user_prompt']
for section in required_sections:
if section not in config:
print(f"❌ 错误: 缺少必需的配置节 '{section}'")
return False
print("✅ 所有必需的配置节都存在")
# 4. 验证聊天配置
chat_config = get_chat_settings()
print(f"📝 聊天配置:")
print(f" - API端点: {chat_config['api_base']}")
print(f" - API密钥: {chat_config['api_key'][:10]}...")
print(f" - 可用模型数量: {len(chat_config['model'])}")
# 5. 验证视觉模型配置
vision_types = ['openai', 'glm4', 'ali', 'gemini']
print(f"🎯 视觉模型配置:")
for vtype in vision_types:
vision_config = get_vision_settings(vtype)
if vision_config:
print(f" - {vtype}: {len(vision_config['model_list'])} 个模型")
else:
print(f" - {vtype}: 未配置")
# 6. 检查API密钥是否为默认值
if chat_config['api_key'] == 'sk-xxxxxxx':
print("⚠️ 警告: 聊天API密钥仍为默认值,请更新为实际密钥")
for vtype in vision_types:
vision_config = get_vision_settings(vtype)
if vision_config and vision_config.get('api_key', '').startswith(('sk-xxxxx', 'xxxxxx', 'AIxxxxxx')):
print(f"⚠️ 警告: {vtype} API密钥仍为默认值,请更新为实际密钥")
print("\n🎉 配置验证完成!")
return True
except Exception as e:
print(f"❌ 配置验证失败: {str(e)}")
return False
def main():
"""主函数"""
print("ComfyUI-LLMs 配置验证工具")
print("=" * 40)
if validate_config():
print("✅ 配置验证通过,您的设置文件已正确配置!")
else:
print("❌ 配置验证失败,请检查并修正配置文件")
sys.exit(1)
if __name__ == "__main__":
main()