Files
MuMuAINovel/backend/app/config.py
T
xiamuceer 1cde345ed9 1.优化AI请求替换OpenAI SDK调用,使用httpx和自定义头请求,避免触发部分公益站的cloudflare
2.修复deepseek模型调用问题,舍弃思考过程AI响应内容,只获取结果内容
3.新增会话过期机制,更新后添加到.env中
4.支持用户在生成章节内容时设置字数
2025-11-03 15:28:51 +08:00

95 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""应用配置管理"""
from pydantic_settings import BaseSettings
from typing import Optional
from pathlib import Path
import logging
# 获取项目根目录(从backend/app/config.py向上两级)
PROJECT_ROOT = Path(__file__).parent.parent
DATA_DIR = PROJECT_ROOT / "data"
DATA_DIR.mkdir(exist_ok=True)
# 配置模块使用标准logging(在logger.py初始化之前)
config_logger = logging.getLogger(__name__)
# 数据库文件路径(绝对路径)
DB_FILE = DATA_DIR / "ai_story.db"
# 生成数据库URL(在类外部生成,确保使用绝对路径)
# 将Windows反斜杠转换为正斜杠,SQLite URL格式要求
DATABASE_URL = f"sqlite+aiosqlite:///{str(DB_FILE.absolute()).replace(chr(92), '/')}"
config_logger.debug(f"数据库文件路径: {DB_FILE}")
config_logger.debug(f"数据库URL: {DATABASE_URL}")
class Settings(BaseSettings):
"""应用配置"""
# 应用配置
app_name: str = "MuMuAINovel"
app_version: str = "1.0.0"
app_host: str = "0.0.0.0"
app_port: int = 8000
debug: bool = True
# 日志配置
log_level: str = "INFO" # DEBUG, INFO, WARNING, ERROR, CRITICAL
log_to_file: bool = True # 是否输出到文件
log_file_path: str = str(PROJECT_ROOT / "logs" / "app.log")
log_max_bytes: int = 10 * 1024 * 1024 # 10MB
log_backup_count: int = 30 # 保留30个备份文件
# CORS配置
cors_origins: list[str] = ["http://localhost:8000", "http://127.0.0.1:8000"]
# 数据库配置 - 使用预先计算好的绝对路径URL
database_url: str = DATABASE_URL
# AI服务配置
openai_api_key: Optional[str] = None
openai_base_url: Optional[str] = None
gemini_api_key: Optional[str] = None
gemini_base_url: Optional[str] = None
anthropic_api_key: Optional[str] = None
anthropic_base_url: Optional[str] = None
default_ai_provider: str = "openai"
default_model: str = "gpt-4"
default_temperature: float = 0.7
default_max_tokens: int = 2000
# LinuxDO OAuth2 配置
LINUXDO_CLIENT_ID: Optional[str] = None
LINUXDO_CLIENT_SECRET: Optional[str] = None
# 回调地址:Docker部署时必须使用实际域名或服务器IP,不能使用localhost
# 本地开发: http://localhost:8000/api/auth/callback
# 生产环境: https://your-domain.com/api/auth/callback 或 http://your-ip:8000/api/auth/callback
LINUXDO_REDIRECT_URI: Optional[str] = None
# 前端URL配置(用于OAuth回调后重定向)
# 本地开发: http://localhost:8000
# 生产环境: https://your-domain.com 或 http://your-ip:8000
FRONTEND_URL: str = "http://localhost:8000"
# 初始管理员配置(LinuxDO user_id
INITIAL_ADMIN_LINUXDO_ID: Optional[str] = None
# 本地账户登录配置
LOCAL_AUTH_ENABLED: bool = True # 是否启用本地账户登录
LOCAL_AUTH_USERNAME: Optional[str] = None # 本地登录用户名
LOCAL_AUTH_PASSWORD: Optional[str] = None # 本地登录密码
LOCAL_AUTH_DISPLAY_NAME: str = "本地用户" # 本地用户显示名称
# 会话配置
SESSION_EXPIRE_MINUTES: int = 120 # 会话过期时间(分钟),默认2小时
SESSION_REFRESH_THRESHOLD_MINUTES: int = 30 # 会话刷新阈值(分钟),剩余时间少于此值时可刷新
class Config:
env_file = ".env"
case_sensitive = False
# 创建全局配置实例
settings = Settings()
config_logger.info(f"配置加载完成: {settings.app_name} v{settings.app_version}")
config_logger.debug(f"调试模式: {settings.debug}")
config_logger.debug(f"AI提供商: {settings.default_ai_provider}")