You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.6 KiB
88 lines
2.6 KiB
3 weeks ago
|
import json
|
||
|
import logging
|
||
|
import os
|
||
|
import warnings
|
||
|
from logging.handlers import RotatingFileHandler
|
||
|
|
||
|
import fastapi
|
||
|
import uvicorn
|
||
|
from fastapi import FastAPI
|
||
|
from sse_starlette import EventSourceResponse
|
||
|
from starlette.staticfiles import StaticFiles
|
||
|
|
||
|
from Util.LightRagUtil import initialize_rag
|
||
|
from lightrag import QueryParam
|
||
|
|
||
|
# 初始化日志
|
||
|
logger = logging.getLogger(__name__)
|
||
|
logger.setLevel(logging.INFO)
|
||
|
|
||
|
# 配置日志处理器
|
||
|
log_file = os.path.join(os.path.dirname(__file__), 'Logs', 'app.log')
|
||
|
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
||
|
|
||
|
# 文件处理器
|
||
|
file_handler = RotatingFileHandler(
|
||
|
log_file, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
|
||
|
file_handler.setFormatter(logging.Formatter(
|
||
|
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
||
|
|
||
|
# 控制台处理器
|
||
|
console_handler = logging.StreamHandler()
|
||
|
console_handler.setFormatter(logging.Formatter(
|
||
|
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
||
|
|
||
|
logger.addHandler(file_handler)
|
||
|
logger.addHandler(console_handler)
|
||
|
|
||
|
|
||
|
async def lifespan(app: FastAPI):
|
||
|
# 抑制HTTPS相关警告
|
||
|
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
|
||
|
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
|
||
3 weeks ago
|
|
||
|
# 初始化RAG
|
||
|
app.state.rag = await initialize_rag(working_dir="./Test/Math")
|
||
3 weeks ago
|
yield
|
||
3 weeks ago
|
|
||
|
# 清理资源
|
||
|
await app.state.rag.finalize_storages()
|
||
3 weeks ago
|
|
||
3 weeks ago
|
|
||
3 weeks ago
|
async def print_stream(stream):
|
||
|
async for chunk in stream:
|
||
|
if chunk:
|
||
|
print(chunk, end="", flush=True)
|
||
3 weeks ago
|
|
||
|
|
||
3 weeks ago
|
app = FastAPI(lifespan=lifespan)
|
||
|
|
||
|
# 挂载静态文件目录
|
||
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
||
|
|
||
3 weeks ago
|
|
||
3 weeks ago
|
@app.post("/api/rag")
|
||
3 weeks ago
|
async def rag(request: fastapi.Request):
|
||
|
data = await request.json()
|
||
3 weeks ago
|
query = data.get("query")
|
||
3 weeks ago
|
|
||
3 weeks ago
|
async def generate_response_stream(query: str):
|
||
3 weeks ago
|
try:
|
||
3 weeks ago
|
resp = await request.app.state.rag.aquery(
|
||
3 weeks ago
|
query=query,
|
||
|
param=QueryParam(mode="hybrid", stream=True))
|
||
3 weeks ago
|
|
||
3 weeks ago
|
async for chunk in resp:
|
||
3 weeks ago
|
if not chunk:
|
||
3 weeks ago
|
continue
|
||
3 weeks ago
|
yield f"data: {json.dumps({'reply': chunk})}\n\n"
|
||
|
print(chunk, end='', flush=True)
|
||
3 weeks ago
|
except Exception as e:
|
||
|
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
||
|
|
||
3 weeks ago
|
return EventSourceResponse(generate_response_stream(query=query))
|
||
3 weeks ago
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|