-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds HybridCypherRetriever, refactor query construction, add tests
- Loading branch information
Showing
8 changed files
with
321 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from neo4j import GraphDatabase | ||
|
||
from random import random | ||
from neo4j_genai import HybridCypherRetriever | ||
from neo4j_genai.embedder import Embedder | ||
from neo4j_genai.indexes import create_vector_index, create_fulltext_index | ||
|
||
URI = "neo4j://localhost:7687" | ||
AUTH = ("neo4j", "password") | ||
|
||
INDEX_NAME = "embedding-name" | ||
FULLTEXT_INDEX_NAME = "fulltext-index-name" | ||
DIMENSION = 1536 | ||
|
||
# Connect to Neo4j database | ||
driver = GraphDatabase.driver(URI, auth=AUTH) | ||
|
||
|
||
# Create Embedder object | ||
class CustomEmbedder(Embedder): | ||
def embed_query(self, text: str) -> list[float]: | ||
return [random() for _ in range(DIMENSION)] | ||
|
||
|
||
embedder = CustomEmbedder() | ||
|
||
# Creating the index | ||
create_vector_index( | ||
driver, | ||
INDEX_NAME, | ||
label="Document", | ||
property="propertyKey", | ||
dimensions=DIMENSION, | ||
similarity_fn="euclidean", | ||
) | ||
create_fulltext_index( | ||
driver, FULLTEXT_INDEX_NAME, label="Document", node_properties=["propertyKey"] | ||
) | ||
|
||
# Initialize the retriever | ||
retrieval_query = "MATCH (node)-[:AUTHORED_BY]->(author:Author)" "RETURN author.name" | ||
retriever = HybridCypherRetriever( | ||
driver, INDEX_NAME, FULLTEXT_INDEX_NAME, retrieval_query, embedder | ||
) | ||
|
||
# Upsert the query | ||
vector = [random() for _ in range(DIMENSION)] | ||
insert_query = ( | ||
"MERGE (n:Document {id: $id})" | ||
"WITH n " | ||
"CALL db.create.setNodeVectorProperty(n, 'propertyKey', $vector)" | ||
"RETURN n" | ||
) | ||
parameters = { | ||
"id": 0, | ||
"vector": vector, | ||
} | ||
driver.execute_query(insert_query, parameters) | ||
|
||
# Perform the similarity search for a text query | ||
query_text = "Who are the fremen?" | ||
print(retriever.search(query_text=query_text, top_k=5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [https://neo4j.com] | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# # | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from neo4j_genai.queries import get_search_query | ||
from neo4j_genai.types import SearchType | ||
|
||
|
||
def test_vector_search_basic(): | ||
expected = ( | ||
"CALL db.index.vector.queryNodes($index, $k, $embedding) " "RETURN node, score" | ||
) | ||
result = get_search_query(SearchType.VECTOR) | ||
assert result == expected | ||
|
||
|
||
def test_hybrid_search_basic(): | ||
expected = ( | ||
"CALL { " | ||
"CALL db.index.vector.queryNodes($vector_index_name, $top_k, $query_vector) " | ||
"YIELD node, score " | ||
"RETURN node, score UNION " | ||
"CALL db.index.fulltext.queryNodes($fulltext_index_name, $query_text, {limit: $top_k}) " | ||
"YIELD node, score " | ||
"WITH collect({node:node, score:score}) AS nodes, max(score) AS max " | ||
"UNWIND nodes AS n " | ||
"RETURN n.node AS node, (n.score / max) AS score " | ||
"} " | ||
"WITH node, max(score) AS score ORDER BY score DESC LIMIT $top_k " | ||
"RETURN node, score" | ||
) | ||
result = get_search_query(SearchType.HYBRID) | ||
assert result == expected | ||
|
||
|
||
def test_vector_search_with_properties(): | ||
properties = ["name", "age"] | ||
expected = ( | ||
"CALL db.index.vector.queryNodes($index, $k, $embedding) " | ||
"YIELD node, score " | ||
"RETURN node {.name, .age} as node, score" | ||
) | ||
result = get_search_query(SearchType.VECTOR, return_properties=properties) | ||
assert result == expected | ||
|
||
|
||
def test_hybrid_search_with_retrieval_query(): | ||
retrieval_query = "MATCH (n) RETURN n LIMIT 10" | ||
expected = ( | ||
"CALL { " | ||
"CALL db.index.vector.queryNodes($vector_index_name, $top_k, $query_vector) " | ||
"YIELD node, score " | ||
"RETURN node, score UNION " | ||
"CALL db.index.fulltext.queryNodes($fulltext_index_name, $query_text, {limit: $top_k}) " | ||
"YIELD node, score " | ||
"WITH collect({node:node, score:score}) AS nodes, max(score) AS max " | ||
"UNWIND nodes AS n " | ||
"RETURN n.node AS node, (n.score / max) AS score " | ||
"} " | ||
"WITH node, max(score) AS score ORDER BY score DESC LIMIT $top_k " | ||
+ retrieval_query | ||
) | ||
result = get_search_query(SearchType.HYBRID, retrieval_query=retrieval_query) | ||
assert result == expected |
Oops, something went wrong.