@ -37,12 +37,17 @@ app = FastAPI(lifespan=lifespan)
app . mount ( " /static " , StaticFiles ( directory = " Static " ) , name = " static " )
# 访问根的跳转
@app . get ( " / " )
async def redirect_to_ai ( ) :
return fastapi . responses . RedirectResponse ( url = " /static/ai.html " )
@app . post ( " /api/rag " )
async def rag ( request : fastapi . Request ) :
data = await request . json ( )
logger . info ( f " Received request: { data } " )
workspace = data . get ( " topic " , " ShiJi " ) # Chinese, Math ,ShiJi 默认是少年读史记
mode = data . get ( " mode " , " hybrid " ) # 默认为hybrid模式
logger. info ( " 工作空间: " + workspace )
# 查询的问题
query = data . get ( " query " )
@ -104,9 +109,7 @@ async def rag(request: fastapi.Request):
rag = await initialize_pg_rag ( WORKING_DIR = WORKING_DIR , workspace = workspace )
resp = await rag . aquery (
query = query ,
param = QueryParam ( mode = mode , stream = True , user_prompt = user_prompt ) )
# hybrid naive
param = QueryParam ( mode = " hybrid " , stream = True , user_prompt = user_prompt ) )
async for chunk in resp :
if not chunk :
continue
@ -180,15 +183,15 @@ async def get_tree_data():
async with pg_pool . acquire ( ) as conn :
# 执行查询
rows = await conn . fetch ( """
SELECT id ,
title ,
parent_id ,
is_leaf ,
prerequisite ,
related
FROM knowledge_points
ORDER BY parent_id , id
""" )
SELECT id ,
title ,
parent_id ,
is_leaf ,
prerequisite ,
related
FROM knowledge_points
ORDER BY parent_id , id
""" )
# 构建节点映射
nodes = { }
for row in rows :
@ -245,10 +248,10 @@ async def update_knowledge(request: fastapi.Request):
async with pg_pool . acquire ( ) as conn :
if update_type == ' prerequisite ' :
await conn . execute ( """
UPDATE knowledge_points
SET prerequisite = $ 1
WHERE id = $ 2
""" ,
UPDATE knowledge_points
SET prerequisite = $ 1
WHERE id = $ 2
""" ,
json . dumps (
[ { " id " : p [ " id " ] , " title " : p [ " title " ] } for p in knowledge ] ,
ensure_ascii = False
@ -256,10 +259,10 @@ async def update_knowledge(request: fastapi.Request):
node_id )
else : # related knowledge
await conn . execute ( """
UPDATE knowledge_points
SET related = $ 1
WHERE id = $ 2
""" ,
UPDATE knowledge_points
SET related = $ 1
WHERE id = $ 2
""" ,
json . dumps (
[ { " id " : p [ " id " ] , " title " : p [ " title " ] } for p in knowledge ] ,
ensure_ascii = False
@ -306,10 +309,11 @@ async def get_sources(page: int = 1, limit: int = 10):
offset = ( page - 1 ) * limit
rows = await conn . fetch (
"""
SELECT id , account_id , account_name , created_at
FROM t_wechat_source
ORDER BY created_at DESC
LIMIT $ 1 OFFSET $ 2
SELECT id , account_id , account_name , created_at
FROM t_wechat_source
ORDER BY created_at DESC
LIMIT $ 1
OFFSET $ 2
""" ,
limit , offset
)
@ -348,11 +352,16 @@ async def get_articles(page: int = 1, limit: int = 10):
offset = ( page - 1 ) * limit
rows = await conn . fetch (
"""
SELECT a . id , a . title , a . source as name ,
a . publish_time , a . collection_time , a . url
SELECT a . id ,
a . title ,
a . source as name ,
a . publish_time ,
a . collection_time ,
a . url
FROM t_wechat_articles a
ORDER BY a . collection_time DESC
LIMIT $ 1 OFFSET $ 2
ORDER BY a . collection_time DESC
LIMIT $ 1
OFFSET $ 2
""" ,
limit , offset
)