Graph Retrieval

You can enhance a vector retriever using GraphRAG to include additional context.

In this lesson, you will update the vector retriever to retriever additional metadata from the graph after the similarity search.

GraphRAG

You can add an additional Cypher retrieval query to the Neo4jVector class. The retrieval query is run after the similarity search and the data it returns is added to the Document metadata.

You can use this retrieval query to retrieve useful context from the graph.

In the movie plot example, you could retrieve additional information about the movies, such as the actors or user ratings.

The additional context can be used to improve and expand the agent’s responses, for example:

Who acts in movies about Love and Romance?

The vector retriever will return movies about Love and Romance, the Cypher retrieval query will return the actors in those movies, and the agent can use this information to answer the question.

This method of vector + graph retrieval is a common approach to GraphRAG (Graph Retrieval Augmented Generation).

Retrieval Query

Open the genai-integration-langchain/vector_graph_retriever.py file:

python
vector_graph_retriever.py
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/vector_graph_retriever.py[tags=**]

This is the same code as the vector retriever agent you created.

You need to define a retrieval_query that will be used to supplement the results of the similarity search.

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/vector_graph_retriever.py[tag=retrieval_query]

The query receives the node and score variables yielded by the vector search.

The query traverses the graph to find related nodes for genres and actors, as well as sorting the results by the user rating.

Vector Store

You can now update the Neo4jVector to use the retrieval_query:

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/vector_graph_retriever.py[tag=plot_vector]

The retrieve function will add the additional context to the agent when the similarity_search method is used.

Click to view the complete code
python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/vector_graph_retriever.py[tags="**;!examples"]

Questions will generate a context that includes the movie plots, genres, actors, and user ratings. The agent will then use this context to generate a more accurate response.

[question]
Who acts in movies about Love and Romance?
[answer]
Audrey Hepburn, Gregory Peck, Christian Slater, Mary Stuart Masterson,
Robert Redford, Michelle Pfeiffer, and Cary Grant act in movies about
love and romance.

Run the application, review the additional context, and experiment with different questions, for example:

Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/vector_graph_retriever.py[tag=examples]

A GraphRAG retriever allows you to combine the power of vector search with the rich context of a graph database, enabling more accurate and context-aware responses.

Continue

When you are ready, continue to the next lesson.

Lesson Summary

In this lesson, you learned how to enhance a vector retriever with a Cypher retrieval query to create a GraphRAG retriever.

In the next challenge, you will explore add additional data to the retrieval query and test the agents ability to answer more complex questions.