|
|
@ -36,17 +36,18 @@ app = FastAPI(lifespan=lifespan)
|
|
|
|
# 挂载静态文件目录
|
|
|
|
# 挂载静态文件目录
|
|
|
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
|
|
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 访问根的跳转
|
|
|
|
# 访问根的跳转
|
|
|
|
@app.get("/")
|
|
|
|
@app.get("/")
|
|
|
|
async def redirect_to_ai():
|
|
|
|
async def redirect_to_ai():
|
|
|
|
return fastapi.responses.RedirectResponse(url="/static/ai.html")
|
|
|
|
return fastapi.responses.RedirectResponse(url="/static/ai.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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()
|
|
|
|
logger.info(f"Received request: {data}")
|
|
|
|
|
|
|
|
workspace = data.get("topic", "ShiJi") # Chinese, Math ,ShiJi 默认是少年读史记
|
|
|
|
workspace = data.get("topic", "ShiJi") # Chinese, Math ,ShiJi 默认是少年读史记
|
|
|
|
mode = data.get("mode", "hybrid") # 默认为hybrid模式
|
|
|
|
logger.info("工作空间:" + workspace)
|
|
|
|
# 查询的问题
|
|
|
|
# 查询的问题
|
|
|
|
query = data.get("query")
|
|
|
|
query = data.get("query")
|
|
|
|
|
|
|
|
|
|
|
@ -108,9 +109,7 @@ async def rag(request: fastapi.Request):
|
|
|
|
rag = await initialize_pg_rag(WORKING_DIR=WORKING_DIR, workspace=workspace)
|
|
|
|
rag = await initialize_pg_rag(WORKING_DIR=WORKING_DIR, workspace=workspace)
|
|
|
|
resp = await rag.aquery(
|
|
|
|
resp = await rag.aquery(
|
|
|
|
query=query,
|
|
|
|
query=query,
|
|
|
|
param=QueryParam(mode=mode, stream=True, user_prompt=user_prompt))
|
|
|
|
param=QueryParam(mode="hybrid", stream=True, user_prompt=user_prompt))
|
|
|
|
# hybrid naive
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async for chunk in resp:
|
|
|
|
async for chunk in resp:
|
|
|
|
if not chunk:
|
|
|
|
if not chunk:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
@ -310,10 +309,11 @@ async def get_sources(page: int = 1, limit: int = 10):
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
rows = await conn.fetch(
|
|
|
|
rows = await conn.fetch(
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
SELECT id, account_id,account_name, created_at
|
|
|
|
SELECT id, account_id, account_name, created_at
|
|
|
|
FROM t_wechat_source
|
|
|
|
FROM t_wechat_source
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
LIMIT $1 OFFSET $2
|
|
|
|
LIMIT $1
|
|
|
|
|
|
|
|
OFFSET $2
|
|
|
|
""",
|
|
|
|
""",
|
|
|
|
limit, offset
|
|
|
|
limit, offset
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -352,11 +352,16 @@ async def get_articles(page: int = 1, limit: int = 10):
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
rows = await conn.fetch(
|
|
|
|
rows = await conn.fetch(
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
SELECT a.id, a.title, a.source as name,
|
|
|
|
SELECT a.id,
|
|
|
|
a.publish_time, a.collection_time,a.url
|
|
|
|
a.title,
|
|
|
|
|
|
|
|
a.source as name,
|
|
|
|
|
|
|
|
a.publish_time,
|
|
|
|
|
|
|
|
a.collection_time,
|
|
|
|
|
|
|
|
a.url
|
|
|
|
FROM t_wechat_articles a
|
|
|
|
FROM t_wechat_articles a
|
|
|
|
ORDER BY a.collection_time DESC
|
|
|
|
ORDER BY a.collection_time DESC
|
|
|
|
LIMIT $1 OFFSET $2
|
|
|
|
LIMIT $1
|
|
|
|
|
|
|
|
OFFSET $2
|
|
|
|
""",
|
|
|
|
""",
|
|
|
|
limit, offset
|
|
|
|
limit, offset
|
|
|
|
)
|
|
|
|
)
|
|
|
|