Create a retriever

Retrieval chain

To incorporate a retriever and Neo4j vector into a LangChain application, you can create a retrieval chain.

The Neo4jVector class has a as_retriever() method that returns a retriever.

By incorporating Neo4jVector into a RetrievalQA chain, you can use data and vectors in Neo4j in a LangChain application.

Open the 2-llm-rag-python-langchain\retriever_chain.py program.

python
Unresolved directive in lesson.adoc - include::{repository-raw}/main/2-llm-rag-python-langchain/retriever_chain.py[]

The program incorporates the moviePlots vector index into a retrieval chain.

The RetrievalQA chain will use the movie_plot_vector retriever to retrieve documents from the moviePlots index and pass them to the chat_llm language model.

Understanding the results

It can be difficult to understand how the model generated the response and how the retriever affected it.

By setting the optional verbose and return_source_documents arguments to True when creating the RetrievalQA chain, you can see the source documents and the retriever’s score for each document.

python
plot_retriever = RetrievalQA.from_llm(
    llm=chat_llm,
    retriever=movie_plot_vector.as_retriever(),
    verbose=True,
    return_source_documents=True
)

Agent

You can add the plot_retriever chain as a tool to the chat_agent.py program you created earlier. The agent can use the chain to find similar movie plots.

To complete this optional challenge, you will need to update the 2-llm-rag-python-langchain/chat_agent.py program to:

  1. Create the Neo4jVector from the moviePlots vector index.

  2. Create the RetrievalQA chain using the Neo4jVector as the retriever.

  3. Update the tools to use the RetrievalQA chain.

You may need to change the name and description of the tools so the LLM can distinguish between them.

Click to reveal the solution

There is no right or wrong way to complete this challenge. Here is one potential solution.

python
Unresolved directive in lesson.adoc - include::{repository-raw}/main/2-llm-rag-python-langchain/solutions/chat_agent_retriever.py[tag=**]

Lesson Summary

You learned how to create a retriever chain and to incorporate it into a LangChain application.

Next you will learn about Cypher generation.

Chatbot

How can I help you today?