Files
2026-09-21 21:08:51 +08:00

105 lines
3.0 KiB
Python
Raw Permalink 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.
# ChromaDB 依赖 sqlite3 >= 3.35.0,部分 Linux 发行版自带版本过低
# 使用 pysqlite3-binary 提供的现代 SQLite 替代系统 sqlite3
# Windows 无 pysqlite3-binary wheel,回退系统 sqlite3(3.43.1 满足要求)
try:
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
except ImportError:
pass
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
from app.core.typesafe_client import typesafe_scorer
# 配置日志
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、TypeSafe 连通性"""
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()
typesafe_health = await typesafe_scorer.health_check()
# disabled 视为正常(可选组件被旁路),只有 unhealthy 才判为降级
all_healthy = all(
h.get("status") in ("healthy", "disabled")
for h in [chromadb_health, ollama_health, llm_health, typesafe_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,
"typesafe": typesafe_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
)