Skip to content

Commit 7c24e70

Browse files
authored
Documentation updates (neo4j#70)
* 0.2.1 documentation update * Docstring updates
1 parent a99a343 commit 7c24e70

File tree

9 files changed

+244
-107
lines changed

9 files changed

+244
-107
lines changed

README.md

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,6 @@ pip install neo4j-genai
2222

2323
## Examples
2424

25-
While the library has more retrievers than shown here, the following examples should be able to get you started.
26-
27-
### Performing a similarity search
28-
29-
Assumption: Neo4j running with populated vector index in place.
30-
31-
In the following example, we use a simple vector search as retriever,
32-
that will perform a similarity search over the `index-name` vector index
33-
in Neo4j.
34-
35-
```python
36-
from neo4j import GraphDatabase
37-
from neo4j_genai.retrievers import VectorRetriever
38-
from neo4j_genai.llm import OpenAILLM
39-
from neo4j_genai.generation import GraphRAG
40-
from neo4j_genai.embeddings.openai import OpenAIEmbeddings
41-
42-
URI = "neo4j://localhost:7687"
43-
AUTH = ("neo4j", "password")
44-
45-
INDEX_NAME = "index-name"
46-
47-
# Connect to Neo4j database
48-
driver = GraphDatabase.driver(URI, auth=AUTH)
49-
50-
# Create Embedder object
51-
embedder = OpenAIEmbeddings(model="text-embedding-3-large")
52-
53-
# Initialize the retriever
54-
retriever = VectorRetriever(driver, INDEX_NAME, embedder)
55-
56-
# Initialize the LLM
57-
# Note: the OPENAI_API_KEY must be in the env vars
58-
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})
59-
60-
# Initialize the RAG pipeline
61-
rag = GraphRAG(retriever=retriever, llm=llm)
62-
63-
# Query the graph
64-
query_text = "How do I do similarity search in Neo4j?"
65-
response = rag.search(query_text=query_text, retriever_config={"top_k": 5})
66-
print(response.answer)
67-
```
68-
6925
### Creating a vector index
7026

7127
When creating a vector index, make sure you match the number of dimensions in the index with the number of dimensions the embeddings have.
@@ -79,7 +35,7 @@ from neo4j_genai.indexes import create_vector_index
7935
URI = "neo4j://localhost:7687"
8036
AUTH = ("neo4j", "password")
8137

82-
INDEX_NAME = "chunk-index"
38+
INDEX_NAME = "vector-index-name"
8339

8440
# Connect to Neo4j database
8541
driver = GraphDatabase.driver(URI, auth=AUTH)
@@ -98,8 +54,8 @@ create_vector_index(
9854

9955
### Populating the Neo4j Vector Index
10056

101-
This library does not write to the database, that is up to you.
102-
See below for how to write using Cypher via the Neo4j driver.
57+
This library does not provide functionality to write data to the database.
58+
See below for how to do this using [the Neo4j Python driver](https://github.com/neo4j/neo4j-python-driver).
10359

10460
Assumption: Neo4j running with a defined vector index
10561

@@ -115,19 +71,65 @@ driver = GraphDatabase.driver(URI, auth=AUTH)
11571

11672
# Upsert the vector
11773
vector = [random() for _ in range(DIMENSION)]
118-
insert_query = (
119-
"MERGE (n:Document {id: $id})"
120-
"WITH n "
121-
"CALL db.create.setNodeVectorProperty(n, 'vectorProperty', $vector)"
122-
"RETURN n"
123-
)
74+
insert_query = """
75+
MERGE (n:Document {id: $id})
76+
WITH n
77+
CALL db.create.setNodeVectorProperty(n, 'vectorProperty', $vector)
78+
RETURN n
79+
"""
12480
parameters = {
12581
"id": 0,
12682
"vector": vector,
12783
}
12884
driver.execute_query(insert_query, parameters)
12985
```
13086

87+
### Performing a similarity search
88+
89+
Assumption: Neo4j running with populated vector index in place.
90+
91+
Limitation: The query over the vector index is an _approximate_ nearest neighbor search and may not give exact results. [See this reference for more details](https://neo4j.com/docs/cypher-manual/current/indexes/semantic-indexes/vector-indexes/#_limitiations_and_known_issues).
92+
93+
While the library has more retrievers than shown here, the following examples should be able to get you started.
94+
95+
In the following example, we use a simple vector search as retriever,
96+
that will perform a similarity search over the `index-name` vector index
97+
in Neo4j.
98+
99+
```python
100+
from neo4j import GraphDatabase
101+
from neo4j_genai.retrievers import VectorRetriever
102+
from neo4j_genai.llm import OpenAILLM
103+
from neo4j_genai.generation import GraphRAG
104+
from neo4j_genai.embeddings.openai import OpenAIEmbeddings
105+
106+
URI = "neo4j://localhost:7687"
107+
AUTH = ("neo4j", "password")
108+
109+
INDEX_NAME = "vector-index-name"
110+
111+
# Connect to Neo4j database
112+
driver = GraphDatabase.driver(URI, auth=AUTH)
113+
114+
# Create Embedder object
115+
embedder = OpenAIEmbeddings(model="text-embedding-3-large")
116+
117+
# Initialize the retriever
118+
retriever = VectorRetriever(driver, INDEX_NAME, embedder)
119+
120+
# Initialize the LLM
121+
# Note: An OPENAI_API_KEY environment variable is required here
122+
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})
123+
124+
# Initialize the RAG pipeline
125+
rag = GraphRAG(retriever=retriever, llm=llm)
126+
127+
# Query the graph
128+
query_text = "How do I do similarity search in Neo4j?"
129+
response = rag.search(query_text=query_text, retriever_config={"top_k": 5})
130+
print(response.answer)
131+
```
132+
131133
# Development
132134

133135
## Install dependencies

docs/source/api.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
API Documentation
44
#################
55

6+
.. _retrievers-section:
7+
68
**********
79
Retrievers
810
**********
@@ -63,6 +65,21 @@ PineconeNeo4jRetriever
6365
:members: search
6466

6567

68+
********************
69+
Database Interaction
70+
********************
71+
72+
.. _create-vector-index:
73+
74+
.. autofunction:: neo4j_genai.indexes.create_vector_index
75+
76+
.. autofunction:: neo4j_genai.indexes.create_fulltext_index
77+
78+
.. autofunction:: neo4j_genai.indexes.drop_index_if_exists
79+
80+
.. autofunction:: neo4j_genai.indexes.upsert_vector
81+
82+
6683
******
6784
Errors
6885
******

docs/source/index.rst

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,14 @@ To install the latest stable version, use:
5454
Examples
5555
********
5656

57-
While the library has more retrievers than shown here, the following examples should be able to get you started.
58-
59-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60-
Performing a similarity search
61-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62-
63-
.. code:: python
64-
65-
from neo4j import GraphDatabase
66-
from neo4j_genai.retrievers import VectorRetriever
67-
from langchain_openai import OpenAIEmbeddings
68-
69-
URI = "neo4j://localhost:7687"
70-
AUTH = ("neo4j", "password")
71-
72-
INDEX_NAME = "embedding-name"
73-
74-
# Connect to Neo4j database
75-
driver = GraphDatabase.driver(URI, auth=AUTH)
76-
77-
# Create Embedder object
78-
embedder = OpenAIEmbeddings(model="text-embedding-3-large")
79-
80-
# Initialize the retriever
81-
retriever = VectorRetriever(driver, INDEX_NAME, embedder)
82-
83-
# Run the similarity search
84-
query_text = "How do I do similarity search in Neo4j?"
85-
response = retriever.search(query_text=query_text, top_k=5)
86-
87-
.. note::
88-
89-
Assumption: Neo4j running with populated vector index in place.
90-
9157
~~~~~~~~~~~~~~~~~~~~~~~
9258
Creating a vector index
9359
~~~~~~~~~~~~~~~~~~~~~~~
9460

9561
When creating a vector index, make sure you match the number of dimensions in the index with the number of dimensions the embeddings have.
9662

63+
See :ref:`the API documentation<create-vector-index>` for more details.
64+
9765
.. code:: python
9866
9967
from neo4j import GraphDatabase
@@ -102,7 +70,7 @@ When creating a vector index, make sure you match the number of dimensions in th
10270
URI = "neo4j://localhost:7687"
10371
AUTH = ("neo4j", "password")
10472
105-
INDEX_NAME = "chunk-index"
73+
INDEX_NAME = "vector-index-name"
10674
10775
# Connect to Neo4j database
10876
driver = GraphDatabase.driver(URI, auth=AUTH)
@@ -119,14 +87,15 @@ When creating a vector index, make sure you match the number of dimensions in th
11987
12088
.. note::
12189

122-
Assumption: Neo4j running
90+
Assumed Neo4j is running
12391

12492
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12593
Populating the Neo4j Vector Index
12694
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12795

128-
This library does not write to the database, that is up to you.
129-
See below for how to write using Cypher via the Neo4j driver.
96+
This library does not provide functionality to write data to the database.
97+
See below for writing data using `the Neo4j Python driver <https://github.com/neo4j/neo4j-python-driver>`_.
98+
13099

131100
.. code:: python
132101
@@ -141,12 +110,12 @@ See below for how to write using Cypher via the Neo4j driver.
141110
142111
# Upsert the vector
143112
vector = [random() for _ in range(DIMENSION)]
144-
insert_query = (
145-
"MERGE (n:Document {id: $id})"
146-
"WITH n "
147-
"CALL db.create.setNodeVectorProperty(n, 'vectorProperty', $vector)"
148-
"RETURN n"
149-
)
113+
insert_query = """
114+
MERGE (n:Document {id: $id})
115+
WITH n
116+
CALL db.create.setNodeVectorProperty(n, 'vectorProperty', $vector)
117+
RETURN n
118+
"""
150119
parameters = {
151120
"id": 0,
152121
"vector": vector,
@@ -156,7 +125,49 @@ See below for how to write using Cypher via the Neo4j driver.
156125
157126
.. note::
158127

159-
Assumption: Neo4j running with a defined vector index
128+
Assumed Neo4j is running with a defined vector index
129+
130+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131+
Performing a similarity search
132+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133+
134+
While the library has more retrievers than shown here, the following examples should be able to get you started.
135+
136+
.. code:: python
137+
138+
from neo4j import GraphDatabase
139+
from neo4j_genai.retrievers import VectorRetriever
140+
from langchain_openai import OpenAIEmbeddings
141+
142+
URI = "neo4j://localhost:7687"
143+
AUTH = ("neo4j", "password")
144+
145+
INDEX_NAME = "vector-index-name"
146+
147+
# Connect to Neo4j database
148+
driver = GraphDatabase.driver(URI, auth=AUTH)
149+
150+
# Create Embedder object
151+
# Note: An OPENAI_API_KEY environment variable is required here
152+
embedder = OpenAIEmbeddings(model="text-embedding-3-large")
153+
154+
# Initialize the retriever
155+
retriever = VectorRetriever(driver, INDEX_NAME, embedder)
156+
157+
# Run the similarity search
158+
query_text = "How do I do similarity search in Neo4j?"
159+
response = retriever.search(query_text=query_text, top_k=5)
160+
161+
.. note::
162+
163+
Assumed Neo4j is running with populated vector index in place.
164+
165+
***********
166+
Limitations
167+
***********
168+
169+
The query over the vector index is an *approximate* nearest neighbor search and may not give exact results. `See this reference for more details <https://neo4j.com/docs/cypher-manual/current/indexes/semantic-indexes/vector-indexes/#_limitiations_and_known_issues>`_.
170+
160171

161172
Development
162173
===========

docs/source/rag.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ source information from external data stores to augment generated responses.
1111

1212
This package enables Python developers to perform RAG using Neo4j.
1313

14+
For more information about different retrieval strategies, see `the API documentation <api.html#retrievers-section>`_.
15+
1416

1517
************************************
1618
Overview
@@ -27,7 +29,7 @@ Overview
2729
URI = "neo4j://localhost:7687"
2830
AUTH = ("neo4j", "password")
2931
30-
INDEX_NAME = "index-name"
32+
INDEX_NAME = "vector-index-name"
3133
3234
# Connect to Neo4j database
3335
driver = GraphDatabase.driver(URI, auth=AUTH)

0 commit comments

Comments
 (0)