Résolution d'entités

Graph RAG avec LangChain et Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Les LLM sont sans état

Appel no 1

Dressez la liste des personnages dans le texte suivant :

{text}
Les personnages de ce texte sont :

* Sampson - Un domestique de la maison Capulet.
* Gregory - Un autre domestique de la maison Capulet.
* Abram - Un domestique de la maison Montague.
* Balthasar - Un domestique de la maison Montague.

Appel no 2

Dressez la liste des personnages dans le texte suivant :

{text}
Les personnages listés dans ce texte sont :

* Sampson - Domestique Capulet
* Gregory - Domestique des Capulet
* Abram - Domestique des Montague
* Benvolio - Domestique des Montague
Graph RAG avec LangChain et Neo4j

Suggestions d'entités

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 avec LangChain et Neo4j

Suggestions d'entités

SystemMessagePromptTemplate.from_template("""
Extrayez l'information correspondant aux entités suivantes.


Ignorez tout personnage qui ne figure pas dans la liste suivante :
{examples}
""",
partial_variables={"examples": [ dict(e) for e in examples ]}
)
Graph RAG avec LangChain et Neo4j

Résolution d'entités basée sur un graphe

résolution_d'entités1.jpg

Graph RAG avec LangChain et Neo4j

Résolution d'entités basée sur un graphe

résolution_d'entités2.jpg

Graph RAG avec LangChain et Neo4j

Résolution d'entités basée sur un graphe

résolution_d'entités3.jpg

Graph RAG avec LangChain et Neo4j

Résolution d'entités basée sur un graphe

résolution_d'entités4.jpg

Graph RAG avec LangChain et Neo4j

Résolution d'entités basée sur un graphe

résolution_d'entités5.jpg

Graph RAG avec LangChain et Neo4j

Repérer des personnes similaires

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 avec LangChain et Neo4j

Exploiter les relations de similarité

un diagramme de graphe de nœuds Person avec deux nœuds Person et une relation similar. Les deux ont des relations vers des nœuds Line

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

Passons à la pratique !

Graph RAG avec LangChain et Neo4j

Preparing Video For Download...