import os from fastapi import FastAPI from fastapi.templating import Jinja2Templates from fastapi.middleware.cors import CORSMiddleware from app.core.config import settings from app.api.routes import register_routes from app.services.qa_service import QAService from app.core.logger import logger def create_app() -> FastAPI: """创建并配置FastAPI应用实例""" logger.info("Starting application initialization...") app = FastAPI( title=settings.APP_NAME, description=settings.APP_DESCRIPTION, version=settings.APP_VERSION, docs_url="/docs", redoc_url="/redoc" ) logger.info("Configuring CORS middleware...") # 配置CORS app.add_middleware( CORSMiddleware, allow_origins=settings.CORS_ORIGINS, allow_credentials=settings.CORS_CREDENTIALS, allow_methods=settings.CORS_METHODS, allow_headers=settings.CORS_HEADERS, ) logger.info("Setting up templates...") # 配置模板 templates = Jinja2Templates(directory=os.path.join(os.path.dirname(__file__), 'templates')) app.state.templates = templates logger.info(f"Initializing QA service with model: {settings.DEFAULT_MODEL}") # 初始化服务 qa_service = QAService( model_name=settings.DEFAULT_MODEL, temperature=settings.TEMPERATURE ) logger.info("Registering routes...") # 注册路由 register_routes(app, qa_service) logger.info("Application initialization completed successfully!") return app