-
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 GenAIClient class and examples
- Loading branch information
Showing
18 changed files
with
892 additions
and
112 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 |
---|---|---|
@@ -1 +1,4 @@ | ||
dist/ | ||
**/__pycache__/* | ||
*.py[cod] | ||
.mypy_cache/ |
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,6 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.5.0 # Use the ref you want to point at | ||
hooks: | ||
- id: trailing-whitespace | ||
# - id: ... |
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,3 @@ | ||
# Neo4j GenAI package for Python | ||
|
||
This repository contains the official Neo4j GenAI features for Python. |
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,54 @@ | ||
from typing import List | ||
from neo4j import GraphDatabase | ||
from neo4j.exceptions import DatabaseError | ||
from neo4j_genai.client import GenAIClient | ||
|
||
from random import random | ||
from neo4j_genai.embeddings import Embeddings | ||
|
||
URI = "neo4j://localhost:7687" | ||
AUTH = ("neo4j", "password") | ||
|
||
INDEX_NAME = "embedding-name" | ||
DIMENSION = 1536 | ||
|
||
# Connect to Neo4j database | ||
driver = GraphDatabase.driver(URI, auth=AUTH) | ||
|
||
|
||
# Create Embeddings object | ||
class CustomEmbeddings(Embeddings): | ||
def embed_query(self, text: str) -> List[float]: | ||
return [random() for _ in range(DIMENSION)] | ||
|
||
|
||
embeddings = CustomEmbeddings() | ||
|
||
# Initialize the client | ||
client = GenAIClient(driver, embeddings) | ||
|
||
# Creating the index | ||
client.create_index( | ||
INDEX_NAME, | ||
label="Document", | ||
property="propertyKey", | ||
dimensions=DIMENSION, | ||
similarity_fn="euclidean", | ||
) | ||
|
||
# Upsert the query | ||
vector = [random() for _ in range(DIMENSION)] | ||
insert_query = ( | ||
"MERGE (n:Document)" | ||
"WITH n " | ||
"CALL db.create.setNodeVectorProperty(n, 'propertyKey', $vector)" | ||
"RETURN n" | ||
) | ||
parameters = { | ||
"vector": vector, | ||
} | ||
client.database_query(insert_query, params=parameters) | ||
|
||
# Perform the similarity search for a text query | ||
query_text = "hello world" | ||
print(client.similarity_search(INDEX_NAME, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from neo4j import GraphDatabase | ||
from neo4j_genai.client import GenAIClient | ||
|
||
from random import random | ||
|
||
URI = "neo4j://localhost:7687" | ||
AUTH = ("neo4j", "password") | ||
|
||
INDEX_NAME = "embedding-name" | ||
DIMENSION = 1536 | ||
|
||
# Connect to Neo4j database | ||
driver = GraphDatabase.driver(URI, auth=AUTH) | ||
|
||
# Initialize the client | ||
client = GenAIClient(driver) | ||
|
||
# Creating the index | ||
client.create_index( | ||
INDEX_NAME, | ||
label="Document", | ||
property="propertyKey", | ||
dimensions=DIMENSION, | ||
similarity_fn="euclidean", | ||
) | ||
|
||
# Upsert the vector | ||
vector = [random() for _ in range(DIMENSION)] | ||
insert_query = ( | ||
"MERGE (n:Document)" | ||
"WITH n " | ||
"CALL db.create.setNodeVectorProperty(n, 'propertyKey', $vector)" | ||
"RETURN n" | ||
) | ||
parameters = { | ||
"vector": vector, | ||
} | ||
client.database_query(insert_query, params=parameters) | ||
|
||
# Perform the similarity search for a vector query | ||
query_vector = [random() for _ in range(DIMENSION)] | ||
print(client.similarity_search(INDEX_NAME, query_vector=query_vector, top_k=5)) |
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.