Ukládání a dotazování dokumentů

Retrieval Augmented Generation (RAG) with LangChain

Meri Nova

Machine Learning Engineer

Inicializace databáze 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) with LangChain

Ukládání grafových dokumentů

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

Ukládání grafových dokumentů

graph.add_graph_documents(
  graph_documents,

include_source=True,
baseEntityLabel=True
)
  • include_source=True: propojí uzly se zdrojovými dokumenty hranou MENTIONS
  • baseEntityLabel=True: přidá každému uzlu štítek __Entity__
Retrieval Augmented Generation (RAG) with LangChain

Grafové dokumenty zobrazené jako uzly a hrany.

Retrieval Augmented Generation (RAG) with LangChain

Přiblížená verze předchozího obrázku zobrazující uzly o modelech OpenAI.

Retrieval Augmented Generation (RAG) with LangChain

Schéma databáze

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

Dotazování Neo4j – jazyk Cypher

Uzel James s relací friends směřující k neznámému uzlu osoby.

Retrieval Augmented Generation (RAG) with LangChain

Dotazování Neo4j – jazyk Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) with LangChain

Dotazování Neo4j – jazyk Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) with LangChain

Dotazování Neo4j – jazyk Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) with LangChain

Dotazování Neo4j – jazyk Cypher

cypher4.jpg

Retrieval Augmented Generation (RAG) with LangChain

Dotazování grafu 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) with LangChain

Lass uns üben!

Retrieval Augmented Generation (RAG) with LangChain

Preparing Video For Download...