120 lines
4.5 KiB
Python
120 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
配置加载器 - 从nanobot主配置读取模型设置
|
|
这样当nanobot配置更改时,项目会自动跟随
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
class ConfigLoader:
|
|
"""配置加载器 - 优先从nanobot主配置读取"""
|
|
|
|
def __init__(self):
|
|
self.nanobot_config_path = Path.home() / ".nanobot" / "config.json"
|
|
self.project_config_path = Path("/home/ubuntu/nanocode/it_automation_enhanced/config/config.json")
|
|
|
|
def load_nanobot_config(self):
|
|
"""加载nanobot主配置"""
|
|
try:
|
|
if self.nanobot_config_path.exists():
|
|
with open(self.nanobot_config_path, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
except Exception as e:
|
|
print(f"警告: 无法加载nanobot配置: {e}")
|
|
return {}
|
|
|
|
def load_project_config(self):
|
|
"""加载项目配置"""
|
|
try:
|
|
if self.project_config_path.exists():
|
|
with open(self.project_config_path, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
except Exception as e:
|
|
print(f"警告: 无法加载项目配置: {e}")
|
|
return {}
|
|
|
|
def get_model_config(self):
|
|
"""
|
|
获取模型配置
|
|
优先从nanobot配置读取,如果不存在则使用项目配置
|
|
"""
|
|
nanobot_config = self.load_nanobot_config()
|
|
project_config = self.load_project_config()
|
|
|
|
# 从nanobot配置读取默认模型设置
|
|
nanobot_defaults = nanobot_config.get('agents', {}).get('defaults', {})
|
|
nanobot_providers = nanobot_config.get('providers', {})
|
|
|
|
provider_name = nanobot_defaults.get('provider', 'moonshot')
|
|
model_name = nanobot_defaults.get('model', 'kimi-k2.5')
|
|
|
|
# 获取该提供商的API配置
|
|
provider_config = nanobot_providers.get(provider_name, {})
|
|
api_key = provider_config.get('apiKey', '')
|
|
api_base = provider_config.get('apiBase', '')
|
|
|
|
# 构建配置字典
|
|
config = {
|
|
'provider': provider_name,
|
|
'model': model_name,
|
|
'api_key': api_key,
|
|
'api_base': api_base,
|
|
'max_tokens': nanobot_defaults.get('maxTokens', 8192),
|
|
'context_window': nanobot_defaults.get('contextWindowTokens', 65536),
|
|
'temperature': nanobot_defaults.get('temperature', 0.7),
|
|
# 从项目配置读取提示词模板
|
|
'translation_prompt': project_config.get(provider_name, {}).get('translation_prompt',
|
|
"请将以下英文技术文章翻译成中文,保持技术准确性,语言自然流畅:\n\n{content}"),
|
|
'summarization_prompt': project_config.get(provider_name, {}).get('summarization_prompt',
|
|
"请用中文总结以下技术文章的核心内容,突出技术要点和创新点:\n\n{content}")
|
|
}
|
|
|
|
return config
|
|
|
|
def get_api_endpoint(self, provider=None):
|
|
"""获取API端点"""
|
|
if provider is None:
|
|
config = self.get_model_config()
|
|
provider = config['provider']
|
|
|
|
# 默认端点
|
|
endpoints = {
|
|
'moonshot': 'https://api.moonshot.cn/v1/chat/completions',
|
|
'deepseek': 'https://api.deepseek.com/chat/completions',
|
|
'openai': 'https://api.openai.com/v1/chat/completions',
|
|
'azureOpenai': None, # 需要从配置读取
|
|
}
|
|
|
|
return endpoints.get(provider, endpoints['moonshot'])
|
|
|
|
# 全局配置加载器实例
|
|
_config_loader = None
|
|
|
|
def get_config_loader():
|
|
"""获取全局配置加载器实例"""
|
|
global _config_loader
|
|
if _config_loader is None:
|
|
_config_loader = ConfigLoader()
|
|
return _config_loader
|
|
|
|
def get_current_model_config():
|
|
"""获取当前模型配置的便捷函数"""
|
|
return get_config_loader().get_model_config()
|
|
|
|
if __name__ == "__main__":
|
|
# 测试配置加载
|
|
print("=== 配置加载器测试 ===")
|
|
loader = ConfigLoader()
|
|
config = loader.get_model_config()
|
|
|
|
print(f"\n当前模型配置:")
|
|
print(f" 提供商: {config['provider']}")
|
|
print(f" 模型: {config['model']}")
|
|
print(f" API密钥: {config['api_key'][:20]}..." if config['api_key'] else " API密钥: 未配置")
|
|
print(f" API端点: {config['api_base']}")
|
|
print(f" 最大Token: {config['max_tokens']}")
|
|
print(f" 上下文窗口: {config['context_window']}")
|
|
|
|
print(f"\nAPI调用端点: {loader.get_api_endpoint()}") |