-
Notifications
You must be signed in to change notification settings - Fork 999
feat: optimize repeated entity extraction #1682
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
Merged
lxobr
merged 11 commits into
dev
from
feature/cog-3256-optimize-repeated-entity-extraction
Oct 30, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
be7d315
feat: add edge text, embed and expose it
lxobr f6c7bd7
Merge branch 'dev' into feature/cog-3256-optimize-repeated-entity-ext…
lxobr 6e35747
feat: update tests
lxobr f3bda73
feat: expose edge_text to retrieval context
lxobr 2544eb2
test: improve test_edge_ingestion.py
lxobr 41e4198
Merge branch 'dev' into feature/cog-3256-optimize-repeated-entity-ext…
lxobr 485774d
Merge branch 'dev' into feature/cog-3256-optimize-repeated-entity-ext…
lxobr fd96390
Merge branch 'dev' into feature/cog-3256-optimize-repeated-entity-ext…
lxobr a3553f6
Merge branch 'dev' into feature/cog-3256-optimize-repeated-entity-ext…
lxobr 772a1bb
Merge branch 'dev' into feature/cog-3256-optimize-repeated-entity-ext…
lxobr a5c0430
Merge branch 'dev' into feature/cog-3256-optimize-repeated-entity-ext…
lxobr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,71 +1,70 @@ | ||
| import string | ||
| from typing import List | ||
| from cognee.modules.graph.cognee_graph.CogneeGraphElements import Edge | ||
|
|
||
| from collections import Counter | ||
|
|
||
| async def resolve_edges_to_text(retrieved_edges: List[Edge]) -> str: | ||
| """ | ||
| Converts retrieved graph edges into a human-readable string format. | ||
| from cognee.modules.graph.cognee_graph.CogneeGraphElements import Edge | ||
| from cognee.modules.retrieval.utils.stop_words import DEFAULT_STOP_WORDS | ||
|
|
||
| Parameters: | ||
| ----------- | ||
|
|
||
| - retrieved_edges (list): A list of edges retrieved from the graph. | ||
| def _get_top_n_frequent_words( | ||
| text: str, stop_words: set = None, top_n: int = 3, separator: str = ", " | ||
| ) -> str: | ||
| """Concatenates the top N frequent words in text.""" | ||
| if stop_words is None: | ||
| stop_words = DEFAULT_STOP_WORDS | ||
|
|
||
| Returns: | ||
| -------- | ||
| words = [word.lower().strip(string.punctuation) for word in text.split()] | ||
| words = [word for word in words if word and word not in stop_words] | ||
|
|
||
| - str: A formatted string representation of the nodes and their connections. | ||
| """ | ||
| top_words = [word for word, freq in Counter(words).most_common(top_n)] | ||
| return separator.join(top_words) | ||
|
|
||
| def _get_nodes(retrieved_edges: List[Edge]) -> dict: | ||
| def _get_title(text: str, first_n_words: int = 7, top_n_words: int = 3) -> str: | ||
| def _top_n_words(text, stop_words=None, top_n=3, separator=", "): | ||
| """Concatenates the top N frequent words in text.""" | ||
| if stop_words is None: | ||
| from cognee.modules.retrieval.utils.stop_words import DEFAULT_STOP_WORDS | ||
|
|
||
| stop_words = DEFAULT_STOP_WORDS | ||
| def _create_title_from_text(text: str, first_n_words: int = 7, top_n_words: int = 3) -> str: | ||
| """Creates a title by combining first words with most frequent words from the text.""" | ||
| first_words = text.split()[:first_n_words] | ||
| top_words = _get_top_n_frequent_words(text, top_n=top_n_words) | ||
| return f"{' '.join(first_words)}... [{top_words}]" | ||
|
|
||
| import string | ||
|
|
||
| words = [word.lower().strip(string.punctuation) for word in text.split()] | ||
| def _extract_nodes_from_edges(retrieved_edges: List[Edge]) -> dict: | ||
| """Creates a dictionary of nodes with their names and content.""" | ||
| nodes = {} | ||
|
|
||
| if stop_words: | ||
| words = [word for word in words if word and word not in stop_words] | ||
| for edge in retrieved_edges: | ||
| for node in (edge.node1, edge.node2): | ||
| if node.id in nodes: | ||
| continue | ||
|
|
||
| from collections import Counter | ||
| text = node.attributes.get("text") | ||
| if text: | ||
| name = _create_title_from_text(text) | ||
| content = text | ||
| else: | ||
| name = node.attributes.get("name", "Unnamed Node") | ||
| content = node.attributes.get("description", name) | ||
|
|
||
| top_words = [word for word, freq in Counter(words).most_common(top_n)] | ||
| nodes[node.id] = {"node": node, "name": name, "content": content} | ||
|
|
||
| return separator.join(top_words) | ||
| return nodes | ||
|
|
||
| """Creates a title, by combining first words with most frequent words from the text.""" | ||
| first_words = text.split()[:first_n_words] | ||
| top_words = _top_n_words(text, top_n=first_n_words) | ||
| return f"{' '.join(first_words)}... [{top_words}]" | ||
|
|
||
| """Creates a dictionary of nodes with their names and content.""" | ||
| nodes = {} | ||
| for edge in retrieved_edges: | ||
| for node in (edge.node1, edge.node2): | ||
| if node.id not in nodes: | ||
| text = node.attributes.get("text") | ||
| if text: | ||
| name = _get_title(text) | ||
| content = text | ||
| else: | ||
| name = node.attributes.get("name", "Unnamed Node") | ||
| content = node.attributes.get("description", name) | ||
| nodes[node.id] = {"node": node, "name": name, "content": content} | ||
| return nodes | ||
| async def resolve_edges_to_text(retrieved_edges: List[Edge]) -> str: | ||
| """Converts retrieved graph edges into a human-readable string format.""" | ||
| nodes = _extract_nodes_from_edges(retrieved_edges) | ||
|
|
||
| nodes = _get_nodes(retrieved_edges) | ||
| node_section = "\n".join( | ||
| f"Node: {info['name']}\n__node_content_start__\n{info['content']}\n__node_content_end__\n" | ||
| for info in nodes.values() | ||
| ) | ||
| connection_section = "\n".join( | ||
| f"{nodes[edge.node1.id]['name']} --[{edge.attributes['relationship_type']}]--> {nodes[edge.node2.id]['name']}" | ||
| for edge in retrieved_edges | ||
| ) | ||
|
|
||
| connections = [] | ||
| for edge in retrieved_edges: | ||
| source_name = nodes[edge.node1.id]["name"] | ||
| target_name = nodes[edge.node2.id]["name"] | ||
| edge_label = edge.attributes.get("edge_text") or edge.attributes.get("relationship_type") | ||
| connections.append(f"{source_name} --[{edge_label}]--> {target_name}") | ||
|
|
||
| connection_section = "\n".join(connections) | ||
|
|
||
| return f"Nodes:\n{node_section}\n\nConnections:\n{connection_section}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.