Files
ragservice/app/main.py
T
2026-09-09 12:45:26 +08:00

91 lines
2.4 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import logging
import uvicorn
from app.config import settings
# from app.api.routes_questions import router as questions_router, knowledge_router
from app.api.routes_rag import router as rag_router
from app.core.embeddings import ollama_embeddings_adapter
from app.core.vector_store import get_vector_store_adapter
from app.core.llm_client import llm_client
# 配置日志
logging.basicConfig(
level=getattr(logging, settings.log_level.upper()),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# 初始化向量存储适配器(触发单例创建)
get_vector_store_adapter()
# 创建 FastAPI 应用
app = FastAPI(
title="Changzuo RAG Service",
description="Python FastAPI RAG 服务 - 向量检索、文档处理、RAG 流水线",
version="1.0.0"
)
# CORS 配置
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
# app.include_router(questions_router)
# app.include_router(knowledge_router)
app.include_router(rag_router)
@app.get("/health")
async def health_check():
"""健康检查端点:检查 ChromaDB、Ollama、LLM API 连通性"""
vector_store = get_vector_store_adapter()
chromadb_health = vector_store.health_check()
ollama_health = await ollama_embeddings_adapter.health_check()
llm_health = await llm_client.health_check()
all_healthy = all(
h.get("status") == "healthy"
for h in [chromadb_health, ollama_health, llm_health]
)
return {
"status": "healthy" if all_healthy else "degraded",
"service": "changzuo-rag-service",
"version": "1.0.0",
"components": {
"chromadb": chromadb_health,
"ollama": ollama_health,
"llm": llm_health
}
}
@app.get("/")
async def root():
return {
"service": "Changzuo RAG Service",
"version": "1.0.0",
"docs": "/docs"
}
@app.on_event("shutdown")
async def shutdown_event():
"""关闭时清理资源"""
logger.info("Service shutdown complete")
if __name__ == "__main__":
uvicorn.run(
"app.main:app",
host=settings.service_host,
port=settings.service_port,
reload=True
)