Skip to content

Commit 1ee7f76

Browse files
authored
Update docs for VectorCypherRetriever (#178)
* Update docs for VectorCypherRetriever * multiline cypher query * add first paragraph for VectorCypherRetriever
1 parent 102d799 commit 1ee7f76

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

docs/source/user_guide_rag.rst

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,11 @@ See also :ref:`vectorretriever`.
543543
Vector Cypher Retriever
544544
=======================
545545

546-
The `VectorCypherRetriever` allows full utilization of Neo4j's graph nature by
547-
enhancing context through graph traversal.
546+
The `VectorCypherRetriever` fully leverages Neo4j's graph capabilities by combining vector-based similarity searches with graph traversal techniques. It processes a query embedding to perform a similarity search against a specified vector index, retrieves relevant node variables, and then executes a Cypher query to traverse the graph based on these nodes. This integration ensures that retrievals are both semantically meaningful and contextually enriched by the underlying graph structure.
547+
548548

549549
Retrieval Query
550-
-----------------------------
550+
---------------
551551

552552
When crafting the retrieval query, it's important to note two available variables
553553
are in the query scope:
@@ -560,26 +560,34 @@ certain movie properties, the retrieval query can be structured as follows:
560560

561561
.. code:: python
562562
563+
retrieval_query = """
564+
MATCH
565+
(actor:Actor)-[:ACTED_IN]->(node)
566+
RETURN
567+
node.title AS movie_title,
568+
node.plot AS movie_plot,
569+
collect(actor.name) AS actors;
570+
"""
563571
retriever = VectorCypherRetriever(
564572
driver,
565573
index_name=INDEX_NAME,
566-
retrieval_query="MATCH (node)<-[:ACTED_IN]-(p:Person) RETURN node.title as movieTitle, node.plot as movieDescription, collect(p.name) as actors, score",
574+
retrieval_query=retrieval_query,
567575
)
568576
569577
578+
It is recommended that the retrieval query returns node properties, as opposed to nodes.
579+
580+
570581
Format the Results
571-
-----------------------------
582+
------------------
572583

573584
.. warning::
574585

575586
This API is in beta mode and will be subject to change in the future.
576587

577-
For improved readability and ease in prompt-engineering, formatting the result to suit
578-
specific needs involves providing a `record_formatter` function to the Cypher retrievers.
579-
This function processes the Neo4j record from the retrieval query, returning a
580-
`RetrieverResultItem` with `content` (str) and `metadata` (dict) fields. The `content`
581-
field is used for passing data to the LLM, while `metadata` can serve debugging purposes
582-
and provide additional context.
588+
The result_formatter function customizes the output of Cypher retrievers for improved prompt engineering and readability. It converts each Neo4j record into a RetrieverResultItem with two fields: `content` and `metadata`.
589+
590+
The `content` field is a formatted string containing the key information intended for the language model, such as movie titles or descriptions. The `metadata` field holds additional details, useful for debugging or providing extra context, like scores or node properties.
583591

584592

585593
.. code:: python
@@ -738,7 +746,7 @@ Also note that there is an helper function to create a full-text index (see `the
738746
.. _hybrid-cypher-retriever-user-guide:
739747

740748
Hybrid Cypher Retrievers
741-
------------------------------------
749+
------------------------
742750

743751
In an hybrid cypher retriever, results are searched for in both a vector and a
744752
full-text index. Once the similar nodes are identified, a retrieval query can traverse

src/neo4j_graphrag/retrievers/vector.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ class VectorCypherRetriever(Retriever):
220220
221221
Note: `node` is a variable from the base query that can be used in `retrieval_query` as seen in the example below.
222222
223+
The retrieval_query is additional Cypher that can allow for graph traversal after retrieving `node`.
224+
223225
Example:
224226
225227
.. code-block:: python
@@ -243,6 +245,7 @@ class VectorCypherRetriever(Retriever):
243245
result_formatter (Optional[Callable[[neo4j.Record], RetrieverResultItem]]): Provided custom function to transform a neo4j.Record to a RetrieverResultItem.
244246
neo4j_database (Optional[str]): The name of the Neo4j database. If not provided, this defaults to "neo4j" in the database (`see reference to documentation <https://neo4j.com/docs/operations-manual/current/database-administration/#manage-databases-default>`_).
245247
248+
Read more in the :ref:`User Guide <vector-cypher-retriever-user-guide>`.
246249
"""
247250

248251
def __init__(

0 commit comments

Comments
 (0)