|
|
|
@ -1,4 +1,6 @@
|
|
|
|
|
import json
|
|
|
|
|
import os.path
|
|
|
|
|
import shutil
|
|
|
|
|
import subprocess
|
|
|
|
|
import tempfile
|
|
|
|
|
import urllib
|
|
|
|
@ -17,7 +19,6 @@ from starlette.staticfiles import StaticFiles
|
|
|
|
|
from Util.LightRagUtil import *
|
|
|
|
|
from Util.PostgreSQLUtil import init_postgres_pool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 想更详细地控制日志输出
|
|
|
|
|
logger = logging.getLogger('lightrag')
|
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
@ -30,7 +31,6 @@ async def lifespan(app: FastAPI):
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
|
|
|
|
# 挂载静态文件目录
|
|
|
|
@ -98,10 +98,14 @@ async def rag(request: fastapi.Request):
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# 使用PG库后,这个是没有用的,但目前的项目代码要求必传,就写一个吧。
|
|
|
|
|
WORKING_DIR = f"./output"
|
|
|
|
|
WORKING_DIR = workspace
|
|
|
|
|
if os.path.exists(WORKING_DIR):
|
|
|
|
|
shutil.rmtree(WORKING_DIR)
|
|
|
|
|
os.makedirs(WORKING_DIR)
|
|
|
|
|
|
|
|
|
|
async def generate_response_stream(query: str):
|
|
|
|
|
try:
|
|
|
|
|
logger.info("workspace=" + workspace)
|
|
|
|
|
rag = await initialize_pg_rag(WORKING_DIR=WORKING_DIR, workspace=workspace)
|
|
|
|
|
resp = await rag.aquery(
|
|
|
|
|
query=query,
|
|
|
|
@ -314,7 +318,7 @@ async def get_sources(page: int = 1, limit: int = 10):
|
|
|
|
|
""",
|
|
|
|
|
limit, offset
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sources = [
|
|
|
|
|
{
|
|
|
|
|
"id": row[0],
|
|
|
|
@ -324,7 +328,7 @@ async def get_sources(page: int = 1, limit: int = 10):
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"code": 0,
|
|
|
|
|
"data": {
|
|
|
|
@ -357,7 +361,7 @@ async def get_articles(page: int = 1, limit: int = 10):
|
|
|
|
|
""",
|
|
|
|
|
limit, offset
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
articles = [
|
|
|
|
|
{
|
|
|
|
|
"id": row[0],
|
|
|
|
@ -369,7 +373,7 @@ async def get_articles(page: int = 1, limit: int = 10):
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"code": 0,
|
|
|
|
|
"data": {
|
|
|
|
|