- 实现Agent管理,支持AI辅助生成系统提示词 - 支持多个AI提供商(OpenRouter、智谱、MiniMax等) - 实现聊天室和讨论引擎 - WebSocket实时消息推送 - 前端使用React + Ant Design - 后端使用FastAPI + MongoDB Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
74 lines
1.8 KiB
Python
74 lines
1.8 KiB
Python
"""
|
|
AI聊天室后端主入口
|
|
FastAPI应用启动文件
|
|
"""
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
from loguru import logger
|
|
|
|
from config import settings
|
|
from database.connection import connect_db, close_db
|
|
from routers import providers, agents, chatrooms, discussions
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""
|
|
应用生命周期管理
|
|
启动时连接数据库,关闭时断开连接
|
|
"""
|
|
logger.info("正在启动AI聊天室服务...")
|
|
await connect_db()
|
|
logger.info("数据库连接成功")
|
|
yield
|
|
logger.info("正在关闭AI聊天室服务...")
|
|
await close_db()
|
|
logger.info("服务已关闭")
|
|
|
|
|
|
# 创建FastAPI应用
|
|
app = FastAPI(
|
|
title="AI聊天室",
|
|
description="多Agent协作讨论平台",
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# 配置CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册路由
|
|
app.include_router(providers.router, prefix="/api/providers", tags=["AI接口管理"])
|
|
app.include_router(agents.router, prefix="/api/agents", tags=["Agent管理"])
|
|
app.include_router(chatrooms.router, prefix="/api/chatrooms", tags=["聊天室管理"])
|
|
app.include_router(discussions.router, prefix="/api/discussions", tags=["讨论结果"])
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""根路径健康检查"""
|
|
return {"message": "AI聊天室服务运行中", "version": "1.0.0"}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""健康检查接口"""
|
|
return {"status": "healthy"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(
|
|
"main:app",
|
|
host=settings.HOST,
|
|
port=settings.PORT,
|
|
reload=settings.DEBUG
|
|
)
|