Entity resolution

Graph RAG with LangChain and Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

LLMs are stateless

Call #1

List the characters in the following text:

{text}
The characters in this text are:

* Sampson - A servant of the Capulet household.
* Gregory - Another servant of the Capulet household.
* Abram - A servant of the Montague household.
* Balthasar - A servant of the Montague household.

Call #2

List the characters in the following text:

{text}
The characters listed in this text are:

* Sampson - Servant Capulet
* Gregory - Servant of Capulet
* Abram - Servant of the Montague
* Benvolio - Servant of the Montague
Graph RAG with LangChain and Neo4j

Entity suggestions

examples = graph.query("""
MATCH (c:Character)-[:BELONGS_TO]->(f:Family)
RETURN c.id AS id, c.name AS name, f.name AS belongsToFamily
""")

for character in example:
    print(character)
{"id": "romeo-montague", "name": "Romeo", "family": "Montague"},

{"id": "juliet-capulet", "name": "Juliet", "family": "Capulet"},
{"id": "sampson", "name": "Sampson", "family": "Capulet"},
...
Graph RAG with LangChain and Neo4j

Entity suggestions

SystemPromptTemplate.from_template("""
Extract the information that corresponds to the following entities.


Ignore any characters that aren't included in the following list:
{examples}
""",
partial_variables={"examples": [ dict(e) for e in examples ]}
)
Graph RAG with LangChain and Neo4j

Graph-based entity resolution

entity_resolution1.jpg

Graph RAG with LangChain and Neo4j

Graph-based entity resolution

entity_resolution2.jpg

Graph RAG with LangChain and Neo4j

Graph-based entity resolution

entity_resolution3.jpg

Graph RAG with LangChain and Neo4j

Graph-based entity resolution

entity_resolution4.jpg

Graph RAG with LangChain and Neo4j

Graph-based entity resolution

entity_resolution5.jpg

Graph RAG with LangChain and Neo4j

Identifying similar people

MATCH path = (c:Character {id: "romeo-capulet"})-[r]-(inCommon:Character),

(inCommon)-[r2]-(other:Character)
RETURN c.id AS id, c.name AS name, other.name AS otherName, other.id AS otherId,
c.name = other.name AS shareName,
EXISTS { (c)-[:BELONGS_TO]->(:Family)<-[:BELONGS_TO]-(other) } AS belongToFamily,
count(*) as nodesInCommon,
collect(path) AS paths
ORDER BY nodesInCommon DESC LIMIT 10
Graph RAG with LangChain and Neo4j

Using similarity relationships

a graph diagram of person nodes with two person nodes and a similar relationship.  Both have relationships to Line nodes

MATCH (:Character {id: 'romeo-montague'})-[:SIMILAR_TO*0..1]-(character),
  (character)-[:HAS_LINE]->(line),
  (line)-[:SPOKEN_TO]->(target)
RETURN character, line
Graph RAG with LangChain and Neo4j

Let's practice!

Graph RAG with LangChain and Neo4j

Preparing Video For Download...