|
|
@ -37,23 +37,25 @@ logger.addHandler(file_handler)
|
|
|
|
logger.addHandler(console_handler)
|
|
|
|
logger.addHandler(console_handler)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
# 抑制HTTPS相关警告
|
|
|
|
# 抑制HTTPS相关警告
|
|
|
|
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
|
|
|
|
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')
|
|
|
|
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
|
|
|
|
yield
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def print_stream(stream):
|
|
|
|
async def print_stream(stream):
|
|
|
|
async for chunk in stream:
|
|
|
|
async for chunk in stream:
|
|
|
|
if chunk:
|
|
|
|
if chunk:
|
|
|
|
print(chunk, end="", flush=True)
|
|
|
|
print(chunk, end="", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
|
|
|
|
|
|
# 挂载静态文件目录
|
|
|
|
# 挂载静态文件目录
|
|
|
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
|
|
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/rag")
|
|
|
|
@app.post("/api/rag")
|
|
|
|
async def rag(request: fastapi.Request):
|
|
|
|
async def rag(request: fastapi.Request):
|
|
|
|
data = await request.json()
|
|
|
|
data = await request.json()
|
|
|
@ -63,15 +65,14 @@ async def rag(request: fastapi.Request):
|
|
|
|
async def generate_response_stream(query: str):
|
|
|
|
async def generate_response_stream(query: str):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
print("Initializing RAG...")
|
|
|
|
print("Initializing RAG...")
|
|
|
|
rag = await initialize_rag(working_dir="./Test/Math")
|
|
|
|
rag = await initialize_rag(working_dir=working_dir)
|
|
|
|
print("RAG initialized, starting query...")
|
|
|
|
print("RAG initialized, starting query...")
|
|
|
|
resp = await rag.aquery(
|
|
|
|
resp = await rag.aquery(
|
|
|
|
query=query,
|
|
|
|
query=query,
|
|
|
|
param=QueryParam(mode="hybrid", stream=True))
|
|
|
|
param=QueryParam(mode="hybrid", stream=True))
|
|
|
|
print("Query started, streaming response...")
|
|
|
|
print("Query started, streaming response...")
|
|
|
|
|
|
|
|
|
|
|
|
async for chunk in resp:
|
|
|
|
async for chunk in resp:
|
|
|
|
#print(f"Processing chunk: {chunk[:50]}...") # 打印前50个字符
|
|
|
|
|
|
|
|
if not chunk:
|
|
|
|
if not chunk:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|