## Neo4j Vector Index

> [Neo4j](https://neo4j.com/) is an open-source graph database with integrated support for vector similarity search

It supports: - approximate nearest neighbor search - Euclidean similarity and cosine similarity - Hybrid search combining vector and keyword searches

This notebook shows how to use the Neo4j vector index (`Neo4jVector`).

See the [installation instruction](https://neo4j.com/docs/operations-manual/current/installation/).

```
# Pip install necessary package%pip install --upgrade --quiet  neo4j%pip install --upgrade --quiet  langchain-openai%pip install --upgrade --quiet  tiktoken
```

We want to use `OpenAIEmbeddings` so we have to get the OpenAI API Key.

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
```

```
from langchain_community.docstore.document import Documentfrom langchain_community.document_loaders import TextLoaderfrom langchain_community.vectorstores import Neo4jVectorfrom langchain_openai import OpenAIEmbeddingsfrom langchain_text_splitters import CharacterTextSplitter
```

```
loader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()
```

```
# Neo4jVector requires the Neo4j database credentialsurl = "bolt://localhost:7687"username = "neo4j"password = "password"# You can also use environment variables instead of directly passing named parameters# os.environ["NEO4J_URI"] = "bolt://localhost:7687"# os.environ["NEO4J_USERNAME"] = "neo4j"# os.environ["NEO4J_PASSWORD"] = "pleaseletmein"
```

## Similarity Search with Cosine Distance (Default)[​](#similarity-search-with-cosine-distance-default "Direct link to Similarity Search with Cosine Distance (Default)")

```
# The Neo4jVector Module will connect to Neo4j and create a vector index if needed.db = Neo4jVector.from_documents(    docs, OpenAIEmbeddings(), url=url, username=username, password=password)
```

```
/Users/tomazbratanic/anaconda3/lib/python3.11/site-packages/pandas/core/arrays/masked.py:60: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (version '1.3.5' currently installed).  from pandas.core import (
```

```
query = "What did the president say about Ketanji Brown Jackson"docs_with_score = db.similarity_search_with_score(query, k=2)
```

```
for doc, score in docs_with_score:    print("-" * 80)    print("Score: ", score)    print(doc.page_content)    print("-" * 80)
```

```
--------------------------------------------------------------------------------Score:  0.9076285362243652Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.----------------------------------------------------------------------------------------------------------------------------------------------------------------Score:  0.8912243843078613A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. And if we are to advance liberty and justice, we need to secure the Border and fix the immigration system. We can do both. At our border, we’ve installed new technology like cutting-edge scanners to better detect drug smuggling.  We’ve set up joint patrols with Mexico and Guatemala to catch more human traffickers.  We’re putting in place dedicated immigration judges so families fleeing persecution and violence can have their cases heard faster. We’re securing commitments and supporting partners in South and Central America to host more refugees and secure their own borders.--------------------------------------------------------------------------------
```

## Working with vectorstore[​](#working-with-vectorstore "Direct link to Working with vectorstore")

Above, we created a vectorstore from scratch. However, often times we want to work with an existing vectorstore. In order to do that, we can initialize it directly.

```
index_name = "vector"  # default index namestore = Neo4jVector.from_existing_index(    OpenAIEmbeddings(),    url=url,    username=username,    password=password,    index_name=index_name,)
```

We can also initialize a vectorstore from existing graph using the `from_existing_graph` method. This method pulls relevant text information from the database, and calculates and stores the text embeddings back to the database.

```
# First we create sample data in graphstore.query(    "CREATE (p:Person {name: 'Tomaz', location:'Slovenia', hobby:'Bicycle', age: 33})")
```

```
# Now we initialize from existing graphexisting_graph = Neo4jVector.from_existing_graph(    embedding=OpenAIEmbeddings(),    url=url,    username=username,    password=password,    index_name="person_index",    node_label="Person",    text_node_properties=["name", "location"],    embedding_node_property="embedding",)result = existing_graph.similarity_search("Slovenia", k=1)
```

```
Document(page_content='\nname: Tomaz\nlocation: Slovenia', metadata={'age': 33, 'hobby': 'Bicycle'})
```

### Metadata filtering[​](#metadata-filtering "Direct link to Metadata filtering")

Neo4j vector store also supports metadata filtering by combining parallel runtime and exact nearest neighbor search. _Requires Neo4j 5.18 or greater version._

Equality filtering has the following syntax.

```
existing_graph.similarity_search(    "Slovenia",    filter={"hobby": "Bicycle", "name": "Tomaz"},)
```

```
[Document(page_content='\nname: Tomaz\nlocation: Slovenia', metadata={'age': 33, 'hobby': 'Bicycle'})]
```

Metadata filtering also support the following operators:

*   `$eq: Equal`
*   `$ne: Not Equal`
*   `$lt: Less than`
*   `$lte: Less than or equal`
*   `$gt: Greater than`
*   `$gte: Greater than or equal`
*   `$in: In a list of values`
*   `$nin: Not in a list of values`
*   `$between: Between two values`
*   `$like: Text contains value`
*   `$ilike: lowered text contains value`

```
existing_graph.similarity_search(    "Slovenia",    filter={"hobby": {"$eq": "Bicycle"}, "age": {"$gt": 15}},)
```

```
[Document(page_content='\nname: Tomaz\nlocation: Slovenia', metadata={'age': 33, 'hobby': 'Bicycle'})]
```

```
existing_graph.similarity_search(    "Slovenia",    filter={"hobby": {"$eq": "Bicycle"}, "age": {"$gt": 15}},)
```

```
[Document(page_content='\nname: Tomaz\nlocation: Slovenia', metadata={'age': 33, 'hobby': 'Bicycle'})]
```

You can also use `OR` operator between filters

```
existing_graph.similarity_search(    "Slovenia",    filter={"$or": [{"hobby": {"$eq": "Bicycle"}}, {"age": {"$gt": 15}}]},)
```

```
[Document(page_content='\nname: Tomaz\nlocation: Slovenia', metadata={'age': 33, 'hobby': 'Bicycle'})]
```

### Add documents[​](#add-documents "Direct link to Add documents")

We can add documents to the existing vectorstore.

```
store.add_documents([Document(page_content="foo")])
```

```
['acbd18db4cc2f85cedef654fccc4a4d8']
```

```
docs_with_score = store.similarity_search_with_score("foo")
```

```
(Document(page_content='foo'), 1.0)
```

## Customize response with retrieval query[​](#customize-response-with-retrieval-query "Direct link to Customize response with retrieval query")

You can also customize responses by using a custom Cypher snippet that can fetch other information from the graph. Under the hood, the final Cypher statement is constructed like so:

```
read_query = (  "CALL db.index.vector.queryNodes($index, $k, $embedding) "  "YIELD node, score ") + retrieval_query
```

The retrieval query must return the following three columns:

*   `text`: Union\[str, Dict\] = Value used to populate `page_content` of a document
*   `score`: Float = Similarity score
*   `metadata`: Dict = Additional metadata of a document

Learn more in this [blog post](https://medium.com/neo4j/implementing-rag-how-to-write-a-graph-retrieval-query-in-langchain-74abf13044f2).

```
retrieval_query = """RETURN "Name:" + node.name AS text, score, {foo:"bar"} AS metadata"""retrieval_example = Neo4jVector.from_existing_index(    OpenAIEmbeddings(),    url=url,    username=username,    password=password,    index_name="person_index",    retrieval_query=retrieval_query,)retrieval_example.similarity_search("Foo", k=1)
```

```
[Document(page_content='Name:Tomaz', metadata={'foo': 'bar'})]
```

Here is an example of passing all node properties except for `embedding` as a dictionary to `text` column,

```
retrieval_query = """RETURN node {.name, .age, .hobby} AS text, score, {foo:"bar"} AS metadata"""retrieval_example = Neo4jVector.from_existing_index(    OpenAIEmbeddings(),    url=url,    username=username,    password=password,    index_name="person_index",    retrieval_query=retrieval_query,)retrieval_example.similarity_search("Foo", k=1)
```

```
[Document(page_content='name: Tomaz\nage: 33\nhobby: Bicycle\n', metadata={'foo': 'bar'})]
```

You can also pass Cypher parameters to the retrieval query. Parameters can be used for additional filtering, traversals, etc…

```
retrieval_query = """RETURN node {.*, embedding:Null, extra: $extra} AS text, score, {foo:"bar"} AS metadata"""retrieval_example = Neo4jVector.from_existing_index(    OpenAIEmbeddings(),    url=url,    username=username,    password=password,    index_name="person_index",    retrieval_query=retrieval_query,)retrieval_example.similarity_search("Foo", k=1, params={"extra": "ParamInfo"})
```

```
[Document(page_content='location: Slovenia\nextra: ParamInfo\nname: Tomaz\nage: 33\nhobby: Bicycle\nembedding: None\n', metadata={'foo': 'bar'})]
```

## Hybrid search (vector + keyword)[​](#hybrid-search-vector-keyword "Direct link to Hybrid search (vector + keyword)")

Neo4j integrates both vector and keyword indexes, which allows you to use a hybrid search approach

```
# The Neo4jVector Module will connect to Neo4j and create a vector and keyword indices if needed.hybrid_db = Neo4jVector.from_documents(    docs,    OpenAIEmbeddings(),    url=url,    username=username,    password=password,    search_type="hybrid",)
```

To load the hybrid search from existing indexes, you have to provide both the vector and keyword indices

```
index_name = "vector"  # default index namekeyword_index_name = "keyword"  # default keyword index namestore = Neo4jVector.from_existing_index(    OpenAIEmbeddings(),    url=url,    username=username,    password=password,    index_name=index_name,    keyword_index_name=keyword_index_name,    search_type="hybrid",)
```

## Retriever options[​](#retriever-options "Direct link to Retriever options")

This section shows how to use `Neo4jVector` as a retriever.

```
retriever = store.as_retriever()retriever.get_relevant_documents(query)[0]
```

```
Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'source': '../../modules/state_of_the_union.txt'})
```

## Question Answering with Sources[​](#question-answering-with-sources "Direct link to Question Answering with Sources")

This section goes over how to do question-answering with sources over an Index. It does this by using the `RetrievalQAWithSourcesChain`, which does the lookup of the documents from an Index.

```
from langchain.chains import RetrievalQAWithSourcesChainfrom langchain_openai import ChatOpenAI
```

```
chain = RetrievalQAWithSourcesChain.from_chain_type(    ChatOpenAI(temperature=0), chain_type="stuff", retriever=retriever)
```

```
chain(    {"question": "What did the president say about Justice Breyer"},    return_only_outputs=True,)
```

```
/Users/tomazbratanic/anaconda3/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.  warn_deprecated(
```

```
{'answer': 'The president honored Justice Stephen Breyer for his service to the country.\n', 'sources': '../../modules/state_of_the_union.txt'}
```