Working with Retrievers

Introduction

In this lesson, you will work hands-on with all three retriever types to see how they work in practice.

There are three Jupyter notebooks to explore:

  1. 01_01_vector_retriever.ipynb - Vector Retriever

  2. 01_02_vector_cypher_retriever.ipynb - Vector + Cypher Retriever

  3. 01_03_text2cypher_retriever.ipynb - Text2Cypher Retriever

Hands-On: Retriever Notebooks

Open the first notebook: 01_01_vector_retriever.ipynb

Each notebook demonstrates:

  1. Setting up the retriever with the knowledge graph we built

  2. Customizing the retriever for your requirements

  3. Using the retriever as part of a GraphRAG pipeline

  4. Comparing results from different retrieval methods

What You’ll Build:

As you work through the notebooks, take time to review the code snippets and understand how each retriever is initialized and used.

Note how each retriever has its place in a complete GraphRAG system!

Initialize Models:

All retrievers will use the same LLM and embedder for consistency:

python
from neo4j_graphrag.retrievers import VectorRetriever, VectorCypherRetriever, Text2CypherRetriever

llm = OpenAILLM(model_name="gpt-4o", api_key=OPENAI_API_KEY)
embedder = OpenAIEmbeddings(api_key=OPENAI_API_KEY)

Vector Retriever:

  • Returns semantically similar text chunks

  • Good for exploratory questions

  • May miss entity-specific context

python
vector_retriever = VectorRetriever(
    driver=driver,
    index_name="chunkEmbeddings",
    embedder=embedder
)

Vector + Cypher Retriever:

  • Provides both content and relationships

  • Richer context with entity information

  • Better for comprehensive answers

python
vector_cypher_retriever = VectorCypherRetriever(
    driver=driver,
    index_name="chunkEmbeddings",
    retrieval_query=cypher_query,
    embedder=embedder
)

Text2Cypher Retriever:

  • Direct, precise answers from graph structure

  • Perfect for factual queries

  • Handles aggregations and counts

python
text2cypher_retriever = Text2CypherRetriever(
    driver=driver,
    llm=llm,
    neo4j_schema=neo4j_schema
)

Testing Different Retrievers

Try these questions with each retriever:

Broad Semantic Questions:

  • "What are the main cybersecurity threats in financial services?"

  • "Tell me about risk factors mentioned in the documents"

Entity-Specific Questions:

  • "What products does Apple mention in their filings?"

  • "Which companies face regulatory challenges?"

Precise Data Questions:

  • "How many companies mention cloud computing?"

  • "Count the risk factors for Microsoft"

Compare the results - you’ll see how each retriever approaches the same question differently!

Summary

In this hands-on lesson, you worked with all three retriever types in practice:

What You Built:

  • Vector Retriever for semantic search

  • Vector + Cypher Retriever for contextual search

  • Text2Cypher Retriever for precise queries

Key Insights:

  • Different retrievers excel at different question types

  • Combining approaches gives comprehensive coverage

  • Understanding retriever strengths guides selection

Preparation:

  • You now understand how each retriever works in practice

  • You’ve seen their different strengths and limitations

  • You’re ready to wrap these retrievers as conversational agent tools

In the next module, you will learn how to combine these retrievers into intelligent agents that can choose the right retrieval method automatically.

Chatbot

How can I help you today?