You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I added a very descriptive title to this question.
I searched the LangChain documentation with the integrated search.
I used the GitHub search to find a similar question and didn't find it.
Commit to Help
I commit to help with one of those options 👆
Example Code
fromlangchain_ollamaimportOllamaEmbeddingsfromlangchain_community.vectorstores.neo4j_vectorimportNeo4jVectorimportosfromdotenvimportload_dotenvfromlangchain.schemaimportDocumentload_dotenv()
URI=os.getenv("NEO4J_URL")
username=os.getenv("NEO4J_USERNAME")
password=os.getenv("NEO4J_PASSWORD")
chunked_folder_path=os.getenv("CHUNK_FOLDER_PATH")
index="vector"keyword_index_name="keyword"print("neo4j url:" ,URI)
print("neo4j username:" ,username)
print("neo4j password:" ,password)
defread_documents(chunked_folder_path):
docs= []
try:
forfilenameinos.listdir(chunked_folder_path):
iffilename.endswith(".txt"):
file_path=os.path.join(chunked_folder_path, filename)
withopen(file_path, 'r', encoding='utf-8') asfile:
content=file.read()
docs.append(Document(page_content=content, metadata={"filename": filename}))
else:
print(f"Skipped non-txt file: {filename}")
exceptExceptionase:
print(f"Error reading documents from folder: {e}")
returndocsdefretrieval_from_graph(documents):
try:
embedding_model=OllamaEmbeddings(model="mxbai-embed-large")
print("Embedding model initialized successfully.")
exceptExceptionase:
print(f"Error initializing the embedding model: {e}")
returnNonetry:
vectorstore=Neo4jVector.from_existing_index(
embedding=embedding_model,
url=URI,
username=username,
password=password,
search_type="hybrid",
index_name=index,
keyword_index_name=keyword_index_name,
node_label=["Events", "Person"],
embedding_node_property="embedding",
)
print("Successfully connected to the existing Neo4j vector index.")
returnvectorstoreexceptExceptionase:
print(f"Existing index not found, Creating a new one ......: {e}")
documents=read_documents(chunked_folder_path)
ifnotdocuments:
print("No documents were loaded. Cannot create index")
returnNonetry:
vectorstore=Neo4jVector.from_documents(
embedding=embedding_model,
documents=documents,
url=URI,
username=username,
password=password,
search_type="hybrid",
index_name=index,
keyword_index_name=keyword_index_name,
node_label=["Events", "Person"],
embedding_node_property="embedding",
)
print("New vector index created successfully")
returnvectorstoreexceptExceptionascreation_error:
print(f"Error creating vector index: {creation_error}")
defsimilarity_search(vectorstore, query):
try:
docs_with_score=vectorstore.similarity_search_with_score(query, k=2)
fordoc, scoreindocs_with_score:
print(f"Document: {doc.page_content}\nScore: {score}")
exceptExceptionase:
print(f"Error during similarity search: {e}")
defquery_similarity_search(query ):
documents=read_documents(chunked_folder_path)
ifnotdocuments:
print("No document found in the folder")
returnvectorstore=retrieval_from_graph(documents)
ifnotvectorstore:
print("Failed to create vector store")
returnresult=similarity_search(vectorstore, query)
returnresultif__name__=="__main__":
query="Who is King Birendra"query_similarity_search(query)
Description
Hello,
I am an enthusiast of knowledge# Problem Description:
I’m working on a project using LangChain to connect to a Neo4j database hosted in a Docker container. I’ve set up the environment using docker-compose, but I’m encountering a Connection Refused error when attempting to interact with Neo4j through the Neo4jVector integration from LangChain.
Here’s what I’ve done so far:
My python worked properly when I used neo4j API and use cloud instance. But when I shifted to docker for neo4j. After than I am facing this error.
This works
[I tested direct connectivity using GraphDatabase.driver and confirmed Neo4j is reachable and it works properly.]
from neo4j import GraphDatabase
uri = "bolt://neo4j_database:7687"
username = "neo4j"
password = "neo4j_admin"
driver = GraphDatabase.driver(uri, auth=(username, password))
def push_data(tx):
# Create historical figures as nodes
tx.run("""
CREATE (mahendra:Person {name: 'King Mahendra', role: 'King of Nepal', reign: '1955-1972'})
CREATE (rana_regime:Event {name: 'Rana Regime', description: 'Autocratic rule by the Rana family in Nepal', period: '1846-1951'})
CREATE (tribhuvan:Person {name: 'King Tribhuvan', role: 'King of Nepal', reign: '1911-1955'})
CREATE (prithvi:Person {name: 'King Prithvi Narayan Shah', role: 'Founder of Unified Nepal', reign: '1743-1775'})
CREATE (bp_koirala:Person {name: 'BP Koirala', role: 'First Democratically Elected Prime Minister of Nepal', term: '1959-1960'})
CREATE (military_coup:Event {name: '1960 Military Coup', description: 'King Mahendra’s coup that dissolved the democratic government', year: 1960})
""")
# Create relationships
tx.run("""
MATCH (mahendra:Person {name: 'King Mahendra'}),
(tribhuvan:Person {name: 'King Tribhuvan'}),
(rana_regime:Event {name: 'Rana Regime'}),
(prithvi:Person {name: 'King Prithvi Narayan Shah'}),
(bp_koirala:Person {name: 'BP Koirala'}),
(military_coup:Event {name: '1960 Military Coup'})
CREATE (tribhuvan)-[:ENDED]->(rana_regime)
CREATE (mahendra)-[:CONDUCTED]->(military_coup)
CREATE (bp_koirala)-[:OVERTHROWN_BY]->(military_coup)
CREATE (prithvi)-[:FOUNDED]->(:Entity {name: 'Unified Nepal'})
CREATE (mahendra)-[:SUCCEEDED]->(tribhuvan)
""")
try:
with driver.session() as session:
session.execute_write(push_data)
print("Data pushed successfully!")
print(uri)
except Exception as e:
print(f"An error occurred: {e}")
finally:
driver.close()
Why am I getting a Connection Refused error when using LangChain's Neo4jVector? How can I resolve this issue while maintaining the current Docker setup?
graphs, and I am currently working on a project about the history of Nepal using Neo4j, Llama 3.2, and LangChain. I would love to connect with you.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Checked other resources
Commit to Help
Example Code
Description
Hello,
I am an enthusiast of knowledge# Problem Description:
I’m working on a project using LangChain to connect to a Neo4j database hosted in a Docker container. I’ve set up the environment using docker-compose, but I’m encountering a Connection Refused error when attempting to interact with Neo4j through the Neo4jVector integration from LangChain.
Here’s what I’ve done so far:
My python worked properly when I used neo4j API and use cloud instance. But when I shifted to docker for neo4j. After than I am facing this error.
This works
[I tested direct connectivity using GraphDatabase.driver and confirmed Neo4j is reachable and it works properly.]
Error:
Embedding model initialized successfully.
Error creating vector index: [Errno 111] Connection refused
The Neo4j browser is accessible at http://localhost:7474 and bolt://172.20.0.3:7687..
[This is my docker-compose.yaml .]
Why am I getting a Connection Refused error when using LangChain's Neo4jVector? How can I resolve this issue while maintaining the current Docker setup?
graphs, and I am currently working on a project about the history of Nepal using Neo4j, Llama 3.2, and LangChain. I would love to connect with you.
System Info
Environment Variables:
NEO4J_URI=bolt://172.20.0.3:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=neo4j_admin
Beta Was this translation helpful? Give feedback.
All reactions