Creating the Graph RAG chain

Retrieval Augmented Generation (RAG) with LangChain

Meri Nova

Machine Learning Engineer

Building the Graph RAG architecture

A graph database.

Retrieval Augmented Generation (RAG) with LangChain

Building the Graph RAG architecture

Documents being transformed into graph documents and stored in the graph database.

Retrieval Augmented Generation (RAG) with LangChain

Building the Graph RAG architecture

A Cypher query is used to retrieve graph documents from the database. A user input and natural language response back to the user is also shown.

Retrieval Augmented Generation (RAG) with LangChain

Building the Graph RAG architecture

A natural language to Cypher translator converts the natural language input into a Cypher query and the retrieved graph documents back into natural language.

Retrieval Augmented Generation (RAG) with LangChain

From user inputs to Cypher queries

A simple graph containing three nodes and three relationships. Two nodes represent people and another represents a location that James has visited.

Retrieval Augmented Generation (RAG) with LangChain

From user inputs to Cypher queries

A user input asking "Where has James visited?".

Retrieval Augmented Generation (RAG) with LangChain

From user inputs to Cypher queries

The Cypher query is generated using the graph schema and user input.

Retrieval Augmented Generation (RAG) with LangChain

GraphCypherQAChain

The Graph RAG architecture to translate user inputs into Cypher queries and return a natural language response.

Retrieval Augmented Generation (RAG) with LangChain

GraphCypherQAChain

The Graph RAG architecture with the generate cypher chain highighted and the summarize results chain highlighted.

Retrieval Augmented Generation (RAG) with LangChain

Refresh schema

graph.refresh_schema()
print(graph.get_schema)
Node properties:
Document {title: STRING, id: STRING, text: STRING, summary: STRING, source: STRING}
Concept {id: STRING}
Organization {id: STRING}
Relationship properties:

The relationships:
(:Document)-[:MENTIONS]->(:Organization)
(:Concept)-[:DEVELOPED_BY]->(:Person)
Retrieval Augmented Generation (RAG) with LangChain

Querying the graph

from langchain_community.chains.graph_qa.cypher import GraphCypherQAChain

chain = GraphCypherQAChain.from_llm( llm=ChatOpenAI(api_key="...", temperature=0), graph=graph, verbose=True )
result = chain.invoke({"query": "What is the most accurate model?"})
1 https://api.python.langchain.com/en/latest/chains/langchain_community.chains.graph_qa.cypher. GraphCypherQAChain.html
Retrieval Augmented Generation (RAG) with LangChain

Querying the graph

print(f"Final answer: {result['result']}")
> Entering new GraphCypherQAChain chain...
Generated Cypher:
MATCH (m:Model)
RETURN m
ORDER BY m.accuracy DESC
LIMIT 1;
Full Context:
[{'m': {'id': 'Artificial Neural Networks'}}]

> Finished chain.


Final answer: Artificial Neural Networks
Retrieval Augmented Generation (RAG) with LangChain

Customization

chain = GraphCypherQAChain.from_llm(
    llm=ChatOpenAI(api_key="...", temperature=0), graph=graph, verbose=True
)
  • qa_prompt: Prompt template for result generation
  • cypher_prompt: Prompt template for Cypher generation
  • cypher_llm: LLM for Cypher generation
  • qa_llm: LLM for result generation
Retrieval Augmented Generation (RAG) with LangChain

Let's practice!

Retrieval Augmented Generation (RAG) with LangChain

Preparing Video For Download...