From 37be370ae94154c5c0cb8e1f6f9c03edaef26046 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 --- CONTRIBUTING.md | 69 ++++++++++++++++++++++++ README.md | 66 +++++++++++++++++++++++ examples/openai_search.py | 4 +- examples/similarity_search_for_text.py | 3 +- examples/similarity_search_for_vector.py | 4 +- 5 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..a3047dea --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,69 @@ +# Contributing to the Neo4j Ecosystem + +At [Neo4j](https://neo4j.com/), we develop our software in the open at GitHub. +This provides transparency for you, our users, and allows you to fork the software to make your own additions and enhancements. +We also provide areas specifically for community contributions, in particular the [neo4j-contrib](https://github.com/neo4j-contrib) space. + +There's an active [Neo4j Online Community](https://community.neo4j.com/) where we work directly with the community. +If you're not already a member, sign up! + +We love our community and wouldn't be where we are without you. + + +## Need to raise an issue? + +Where you raise an issue depends largely on the nature of the problem. + +Firstly, if you are an Enterprise customer, you might want to head over to our [Customer Support Portal](https://support.neo4j.com/). + +There are plenty of public channels available too, though. +If you simply want to get started or have a question on how to use a particular feature, ask a question in [Neo4j Online Community](https://community.neo4j.com/). +If you think you might have hit a bug in our software (it happens occasionally!) or you have specific feature request then use the issue feature on the relevant GitHub repository. +Check first though as someone else may have already raised something similar. + +[StackOverflow](https://stackoverflow.com/questions/tagged/neo4j) also hosts a ton of questions and might already have a discussion around your problem. +Make sure you have a look there too. + +Include as much information as you can in any request you make: + +- Which versions of our products are you using? +- Which language (and which version of that language) are you developing with? +- What operating system are you on? +- Are you working with a cluster or on a single machine? +- What code are you running? +- What errors are you seeing? +- What solutions have you tried already? + + +## Want to contribute? + +If you want to contribute a pull request, we have a little bit of process you'll need to follow: + +- Do all your work in a personal fork of the original repository +- [Rebase](https://github.com/edx/edx-platform/wiki/How-to-Rebase-a-Pull-Request), don't merge (we prefer to keep our history clean) +- Create a branch (with a useful name) for your contribution +- Make sure you're familiar with the appropriate coding style (this varies by language so ask if you're in doubt) +- Include unit tests if appropriate (obviously not necessary for documentation changes) +- Take a moment to read and sign our [CLA](https://neo4j.com/developer/cla) + +We can't guarantee that we'll accept pull requests and may ask you to make some changes before they go in. +Occasionally, we might also have logistical, commercial, or legal reasons why we can't accept your work but we'll try to find an alternative way for you to contribute in that case. +Remember that many community members have become regular contributors and some are now even Neo employees! + + +## Specifically for this project +Setting up the development environment: + +1. Install Python 3.9.1+ +2. Install poetry (see https://python-poetry.org/docs/#installation) +3. Install dependencies: + +```shell +poetry install +``` + +4. Install the pre-commit hook, that will do some code-format-checking everytime you commit. + +```shell +pre-commit install +``` diff --git a/README.md b/README.md index d021e195..7909f49e 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 f42f7696..87864bdd 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 3104bbc6..ad28ad0e 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 31045676..4fc4c7ac 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