Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimize api /knowledge/space/list #906

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pilot/server/knowledge/document_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ def get_documents(self, query):
session.close()
return result

def get_knowledge_documents_count_bulk(self, space_names):
session = self.get_session()
"""
Perform a batch query to count the number of documents for each knowledge space.

Args:
space_names: A list of knowledge space names to query for document counts.
session: A SQLAlchemy session object.

Returns:
A dictionary mapping each space name to its document count.
"""
counts_query = (
session.query(
KnowledgeDocumentEntity.space,
func.count(KnowledgeDocumentEntity.id).label("document_count"),
)
.filter(KnowledgeDocumentEntity.space.in_(space_names))
.group_by(KnowledgeDocumentEntity.space)
)

results = counts_query.all()
docs_count = {result.space: result.document_count for result in results}
return docs_count

def get_knowledge_documents_count(self, query):
session = self.get_session()
knowledge_documents = session.query(func.count(KnowledgeDocumentEntity.id))
Expand Down
10 changes: 6 additions & 4 deletions pilot/server/knowledge/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ def get_knowledge_space(self, request: KnowledgeSpaceRequest):
query = KnowledgeSpaceEntity(
name=request.name, vector_type=request.vector_type, owner=request.owner
)
responses = []
spaces = knowledge_space_dao.get_knowledge_space(query)
space_names = [space.name for space in spaces]
docs_count = knowledge_document_dao.get_knowledge_documents_count_bulk(
space_names
)
responses = []
for space in spaces:
res = SpaceQueryResponse()
res.id = space.id
Expand All @@ -128,9 +132,7 @@ def get_knowledge_space(self, request: KnowledgeSpaceRequest):
res.gmt_created = space.gmt_created
res.gmt_modified = space.gmt_modified
res.context = space.context
query = KnowledgeDocumentEntity(space=space.name)
doc_count = knowledge_document_dao.get_knowledge_documents_count(query)
res.docs = doc_count
res.docs = docs_count.get(space.name, 0)
responses.append(res)
return responses

Expand Down