How to query llm-graph-builder.neo4jlabs.com from python?

I used llm-graph-builder.neo4jlabs.com to generate a knowledge graph on an Aura instance. I'm able to chat with it from within the chatbot tab.

I would now like to integrate with that instance using python neo4j-graphrag. Following the example from User Guide: RAG — neo4j-graphrag-python documentation
and using the index named vector that I found in my instance:

%%capture
%pip install neo4j-graphrag 

from neo4j import GraphDatabase
from neo4j_graphrag.retrievers import VectorRetriever
from neo4j_graphrag.llm import OpenAILLM
from neo4j_graphrag.generation import GraphRAG
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from google.colab import userdata
import os

os.environ['OPENAI_API_KEY'] = userdata.get('openai')

# 1. Neo4j driver
URI = userdata.get('NEO4J_URI')
AUTH = (userdata.get('NEO4J_USERNAME'), userdata.get('NEO4J_PASSWORD'))

INDEX_NAME = "vector"

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

# 2. Retriever
# Create Embedder object, needed to convert the user question (text) to a vector
embedder = OpenAIEmbeddings(model="text-embedding-3-large")

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

# 3. LLM
# Note: the OPENAI_API_KEY must be in the env vars
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})

# Initialize the RAG pipeline
rag = GraphRAG(retriever=retriever, llm=llm)

# Query the graph
query_text = "How do I do similarity search in Neo4j?"
response = rag.search(query_text=query_text, retriever_config={"top_k": 5})
print(response.answer)

I get the following error

ClientError: {code: Neo.ClientError.Procedure.ProcedureCallFailed} 
{message: Failed to invoke procedure `db.index.vector.queryNodes`: 
Caused by: java.lang.IllegalArgumentException: 
Index query vector has 1536 dimensions, but indexed vectors have 384.}

I suspect the problem is that I'm using the wrong embedding model, but I have not yet found the documentation that specifies which model is used by llm-graph-builder. It's also possible I'm using the wrong vector index.

How do I update my code to be able to query the knowledge graph generated by llm-graph-builder?

Reading the code, I was able to discover it uses HuggingFace. If I make the following changes to my code, I'm able to query.

%pip installlangchain-huggingface
from langchain_huggingface import HuggingFaceEmbeddings
embedder = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

I wasn't able to find this documented anywhere, is it somewhere?

1 Like