abo/news_agent/config.py

85 lines
2.8 KiB
Python
Raw Normal View History

2025-07-16 15:34:54 +08:00
import os
from typing import Dict, Any
class Config:
"""新闻稿生成器配置文件"""
# 模型配置
MODELS = {
'openai': {
'name': 'OpenAI GPT-4',
'api_key': os.getenv('OPENAI_API_KEY', ''),
'model': 'gpt-4',
'base_url': 'https://api.openai.com/v1',
'max_tokens': 800,
'temperature': 0.7
},
'volcengine': {
'name': '火山引擎豆包',
'api_key': os.getenv('VOLCENGINE_API_KEY', '6840c164-3b7e-4ddc-9d51-20624defc205'),
'model': 'doubao-pro-4k',
'base_url': 'maas-api.ml-platform-cn-beijing.volces.com',
'max_tokens': 800,
'temperature': 0.7
},
'dashscope': {
'name': '阿里通义千问',
'api_key': os.getenv('DASHSCOPE_API_KEY', 'sk-29a937c25bff4752af88514525c329b3'),
'model': 'qwen-plus',
'base_url': 'https://dashscope.aliyuncs.com',
'max_tokens': 800,
'temperature': 0.7
}
}
# 文案风格配置
STYLES = ['正式', '简洁', '感性', '科技']
# Flask应用配置
FLASK_CONFIG = {
'debug': True,
'host': '127.0.0.1',
'port': 50001,
'secret_key': os.getenv('FLASK_SECRET_KEY', 'your-secret-key-here')
}
# 文档配置
DOCUMENT_CONFIG = {
'default_filename': 'news.docx',
'image_width': 4, # 图片宽度(英寸)
'title': '新闻稿'
}
@classmethod
def get_model_config(cls, model_type: str) -> Dict[str, Any]:
"""获取指定模型的配置"""
if model_type not in cls.MODELS:
raise ValueError(f"不支持的模型类型: {model_type}")
return cls.MODELS[model_type]
@classmethod
def validate_api_keys(cls) -> Dict[str, bool]:
"""验证所有API密钥是否已设置"""
validation_result = {}
for model_type, config in cls.MODELS.items():
validation_result[model_type] = bool(config['api_key'])
return validation_result
@classmethod
def get_available_models(cls) -> Dict[str, str]:
"""获取可用的模型列表已配置API密钥的模型"""
available = {}
for model_type, config in cls.MODELS.items():
if config['api_key']:
available[model_type] = config['name']
return available
@classmethod
def update_api_key(cls, model_type: str, api_key: str) -> None:
"""更新指定模型的API密钥"""
if model_type not in cls.MODELS:
raise ValueError(f"不支持的模型类型: {model_type}")
cls.MODELS[model_type]['api_key'] = api_key
# 创建配置实例
config = Config()