Stocarea și interogarea documentelor

Retrieval Augmented Generation (RAG) cu LangChain

Meri Nova

Machine Learning Engineer

Instanțierea bazei de date 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) cu LangChain

Stocarea documentelor graf

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) cu LangChain

Stocarea documentelor graf

graph.add_graph_documents(
  graph_documents,

include_source=True,
baseEntityLabel=True
)
  • include_source=True: leagă nodurile de documentele sursă printr-o muchie MENTIONS
  • baseEntityLabel=True: adaugă eticheta __Entity__ fiecărui nod
Retrieval Augmented Generation (RAG) cu LangChain

Documentele graf reprezentate ca noduri și muchii.

Retrieval Augmented Generation (RAG) cu LangChain

O versiune mărită a imaginii anterioare, cu noduri despre modelele OpenAI.

Retrieval Augmented Generation (RAG) cu LangChain

Schema bazei de date

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) cu LangChain

Interogarea Neo4j - Limbajul Cypher

Un nod numit James cu o relație numită friends care indică spre un nod persoană necunoscut.

Retrieval Augmented Generation (RAG) cu LangChain

Interogarea Neo4j - Limbajul Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) cu LangChain

Interogarea Neo4j - Limbajul Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) cu LangChain

Interogarea Neo4j - Limbajul Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) cu LangChain

Interogarea Neo4j - Limbajul Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) cu LangChain

Interogarea grafului 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) cu LangChain

Să exersăm!

Retrieval Augmented Generation (RAG) cu LangChain

Preparing Video For Download...