Skip to content

Commit ceda0f5

Browse files
committed
enhance getting chunks by relationship ids
1 parent 4958ed8 commit ceda0f5

1 file changed

Lines changed: 40 additions & 21 deletions

File tree

backend/app/rag/indices/knowledge_graph/graph_store/tidb_graph_store.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from app.models import (
4141
KnowledgeBase,
4242
EntityType,
43+
Document,
4344
)
4445

4546
logger = logging.getLogger(__name__)
@@ -312,7 +313,7 @@ def create_relationship(
312313
self._session.flush()
313314

314315
def get_subgraph_by_relationship_ids(
315-
self, ids: list[int], **kwargs
316+
self, ids: list[int], **kwargs
316317
) -> RetrievedKnowledgeGraph:
317318
stmt = (
318319
select(self._relationship_model)
@@ -1081,39 +1082,57 @@ def get_chunks_by_relationships(
10811082
}
10821083

10831084
if not chunk_ids:
1084-
return []
1085+
# Query chunks
1086+
chunks = session.exec(
1087+
select(self._chunk_model).where(self._chunk_model.id.in_(chunk_ids))
1088+
).all()
10851089

1086-
# Query chunks
1087-
chunks = session.exec(
1088-
select(self._chunk_model).where(self._chunk_model.id.in_(chunk_ids))
1090+
return [
1091+
{
1092+
"id": chunk.id,
1093+
"text": chunk.text,
1094+
"document_id": chunk.document_id,
1095+
"meta": {
1096+
"language": chunk.meta.get("language"),
1097+
"product": chunk.meta.get("product"),
1098+
"resource": chunk.meta.get("resource"),
1099+
"source_uri": chunk.meta.get("source_uri"),
1100+
"tidb_version": chunk.meta.get("tidb_version"),
1101+
},
1102+
}
1103+
for chunk in chunks
1104+
]
1105+
1106+
document_ids = {
1107+
rel.meta.get("document_id")
1108+
for rel in relationships
1109+
if rel.meta.get("document_id") is not None
1110+
}
1111+
1112+
documents = session.exec(
1113+
select(Document).where(Document.id.in_(document_ids))
10891114
).all()
10901115

10911116
return [
10921117
{
1093-
"id": chunk.id,
1094-
"text": chunk.text,
1095-
"document_id": chunk.document_id,
1096-
"meta": {
1097-
"language": chunk.meta.get("language"),
1098-
"product": chunk.meta.get("product"),
1099-
"resource": chunk.meta.get("resource"),
1100-
"source_uri": chunk.meta.get("source_uri"),
1101-
"tidb_version": chunk.meta.get("tidb_version"),
1102-
},
1118+
"id": doc.id,
1119+
"text": doc.content,
1120+
"document_id": doc.id,
1121+
"meta": doc.meta,
11031122
}
1104-
for chunk in chunks
1123+
for doc in documents
11051124
]
1106-
1125+
11071126
def get_entire_knowledge_graph(self) -> RetrievedKnowledgeGraph:
11081127
"""Retrieve all entities and relationships from the knowledge graph store.
1109-
1128+
11101129
Returns:
11111130
RetrievedKnowledgeGraph containing all entities and relationships
11121131
"""
11131132
# Query all entities
11141133
entity_query = select(self._entity_model).order_by(self._entity_model.id)
11151134
db_entities = self._session.exec(entity_query).all()
1116-
1135+
11171136
# Query all relationships with their related entities
11181137
relationship_query = (
11191138
select(self._relationship_model)
@@ -1124,7 +1143,7 @@ def get_entire_knowledge_graph(self) -> RetrievedKnowledgeGraph:
11241143
.order_by(self._relationship_model.id)
11251144
)
11261145
db_relationships = self._session.exec(relationship_query).all()
1127-
1146+
11281147
# Convert entities to RetrievedEntity objects
11291148
entities = []
11301149
for entity in db_entities:
@@ -1138,7 +1157,7 @@ def get_entire_knowledge_graph(self) -> RetrievedKnowledgeGraph:
11381157
entity_type=entity.entity_type,
11391158
)
11401159
)
1141-
1160+
11421161
# Convert relationships to RetrievedRelationship objects
11431162
relationships = []
11441163
for rel in db_relationships:

0 commit comments

Comments
 (0)