From e6db0e91b9ee00f704df5a06136df9a9e4c13d3c Mon Sep 17 00:00:00 2001 From: Will Tai Date: Mon, 8 Apr 2024 17:22:24 +0100 Subject: [PATCH] Added content to README --- README.md | 66 ++++++++++++++++++++++++ examples/openai_search.py | 4 +- examples/similarity_search_for_text.py | 3 +- examples/similarity_search_for_vector.py | 4 +- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d021e195b..7909f49ef 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,69 @@ # Neo4j GenAI package for Python This repository contains the official Neo4j GenAI features for Python. + +## Installation + +This package requires Python (>=3.8.1). + +To install the latest stable version, use: + +```shell +pip install neo4j-genai +``` + +## Example +After setting up a Neo4j database instance: +```python +from neo4j import GraphDatabase +from neo4j_genai import VectorRetriever + +from random import random + +from neo4j_genai.indexes import create_vector_index + +URI = "neo4j://localhost:7687" +AUTH = ("neo4j", "password") + +INDEX_NAME = "embedding-name" +DIMENSION = 1536 + +# Connect to Neo4j database +driver = GraphDatabase.driver(URI, auth=AUTH) + +# Creating the index +create_vector_index( + driver, + INDEX_NAME, + label="Document", + property="propertyKey", + dimensions=DIMENSION, + similarity_fn="euclidean", +) + +# Initialize the retriever +retriever = VectorRetriever(driver, INDEX_NAME) + +# Upsert the vector +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 vector query +query_vector = [random() for _ in range(DIMENSION)] +print(retriever.search(query_vector=query_vector, top_k=5)) + +``` + +## Further information +- [The official Neo4j Python driver](https://github.com/neo4j/neo4j-python-driver) +- [Neo4j GenAI integrations](https://neo4j.com/docs/cypher-manual/current/genai-integrations/) diff --git a/examples/openai_search.py b/examples/openai_search.py index f42f76962..87864bdd9 100644 --- a/examples/openai_search.py +++ b/examples/openai_search.py @@ -34,13 +34,15 @@ # Upsert the query vector = [random() for _ in range(DIMENSION)] + insert_query = ( - "MERGE (n:Document)" + "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) diff --git a/examples/similarity_search_for_text.py b/examples/similarity_search_for_text.py index 3104bbc6e..ad28ad0e8 100644 --- a/examples/similarity_search_for_text.py +++ b/examples/similarity_search_for_text.py @@ -40,12 +40,13 @@ def embed_query(self, text: str) -> List[float]: # Upsert the query vector = [random() for _ in range(DIMENSION)] insert_query = ( - "MERGE (n:Document)" + "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) diff --git a/examples/similarity_search_for_vector.py b/examples/similarity_search_for_vector.py index 310456760..4fc4c7ac8 100644 --- a/examples/similarity_search_for_vector.py +++ b/examples/similarity_search_for_vector.py @@ -30,15 +30,15 @@ # Upsert the vector vector = [random() for _ in range(DIMENSION)] insert_query = ( - "MERGE (n:Document)" + "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 vector query