-
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 HybridSearchRetriever and creates abstract base class Retriever
- Loading branch information
Showing
6 changed files
with
191 additions
and
28 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.embedder import Embedder | ||
from neo4j_genai.indexes import create_vector_index, drop_index, create_fulltext_index | ||
from neo4j_genai.retrievers import HybridSearchRetriever | ||
|
||
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 | ||
drop_index(driver, INDEX_NAME) | ||
drop_index(driver, FULLTEXT_INDEX_NAME) | ||
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 | ||
retriever = HybridSearchRetriever(driver, INDEX_NAME, FULLTEXT_INDEX_NAME, 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 = "hello world" | ||
fulltext_query = "fremen" | ||
print(retriever.search(query_text=query_text, fulltext_query=fulltext_query, 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