Working with Retrievers

Introduction

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

Hands-On: Retriever Notebook

Open the notebook: 01_Retrievers_notebook.ipynb

This notebook demonstrates:

  1. Setting up retrievers with the knowledge graph we built

  2. Vector retrieval for semantic search

  3. Hybrid retrieval combining vectors and graph traversal

  4. Text2Cypher for structured queries

  5. Comparing results from different retrieval methods

== What You’ll Build

As you work through the notebook, 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?