Skip to content

Commit 3c3fa32

Browse files
Ian Robinsonianrob
andauthored
Remove code that creates NEXT relationships between facts (#267)
Co-authored-by: ianrob <ianrob@amazon.co.uk>
1 parent e5ffb76 commit 3c3fa32

8 files changed

Lines changed: 3 additions & 181 deletions

File tree

docs-site/src/content/docs/lexical-graph/graph-model.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ There are two types of fact: subject-predicate-object (SPO) facts, and subject-p
7474
Neptune Analytics PURPOSE analyze graph data
7575
```
7676

77-
SPO facts are connected to other facts via `__NEXT__` relationships, where the object entity of a first fact acts as the subject entity for a subsequent fact.
78-
7977
Facts provide *connectivity across different sources*. It's not uncommon for an individual fact to be mentioned multiple times in the underlying corpus: for example, in a news articles dataset, a particular fact might be repeated in different news articles reporting on the same story. In the graph, there will be a single node to represent this specific fact. From this node it is then possible to traverse via statements, topics and chunks to all the places where that particular fact is mentioned.
8078

8179
Every fact `__SUPPORTS__` at least one statement. A fact can support multiple statements, belonging to the same or different topics and sources.

examples/lexical-graph/scripts/repair_facts.py

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -290,47 +290,6 @@ def create_entity_entity_relation(graph_store, facts):
290290
graph_store.execute_query_with_retry(cypher, parameters)
291291

292292

293-
294-
def create_fact_next_relation(graph_store, facts):
295-
296-
params = []
297-
298-
for fact in facts:
299-
params.append({
300-
'fact_id': fact['fact_id']
301-
})
302-
303-
parameters = {
304-
'params': params
305-
}
306-
307-
308-
statements_prev = [
309-
'// insert connection to prev facts',
310-
'UNWIND $params AS params',
311-
f'MATCH (fact:`__Fact__`{{{graph_store.node_id("factId")}: params.fact_id}})<-[:`__SUBJECT__`]-(:`__Entity__`)-[:`__OBJECT__`]->(prevFact:`__Fact__`)',
312-
'WHERE fact <> prevFact // and NOT ((fact)<-[:`__NEXT__`]-(prevFact))',
313-
'WITH DISTINCT fact, prevFact',
314-
'MERGE (fact)<-[:`__NEXT__`]-(prevFact)'
315-
]
316-
317-
query_prev = '\n'.join(statements_prev)
318-
319-
graph_store.execute_query_with_retry(query_prev, parameters, max_attempts=5, max_wait=7)
320-
321-
statements_next = [
322-
'// insert connection to next facts',
323-
'UNWIND $params AS params',
324-
f'MATCH (fact:`__Fact__`{{{graph_store.node_id("factId")}: params.fact_id}})<-[:`__OBJECT__`]-(:`__Entity__`)-[:`__SUBJECT__`]->(nextFact:`__Fact__`)',
325-
'WHERE fact <> nextFact // and NOT ((fact)-[:`__NEXT__`]->(nextFact))',
326-
'WITH DISTINCT fact, nextFact',
327-
'MERGE (fact)-[:`__NEXT__`]->(nextFact)'
328-
]
329-
330-
query_next = '\n'.join(statements_next)
331-
332-
graph_store.execute_query_with_retry(query_next, parameters, max_attempts=5, max_wait=7)
333-
334293
def get_stats(graph_store, fact_ids, batch_size):
335294

336295
stats = {}
@@ -377,57 +336,6 @@ def get_stats(graph_store, fact_ids, batch_size):
377336

378337
stats['num_object_relationships'] = total_object
379338

380-
381-
382-
383-
#cypher = '''
384-
#MATCH (:`__Entity__`)-[r:`__SUBJECT__`]->()
385-
#RETURN count(r) AS count
386-
#'''
387-
#
388-
#results = graph_store.execute_query_with_retry(cypher, {})
389-
#
390-
#stats['num_subject_relationships'] = results[0]['count']
391-
#
392-
#cypher = '''
393-
#MATCH (:`__Entity__`)-[r:`__OBJECT__`]->()
394-
#RETURN count(r) AS count
395-
#'''
396-
#
397-
#results = graph_store.execute_query_with_retry(cypher, {})
398-
#
399-
#stats['num_object_relationships'] = results[0]['count']
400-
401-
#cypher = '''
402-
#MATCH (:`__Entity__`)-[r:`__RELATION__`]->(:`__Entity__`)
403-
#RETURN count(r) AS count
404-
#'''
405-
#
406-
#results = graph_store.execute_query_with_retry(cypher, {})
407-
#
408-
#stats['num_relation_relationships'] = results[0]['count']
409-
410-
total_next = 0
411-
412-
progress_bar_1 = tqdm(total=len(fact_ids), desc='Counting NEXT relationships')
413-
for fact_id_batch in iter_batch(fact_ids, batch_size=batch_size):
414-
cypher = '''
415-
MATCH (f)-[r:`__NEXT__`]->() WHERE id(f) in $fact_ids
416-
RETURN count(r) AS count
417-
'''
418-
419-
params = {
420-
'fact_ids': fact_id_batch
421-
}
422-
423-
results = graph_store.execute_query_with_retry(cypher, params)
424-
425-
counts = [r['count'] for r in results]
426-
total_next += sum(counts)
427-
progress_bar_1.update(len(fact_id_batch))
428-
429-
stats['num_next_relationships'] = total_next
430-
431339
return stats
432340

433341
def iter_batch(iterable, batch_size):
@@ -477,23 +385,6 @@ def repair(graph_store_info, batch_size, skip_invalid_relationships, skip_entity
477385
create_entity_fact_relation(graph_store, facts, 'object')
478386
progress_bar_1.update(len(fact_id_batch))
479387

480-
#print()
481-
#print('Creating RELATION entity-entity relationships...')
482-
#total = 0
483-
#for fact_id_batch in iter_batch(fact_ids, batch_size=batch_size):
484-
# facts = get_facts(graph_store, fact_id_batch)
485-
# create_entity_entity_relation(graph_store, facts)
486-
# total += len(fact_id_batch)
487-
# if total % TOTAL_MOD == 0:
488-
# print(f' {total}')
489-
#print(f' Done')
490-
491-
progress_bar_2 = tqdm(total=len(fact_ids_to_process), desc='Creating NEXT fact-fact relationships')
492-
for fact_id_batch in iter_batch(fact_ids_to_process, batch_size=batch_size):
493-
facts = get_facts(graph_store, fact_id_batch)
494-
create_fact_next_relation(graph_store, facts)
495-
progress_bar_2.update(len(fact_id_batch))
496-
497388
stats['after'] = get_stats(graph_store, fact_ids, batch_size)
498389

499390
return stats

images/lexical-graph.png

-1.12 KB
Loading

integration-tests/test-scripts/graphrag_toolkit_tests/build_facts.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,5 @@ def test_contains_correct_number_of_facts(self):
6161

6262
self.assertEqual(4, fact_node_count)
6363

64-
def test_contains_correct_number_of_next_relationships(self):
65-
"""Graph contains correct number of next relationships"""
66-
67-
results = self._graph_store.execute_query('MATCH (:`__Fact__`)-[r:`__NEXT__`]->() RETURN count(r) AS count')
68-
next_relationship_count = results[0]['count']
69-
70-
self.assertEqual(3, next_relationship_count)
7164

7265
handler.run_assertions(BuildFactsAssertions)

integration-tests/test-scripts/graphrag_toolkit_tests/local_entities.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ def test_fact_supports_statement_relationships(self):
118118

119119
self.assertEqual(relationship_count, 43)
120120

121-
def test_fact_next_fact_relationships(self):
122-
"""Graph contains expected fact NEXT fact relationships"""
123-
124-
results = self._graph_store.execute_query('MATCH (:`__Fact__`)<-[r:`__NEXT__`]-(:`__Fact__`) RETURN count(r) AS count')
125-
relationship_count = results[0]['count']
126-
127-
self.assertEqual(relationship_count, 27)
128121

129122
def test_entity_subject_fact_relationships(self):
130123
"""Graph contains expected entity SUBJECT fact relationships"""
@@ -250,13 +243,6 @@ def test_fact_supports_statement_relationships(self):
250243

251244
self.assertEqual(relationship_count, 43)
252245

253-
def test_fact_next_fact_relationships(self):
254-
"""Graph contains expected fact NEXT fact relationships"""
255-
256-
results = self._graph_store.execute_query('MATCH (:`__Fact__`)<-[r:`__NEXT__`]-(:`__Fact__`) RETURN count(r) AS count')
257-
relationship_count = results[0]['count']
258-
259-
self.assertEqual(relationship_count, 15)
260246

261247
def test_entity_subject_fact_relationships(self):
262248
"""Graph contains expected entity SUBJECT fact relationships"""

lexical-graph/src/graphrag_toolkit/lexical_graph/indexing/build/fact_graph_builder.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -129,52 +129,6 @@ def insert_entity_fact_relationship(relationship_type:str, entity_id:Optional[st
129129
insert_entity_fact_relationship('object', fact.object.entityId)
130130
if fact.complement and include_local_entities:
131131
insert_entity_fact_relationship('object', fact.complement.entityId)
132-
133-
create_next_relationship = Query(
134-
query=f"""// insert next connection between facts
135-
UNWIND $params AS params
136-
MATCH (start{{{graph_client.node_id("factId")}: params.startId}}), (end{{{graph_client.node_id("factId")}: params.endId}})
137-
MERGE (start)-[:`__NEXT__`]->(end)
138-
"""
139-
)
140-
141-
find_start_end_for_prev_facts = Query(
142-
query=f"""// get start and end facts for prev connection
143-
UNWIND $params AS params
144-
MATCH (fact:`__Fact__`{{{graph_client.node_id("factId")}: params.fact_id}})<-[:`__SUBJECT__`]-(:`__Entity__`)-[:`__OBJECT__`]->(prevFact:`__Fact__`)
145-
WHERE fact <> prevFact
146-
RETURN {graph_client.node_id('prevFact.factId')} AS startId, {graph_client.node_id('fact.factId')} AS endId
147-
""",
148-
child_queries=[create_next_relationship]
149-
)
150-
151-
params = {
152-
'fact_id': fact.factId
153-
}
154-
155-
query_tree = QueryTree('insert-prev-facts', find_start_end_for_prev_facts)
156-
157-
graph_client.execute_query_with_retry(query_tree, self._to_params(params), max_attempts=10, max_wait=10)
158-
159-
if fact.object or fact.complement:
160-
161-
find_start_end_for_next_facts = Query(
162-
query=f"""// get start and end facts for next connection
163-
UNWIND $params AS params
164-
MATCH (fact:`__Fact__`{{{graph_client.node_id("factId")}: params.fact_id}})<-[:`__OBJECT__`]-(:`__Entity__`)-[:`__SUBJECT__`]->(nextFact:`__Fact__`)
165-
WHERE fact <> nextFact
166-
RETURN {graph_client.node_id('fact.factId')} AS startId, {graph_client.node_id('nextFact.factId')} AS endId
167-
""",
168-
child_queries=[create_next_relationship]
169-
)
170-
171-
params = {
172-
'fact_id': fact.factId
173-
}
174-
175-
query_tree = QueryTree('insert-next-facts', find_start_end_for_next_facts)
176-
177-
graph_client.execute_query_with_retry(query_tree, self._to_params(params), max_attempts=10, max_wait=10)
178132

179133
else:
180134
logger.warning(f'fact_id missing from fact node [node_id: {node.node_id}]')

lexical-graph/src/graphrag_toolkit/lexical_graph/retrieval/retrievers/entity_based_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _single_entity_based_graph_search(self, entity_id, query:QueryBundle):
190190

191191
cypher = f'''// single entity-based graph search
192192
MATCH (:`__Entity__`{{{self.graph_store.node_id("entityId")}:$startId}})
193-
-[:`__SUBJECT__`]->()-[:`__NEXT__`*0..1]-()
193+
-[:`__SUBJECT__`]->()
194194
-[:`__SUPPORTS__`]->()
195195
-[:`__PREVIOUS__`*0..1]-(l)
196196
RETURN DISTINCT {self.graph_store.node_id("l.statementId")} AS l LIMIT $statementLimit'''

lexical-graph/src/graphrag_toolkit/lexical_graph/retrieval/retrievers/topic_based_search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def topic_based_graph_search(self, topic_id):
8080
query to traverse a graph database, retrieving relevant `__Fact__` and associated
8181
`__Statement__` nodes connected to a specific topic.
8282
83-
The query traverses relationships such as `__NEXT__`, `__SUPPORTS__`, and
83+
The query traverses relationships such as `__SUPPORTS__`, and
8484
`__BELONGS_TO__` to ensure that all relevant nodes and their connections are
8585
retrieved based on the provided topic ID and query limits.
8686
@@ -99,7 +99,7 @@ def topic_based_graph_search(self, topic_id):
9999
MATCH (f)-[:`__SUPPORTS__`]->()-[:`__BELONGS_TO__`]->(tt:`__Topic__`)
100100
WHERE {self.graph_store.node_id("tt.topicId")} = $topicId
101101
WITH f LIMIT $statementLimit
102-
MATCH (f)-[:`__NEXT__`*0..1]-()-[:`__SUPPORTS__`]->(l)
102+
MATCH (f)-[:`__SUPPORTS__`]->(l)
103103
RETURN DISTINCT {self.graph_store.node_id("l.statementId")} AS l LIMIT $statementLimit
104104
'''
105105

0 commit comments

Comments
 (0)