Stocker et interroger des documents

Retrieval Augmented Generation (RAG) avec LangChain

Meri Nova

Machine Learning Engineer

Instancier la base Neo4j

from langchain_community.graphs import Neo4jGraph

graph = Neo4jGraph(url="bolt://localhost:7687", username="neo4j", password="...")
import os

url = os.environ["NEO4J_URI"]
user = os.environ["NEO4J_USERNAME"]
password = os.environ["NEO4J_PASSWORD"]

graph = Neo4jGraph(url=url, username=user, password=password)
1 https://neo4j.com/download/
Retrieval Augmented Generation (RAG) avec LangChain

Stocker des documents de graphe

from langchain_experimental.graph_transformers import LLMGraphTransformer

llm = ChatOpenAI(api_key="...", temperature=0, model="gpt-4o-mini")
llm_transformer = LLMGraphTransformer(llm=llm)

graph_documents = llm_transformer.convert_to_graph_documents(documents)
Retrieval Augmented Generation (RAG) avec LangChain

Stocker des documents de graphe

graph.add_graph_documents(
  graph_documents,

include_source=True,
baseEntityLabel=True
)
  • include_source=True : relier les nœuds aux documents sources avec l'arête MENTIONS
  • baseEntityLabel=True : ajouter l'étiquette __Entity__ à chaque nœud
Retrieval Augmented Generation (RAG) avec LangChain

Les documents de graphe représentés en nœuds et arêtes.

Retrieval Augmented Generation (RAG) avec LangChain

Gros plan de l'image précédente montrant des nœuds sur les modèles OpenAI.

Retrieval Augmented Generation (RAG) avec LangChain

Schéma de la base de données

print(graph.get_schema)
Node properties:
Concept {id: STRING}
Architecture {id: STRING}
Organization {id: STRING}
Event {id: STRING}
Paper {id: STRING}

The relationships:
(:Concept)-[:DEVELOPED_BY]->(:Person)
(:Architecture)-[:BASED_ON]->(:Concept)
(:Organization)-[:PROPOSED]->(:Concept)
(:Document)-[:MENTIONS]->(:Event)
(:Paper)-[:BASED_ON]->(:Concept)
Retrieval Augmented Generation (RAG) avec LangChain

Interroger Neo4j – langage de requête Cypher

Un nœud nommé James avec une relation « friends » pointant vers un nœud personne mystère.

Retrieval Augmented Generation (RAG) avec LangChain

Interroger Neo4j – langage de requête Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) avec LangChain

Interroger Neo4j – langage de requête Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) avec LangChain

Interroger Neo4j – langage de requête Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) avec LangChain

Interroger Neo4j – langage de requête Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) avec LangChain

Interroger le graphe LLM

results = graph.query("""
MATCH (gpt4:Model {id: "Gpt-4"})-[:DEVELOPED_BY]->(org:Organization)
RETURN org
""")

print(results)
[{'org': {'id': 'Openai'}}]
Retrieval Augmented Generation (RAG) avec LangChain

Passons à la pratique !

Retrieval Augmented Generation (RAG) avec LangChain

Preparing Video For Download...