From a378ac30db179c032aa29c365552fb0ac56994af Mon Sep 17 00:00:00 2001 From: Oskar Hane Date: Thu, 25 Apr 2024 16:48:33 +0200 Subject: [PATCH 1/5] Update README --- README.md | 94 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index cc231d85..0c57d3f5 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ This repository contains the official Neo4j GenAI features for Python. +The purpose of this package is to provide a first party package to developers, +where Neo4j can guarantee long term commitment and maintenance as well as being +fast to ship new features and high performing patterns and methods. + # Usage ## Installation @@ -14,23 +18,48 @@ To install the latest stable version, use: pip install neo4j-genai ``` -## Example +## Examples + +While the library have more retrievers than shown here, the following examples should be able to get you started. + +### Performing a simple similarity search -After setting up a Neo4j database instance: +Assumptions: Neo4j running with populated vector index in place. ```python from neo4j import GraphDatabase from neo4j_genai import VectorRetriever -from random import random +URI = "neo4j://localhost:7687" +AUTH = ("neo4j", "password") + +INDEX_NAME = "embedding-name" + +# Connect to Neo4j database +driver = GraphDatabase.driver(URI, auth=AUTH) +# Initialize the retriever +retriever = VectorRetriever(driver, INDEX_NAME) + +# Perform the similarity search for a vector query +query_text = "How do I do similarity search in Neo4j?" +response = retriever.search(query_text=query_text, top_k=5) +``` + +### Creating a vector index + +When creating a vector index, make sure you match the number of dimensions in the index with the number of dimensions the embeddings have. + +Assumptions: Neo4j running + +```python +from neo4j import GraphDatabase from neo4j_genai.indexes import create_vector_index URI = "neo4j://localhost:7687" AUTH = ("neo4j", "password") -INDEX_NAME = "embedding-name" -DIMENSION = 1536 +INDEX_NAME = "chunk-index" # Connect to Neo4j database driver = GraphDatabase.driver(URI, auth=AUTH) @@ -40,20 +69,36 @@ create_vector_index( driver, INDEX_NAME, label="Document", - property="propertyKey", - dimensions=DIMENSION, + property="textProperty", + dimensions=1536, similarity_fn="euclidean", ) -# Initialize the retriever -retriever = VectorRetriever(driver, INDEX_NAME) +``` + +### Populating the Neo4j Vector Index + +This library does not write to the database, that is up to you. +See below for how to write using Cypher via the Neo4j driver. + +Assumptions: Neo4j running with a defined vector index + +```python +from neo4j import GraphDatabase +from random import random + +URI = "neo4j://localhost:7687" +AUTH = ("neo4j", "password") + +# Connect to Neo4j database +driver = GraphDatabase.driver(URI, auth=AUTH) # 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)" + "CALL db.create.setNodeVectorProperty(n, 'textProperty', $vector)" "RETURN n" ) parameters = { @@ -61,11 +106,6 @@ parameters = { "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)) - ``` # Development @@ -76,7 +116,6 @@ print(retriever.search(query_vector=query_vector, top_k=5)) poetry install ``` - ## Getting started ### Issues @@ -102,18 +141,17 @@ and/or [Discord](https://discord.gg/neo4j). When you're finished with your changes, create a pull request, also known as a PR. -* Ensure that you have [signed the CLA](https://neo4j.com/developer/contributing-code/#sign-cla). -* Ensure that the base of your PR is set to `main`. -* Don't forget to [link your PR to an issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) -if you are solving one. -* Enable the checkbox to [allow maintainer edits](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork) -so that maintainers can make any necessary tweaks and update your branch for merge. -* Reviewers may ask for changes to be made before a PR can be merged, either using -[suggested changes](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/incorporating-feedback-in-your-pull-request) -or normal pull request comments. You can apply suggested changes directly through -the UI, and any other changes can be made in your fork and committed to the PR branch. -* As you update your PR and apply changes, mark each conversation as [resolved](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request#resolving-conversations). - +- Ensure that you have [signed the CLA](https://neo4j.com/developer/contributing-code/#sign-cla). +- Ensure that the base of your PR is set to `main`. +- Don't forget to [link your PR to an issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) + if you are solving one. +- Enable the checkbox to [allow maintainer edits](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork) + so that maintainers can make any necessary tweaks and update your branch for merge. +- Reviewers may ask for changes to be made before a PR can be merged, either using + [suggested changes](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/incorporating-feedback-in-your-pull-request) + or normal pull request comments. You can apply suggested changes directly through + the UI, and any other changes can be made in your fork and committed to the PR branch. +- As you update your PR and apply changes, mark each conversation as [resolved](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request#resolving-conversations). ## Run tests From 30e3d4960bc0b97bf7b6bee0810cf5dc53228f0f Mon Sep 17 00:00:00 2001 From: Oskar Hane Date: Fri, 26 Apr 2024 09:52:37 +0200 Subject: [PATCH 2/5] Update README.md Co-authored-by: willtai --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c57d3f5..9da43e77 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ pip install neo4j-genai ## Examples -While the library have more retrievers than shown here, the following examples should be able to get you started. +While the library has more retrievers than shown here, the following examples should be able to get you started. ### Performing a simple similarity search From 115e42aea2c261b58be5bcc5a0dc122757927344 Mon Sep 17 00:00:00 2001 From: Oskar Hane Date: Fri, 26 Apr 2024 12:03:17 +0200 Subject: [PATCH 3/5] Update README.md Co-authored-by: willtai --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9da43e77..556d7a87 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ While the library has more retrievers than shown here, the following examples sh ### Performing a simple similarity search -Assumptions: Neo4j running with populated vector index in place. +Assumption: Neo4j running with populated vector index in place. ```python from neo4j import GraphDatabase From f619e505133a6deca5742b7aab6de2aa103b608d Mon Sep 17 00:00:00 2001 From: Oskar Hane Date: Fri, 26 Apr 2024 12:03:22 +0200 Subject: [PATCH 4/5] Update README.md Co-authored-by: willtai --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 556d7a87..063180ea 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ response = retriever.search(query_text=query_text, top_k=5) When creating a vector index, make sure you match the number of dimensions in the index with the number of dimensions the embeddings have. -Assumptions: Neo4j running +Assumption: Neo4j running ```python from neo4j import GraphDatabase From 982eeb43084f75f7778333fe51f4cc61ddc8b619 Mon Sep 17 00:00:00 2001 From: Oskar Hane Date: Fri, 26 Apr 2024 12:03:27 +0200 Subject: [PATCH 5/5] Update README.md Co-authored-by: willtai --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 063180ea..a6507970 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ create_vector_index( This library does not write to the database, that is up to you. See below for how to write using Cypher via the Neo4j driver. -Assumptions: Neo4j running with a defined vector index +Assumption: Neo4j running with a defined vector index ```python from neo4j import GraphDatabase