Skip to content

Commit

Permalink
OpenAI embedding used
Browse files Browse the repository at this point in the history
  • Loading branch information
willtai committed Apr 4, 2024
1 parent 32b967d commit 865b6cd
Show file tree
Hide file tree
Showing 4 changed files with 654 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist/
.coverage
htmlcov/
.idea/
.env
54 changes: 54 additions & 0 deletions examples/openai_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import List
from neo4j import GraphDatabase
from neo4j_genai import VectorRetriever

from random import random
from neo4j_genai.indexes import create_vector_index, drop_vector_index

import os
from langchain_openai import OpenAIEmbeddings

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")

INDEX_NAME = "embedding-name-large"
DIMENSION = 3072

# Connect to Neo4j database
driver = GraphDatabase.driver(URI, auth=AUTH)


# Create Embedder object
embedder = OpenAIEmbeddings(model="text-embedding-3-large")

# Initialize the retriever
retriever = VectorRetriever(driver, embedder)

drop_vector_index(driver, INDEX_NAME)

# Creating the index
create_vector_index(
driver,
INDEX_NAME,
label="Document",
property="propertyKey",
dimensions=DIMENSION,
similarity_fn="cosine",
)

# 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,
}
driver.execute_query(insert_query, parameters)

# Perform the similarity search for a text query
query_text = "hello world"
print(retriever.search(INDEX_NAME, query_text=query_text, top_k=5))
Loading

0 comments on commit 865b6cd

Please sign in to comment.