Graph RAG with LangChain and Neo4j
Adam Cowley
Manager, Developer Education at Neo4j
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.
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
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"},
...
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 ]}
)
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
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