Skip to content

Commit

Permalink
feat(llm): extend node from llm
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed Jul 11, 2024
1 parent 71ae7c1 commit 7e9635e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/retk/controllers/ai/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ async def get_extended_nodes(
au: AuthedUser,
) -> schemas.ai.GetExtendedNodesResponse:
docs = await core.ai.llm.knowledge.extended.get_extended_nodes(uid=au.u.id)
return schemas.ai.GetExtendedNodesResponse(
requestId=au.request_id,
nodes=[schemas.ai.GetExtendedNodesResponse.Node(
nodes = []
for doc in docs:
res = doc["extendMd"].split("\n", 1)
if len(res) == 2:
title, content = res
else:
title, content = res[0], ""
node = schemas.ai.GetExtendedNodesResponse.Node(
id=str(doc["_id"]),
sourceNid=doc["sourceNid"],
sourceTitle=doc["sourceMd"].split("\n", 1)[0].strip(),
md=doc["extendMd"],
) for doc in docs]
title=title.strip(),
content=content.strip(),
)
nodes.append(node)
return schemas.ai.GetExtendedNodesResponse(
requestId=au.request_id,
nodes=nodes
)


Expand Down
3 changes: 2 additions & 1 deletion src/retk/controllers/schemas/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Node(BaseModel):
id: str
sourceNid: str
sourceTitle: str
md: str
title: str
content: str

requestId: str
nodes: List[Node]
3 changes: 2 additions & 1 deletion src/retk/controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async def get_user(
max_space = 0
else:
max_space = const.USER_TYPE.id2config(au.u.type).max_store_space
total_nodes = await core.user.get_user_nodes_count(uid=au.u.id, disabled=False, in_trash=False)
return schemas.user.UserInfoResponse(
requestId=au.request_id,
user=schemas.user.UserInfoResponse.User(
Expand All @@ -38,7 +39,7 @@ async def get_user(
editorSepRightWidth=au.u.settings.editor_sep_right_width,
editorSideCurrentToolId=au.u.settings.editor_side_current_tool_id,
),
totalNodes=await core.user.get_user_nodes_count(uid=au.u.id, disabled=False, in_trash=False),
totalNodes=total_nodes,
),
)

Expand Down
2 changes: 1 addition & 1 deletion src/retk/routes/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

router = APIRouter(
prefix="/api/ai",
tags=["node"],
tags=["ai"],
responses={404: {"description": "Not found"}},
)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,8 @@ async def test_node_extend(self):
rj = self.check_ok_response(resp, 200)
self.assertEqual(1, len(rj["nodes"]))
n = rj["nodes"][0]
self.assertEqual("this is extended md", n["md"])
self.assertEqual("this is extended md", n["title"])
self.assertEqual("", n["content"])
self.assertEqual(node["id"], n["sourceNid"])

resp = self.client.post(
Expand Down

0 comments on commit 7e9635e

Please sign in to comment.