153 lines
4.0 KiB
Python
153 lines
4.0 KiB
Python
"""
|
||
数据接入服务配置管理
|
||
"""
|
||
|
||
import os
|
||
from typing import List, Optional
|
||
from pydantic_settings import BaseSettings
|
||
|
||
|
||
class Settings(BaseSettings):
|
||
"""数据接入服务配置"""
|
||
|
||
# 应用设置
|
||
app_name: str = "taiji-AI-PAD 数据接入服务"
|
||
debug: bool = False
|
||
|
||
# Redis设置
|
||
redis_url: str = os.getenv("REDIS_URL", "redis://redis:6379")
|
||
redis_max_connections: int = 20
|
||
|
||
# NATS设置
|
||
nats_url: str = os.getenv("NATS_URL", "nats://nats:4222")
|
||
|
||
# RapidAPI设置
|
||
rapidapi_key: str = os.getenv("RAPIDAPI_KEY", "")
|
||
rapidapi_host: str = os.getenv("RAPIDAPI_HOST", "rapidapi.com")
|
||
rapidapi_base_url: str = "https://rapidapi.com"
|
||
rapidapi_timeout: int = 30
|
||
rapidapi_rate_limit: int = 1000 # 每分钟请求数
|
||
|
||
# APILLAMA模型设置(使用OpenRouter API)
|
||
apillama_model_id: str = os.getenv(
|
||
"APILLAMA_MODEL_ID",
|
||
"meta-llama/llama-3.1-8b-instruct"
|
||
)
|
||
openrouter_api_key: str = os.getenv("OPENROUTER_API_KEY", "")
|
||
openrouter_base_url: str = os.getenv(
|
||
"OPENROUTER_BASE_URL",
|
||
"https://openrouter.ai/api/v1"
|
||
)
|
||
apillama_max_tokens: int = 2048
|
||
apillama_temperature: float = 0.3
|
||
apillama_top_p: float = 0.9
|
||
|
||
# 缓存设置
|
||
cache_dir: str = "/app/cache"
|
||
cache_ttl: int = 3600 # 秒
|
||
max_cache_size: int = 1000 # MB
|
||
|
||
# OpenAPI解析设置
|
||
openapi_timeout: int = 60
|
||
openapi_max_size: int = 10 * 1024 * 1024 # 10MB
|
||
supported_openapi_versions: List[str] = ["2.0", "3.0", "3.1"]
|
||
|
||
# 工具生成设置
|
||
max_tools_per_api: int = 50
|
||
tool_name_max_length: int = 100
|
||
tool_description_max_length: int = 500
|
||
|
||
# API处理设置
|
||
max_concurrent_requests: int = 10
|
||
request_timeout: int = 30
|
||
retry_attempts: int = 3
|
||
retry_delay: float = 1.0
|
||
|
||
# 安全设置
|
||
allowed_domains: List[str] = [
|
||
"rapidapi.com",
|
||
"github.com",
|
||
"swagger.io",
|
||
"openapis.org"
|
||
]
|
||
blocked_domains: List[str] = []
|
||
|
||
# 监控设置
|
||
enable_metrics: bool = True
|
||
metrics_port: int = 8001
|
||
|
||
# 日志设置
|
||
log_level: str = "INFO"
|
||
log_format: str = "json"
|
||
log_file: Optional[str] = "/app/logs/data-ingestion.log"
|
||
|
||
# 并发设置
|
||
max_workers: int = 4
|
||
max_queue_size: int = 1000
|
||
|
||
# API限制设置
|
||
max_endpoints_per_spec: int = 200
|
||
max_parameters_per_endpoint: int = 50
|
||
max_response_schemas: int = 100
|
||
|
||
# 文件处理设置
|
||
temp_dir: str = "/tmp/taiji-data-ingestion"
|
||
max_file_size: int = 50 * 1024 * 1024 # 50MB
|
||
allowed_file_types: List[str] = [
|
||
"application/json",
|
||
"text/yaml",
|
||
"text/plain",
|
||
"application/yaml"
|
||
]
|
||
|
||
# 数据库设置(如果需要持久化)
|
||
database_url: Optional[str] = os.getenv("ASYNC_DATABASE_URL") or os.getenv("DATABASE_URL")
|
||
|
||
class Config:
|
||
env_file = ".env"
|
||
env_file_encoding = "utf-8"
|
||
case_sensitive = False
|
||
extra = "ignore"
|
||
|
||
|
||
class DevelopmentSettings(Settings):
|
||
"""开发环境配置"""
|
||
debug: bool = True
|
||
log_level: str = "DEBUG"
|
||
apillama_device: str = "cpu"
|
||
cache_ttl: int = 600 # 10分钟
|
||
|
||
|
||
class ProductionSettings(Settings):
|
||
"""生产环境配置"""
|
||
debug: bool = False
|
||
log_level: str = "INFO"
|
||
apillama_device: str = "cuda" # 如果有GPU
|
||
max_concurrent_requests: int = 50
|
||
max_workers: int = 8
|
||
|
||
|
||
class TestingSettings(Settings):
|
||
"""测试环境配置"""
|
||
debug: bool = True
|
||
redis_url: str = "redis://localhost:6379/1" # 使用不同的数据库
|
||
cache_ttl: int = 60 # 1分钟
|
||
rapidapi_key: str = "test-key"
|
||
|
||
|
||
def get_settings() -> Settings:
|
||
"""根据环境变量获取相应的配置"""
|
||
environment = os.getenv("ENVIRONMENT", "development").lower()
|
||
|
||
if environment == "production":
|
||
return ProductionSettings()
|
||
elif environment == "testing":
|
||
return TestingSettings()
|
||
else:
|
||
return DevelopmentSettings()
|
||
|
||
|
||
# 全局配置实例
|
||
settings = get_settings()
|
||
|