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 the perf issue in building nodes from splits. #10766

Merged
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
10 changes: 7 additions & 3 deletions llama-index-core/llama_index/core/node_parser/node_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def build_nodes_from_splits(
ref_doc = ref_doc or document
id_func = id_func or default_id_func
nodes: List[TextNode] = []
"""Calling as_related_node_info() on a document recomputes the hash for the whole text and metadata"""
"""It is not that bad, when creating relationships between the nodes, but is terrible when adding a relationship"""
"""between the node and a document, hence we create the relationship only once here and pass it to the nodes"""
relationships = {NodeRelationship.SOURCE: ref_doc.as_related_node_info()}
for i, text_chunk in enumerate(text_splits):
logger.debug(f"> Adding chunk: {truncate_text(text_chunk, 50)}")

Expand All @@ -54,7 +58,7 @@ def build_nodes_from_splits(
metadata_seperator=document.metadata_seperator,
metadata_template=document.metadata_template,
text_template=document.text_template,
relationships={NodeRelationship.SOURCE: ref_doc.as_related_node_info()},
relationships=relationships,
)
nodes.append(image_node) # type: ignore
elif isinstance(document, Document):
Expand All @@ -67,7 +71,7 @@ def build_nodes_from_splits(
metadata_seperator=document.metadata_seperator,
metadata_template=document.metadata_template,
text_template=document.text_template,
relationships={NodeRelationship.SOURCE: ref_doc.as_related_node_info()},
relationships=relationships,
)
nodes.append(node)
elif isinstance(document, TextNode):
Expand All @@ -80,7 +84,7 @@ def build_nodes_from_splits(
metadata_seperator=document.metadata_seperator,
metadata_template=document.metadata_template,
text_template=document.text_template,
relationships={NodeRelationship.SOURCE: ref_doc.as_related_node_info()},
relationships=relationships,
)
nodes.append(node)
else:
Expand Down
Loading