Files
ai-agent-server/app/main.py
T
2026-04-01 14:31:59 +08:00

48 lines
1.4 KiB
Python

import datetime
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_oauth2_redirect_html, get_redoc_html, get_swagger_ui_html
from fastapi.responses import RedirectResponse
from langchain.chat_models import init_chat_model
from langserve import add_routes
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from app.config.env import env
from app.controller.add_custom_stream_route import add_custom_stream_route
from app.controller.add_graph_proxy_route import add_graph_proxy_route
from app.routes import routes
from app.utils.get_local_ips import get_local_ips
from app.utils.llm_utils import create_llm
from app.utils.mysql_utils import check_database_connection
@asynccontextmanager
async def lifespan(app: FastAPI):
print(f"应用启动:{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
async_engine = await check_database_connection()
yield
print(f"应用销毁:{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
await async_engine.dispose()
app = FastAPI(
lifespan=lifespan,
docs_url=None, # 禁用默认 Swagger
redoc_url=None, # 禁用默认 ReDoc
)
for add_route_func in routes:
add_route_func(app)
if env.server_enable_cors:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)