# ๐ฆ๏ธ๐ LangChain Neo4j
This package contains the LangChain integration with Neo4j.
## ๐ฆ Installation
```bash
pip install -U langchain-neo4j
```
## ๐ป Examples
### Neo4jGraph
The `Neo4jGraph` class is a wrapper around Neo4j's Python driver.
It provides a simple interface for interacting with a Neo4j database.
```python
from langchain_neo4j import Neo4jGraph
graph = Neo4jGraph(url="bolt://localhost:7687", username="neo4j", password="password")
graph.query("MATCH (n) RETURN n LIMIT 1;")
```
### Neo4jChatMessageHistory
The `Neo4jChatMessageHistory` class is used to store chat message history in a Neo4j database.
It stores messages as nodes and creates relationships between them, allowing for easy querying of the conversation history.
```python
from langchain_neo4j import Neo4jChatMessageHistory
history = Neo4jChatMessageHistory(
url="bolt://localhost:7687",
username="neo4j",
password="password",
session_id="session_id_1",
)
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages
```
### Neo4jVector
The `Neo4jVector` class provides functionality for managing a Neo4j vector store.
It enables you to create new vector indexes, add vectors to existing indexes, and perform queries on indexes.
```python
from langchain.docstore.document import Document
from langchain_openai import OpenAIEmbeddings
from langchain_neo4j import Neo4jVector
# Create a vector store from some documents and embeddings
docs = [
Document(
page_content=(
"LangChain is a framework to build "
"with LLMs by chaining interoperable components."
),
)
]
embeddings = OpenAIEmbeddings(
model="text-embedding-3-large",
api_key="sk-...", # Replace with your OpenAI API key
)
db = Neo4jVector.from_documents(
docs,
embeddings,
url="bolt://localhost:7687",
username="neo4j",
password="password",
)
# Query the vector store for similar documents
docs_with_score = db.similarity_search_with_score("What is LangChain?", k=1)
```
### GraphCypherQAChain
The `CypherQAChain` class enables natural language interactions with a Neo4j database.
It uses an LLM and the database's schema to translate a user's question into a Cypher query, which is executed against the database.
The resulting data is then sent along with the user's question to the LLM to generate a natural language response.
```python
from langchain_openai import ChatOpenAI
from langchain_neo4j import GraphCypherQAChain, Neo4jGraph
llm = ChatOpenAI(
temperature=0,
api_key="sk-...", # Replace with your OpenAI API key
)
graph = Neo4jGraph(url="bolt://localhost:7687", username="neo4j", password="password")
chain = GraphCypherQAChain.from_llm(llm=llm, graph=graph, allow_dangerous_requests=True)
chain.run("Who starred in Top Gun?")
```
## ๐งช Tests
Install the test dependencies to run the tests:
```bash
poetry install --with test,test_integration,mmr
```
### Unit Tests
Run the unit tests using:
```bash
make tests
```
### Integration Tests
1. Start the Neo4j instance using Docker:
```bash
cd tests/integration_tests/docker-compose
docker-compose -f neo4j.yml up
```
2. Run the tests:
```bash
make integration_tests
```
## ๐งน Code Formatting and Linting
Install the codespell, lint, and typing dependencies to lint and format your code:
```bash
poetry install --with codespell,lint,typing,mmr
```
To format your code, run:
```bash
make format
```
To lint it, run:
```bash
make lint
```
Raw data
{
"_id": null,
"home_page": "https://github.com/langchain-ai/langchain-neo4j",
"name": "langchain-neo4j",
"maintainer": null,
"docs_url": null,
"requires_python": "<3.14,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/3a/d3/541635bab2cc1d9bcab59e61bdd68c7c43898ba4c6dce78dccede7094a77/langchain_neo4j-0.5.0.tar.gz",
"platform": null,
"description": "# \ud83e\udd9c\ufe0f\ud83d\udd17 LangChain Neo4j\n\nThis package contains the LangChain integration with Neo4j.\n\n## \ud83d\udce6 Installation\n\n```bash\npip install -U langchain-neo4j\n```\n\n## \ud83d\udcbb Examples\n\n### Neo4jGraph\n\nThe `Neo4jGraph` class is a wrapper around Neo4j's Python driver.\nIt provides a simple interface for interacting with a Neo4j database.\n\n```python\nfrom langchain_neo4j import Neo4jGraph\n\ngraph = Neo4jGraph(url=\"bolt://localhost:7687\", username=\"neo4j\", password=\"password\")\ngraph.query(\"MATCH (n) RETURN n LIMIT 1;\")\n```\n\n### Neo4jChatMessageHistory\n\nThe `Neo4jChatMessageHistory` class is used to store chat message history in a Neo4j database.\nIt stores messages as nodes and creates relationships between them, allowing for easy querying of the conversation history.\n\n```python\nfrom langchain_neo4j import Neo4jChatMessageHistory\n\nhistory = Neo4jChatMessageHistory(\n url=\"bolt://localhost:7687\",\n username=\"neo4j\",\n password=\"password\",\n session_id=\"session_id_1\",\n)\nhistory.add_user_message(\"hi!\")\nhistory.add_ai_message(\"whats up?\")\nhistory.messages\n```\n\n### Neo4jVector\n\nThe `Neo4jVector` class provides functionality for managing a Neo4j vector store.\nIt enables you to create new vector indexes, add vectors to existing indexes, and perform queries on indexes.\n\n```python\nfrom langchain.docstore.document import Document\nfrom langchain_openai import OpenAIEmbeddings\n\nfrom langchain_neo4j import Neo4jVector\n\n# Create a vector store from some documents and embeddings\ndocs = [\n Document(\n page_content=(\n \"LangChain is a framework to build \"\n \"with LLMs by chaining interoperable components.\"\n ),\n )\n]\nembeddings = OpenAIEmbeddings(\n model=\"text-embedding-3-large\",\n api_key=\"sk-...\", # Replace with your OpenAI API key\n)\ndb = Neo4jVector.from_documents(\n docs,\n embeddings,\n url=\"bolt://localhost:7687\",\n username=\"neo4j\",\n password=\"password\",\n)\n# Query the vector store for similar documents\ndocs_with_score = db.similarity_search_with_score(\"What is LangChain?\", k=1)\n```\n\n### GraphCypherQAChain\n\nThe `CypherQAChain` class enables natural language interactions with a Neo4j database.\nIt uses an LLM and the database's schema to translate a user's question into a Cypher query, which is executed against the database.\nThe resulting data is then sent along with the user's question to the LLM to generate a natural language response.\n\n```python\nfrom langchain_openai import ChatOpenAI\n\nfrom langchain_neo4j import GraphCypherQAChain, Neo4jGraph\n\nllm = ChatOpenAI(\n temperature=0,\n api_key=\"sk-...\", # Replace with your OpenAI API key\n)\ngraph = Neo4jGraph(url=\"bolt://localhost:7687\", username=\"neo4j\", password=\"password\")\nchain = GraphCypherQAChain.from_llm(llm=llm, graph=graph, allow_dangerous_requests=True)\nchain.run(\"Who starred in Top Gun?\")\n```\n\n## \ud83e\uddea Tests\n\nInstall the test dependencies to run the tests:\n\n```bash\npoetry install --with test,test_integration,mmr\n```\n\n### Unit Tests\n\nRun the unit tests using:\n\n```bash\nmake tests\n```\n\n### Integration Tests\n\n1. Start the Neo4j instance using Docker:\n\n ```bash\n cd tests/integration_tests/docker-compose\n docker-compose -f neo4j.yml up\n ```\n\n2. Run the tests:\n\n ```bash\n make integration_tests\n ```\n\n## \ud83e\uddf9 Code Formatting and Linting\n\nInstall the codespell, lint, and typing dependencies to lint and format your code:\n\n```bash\npoetry install --with codespell,lint,typing,mmr\n```\n\nTo format your code, run:\n\n```bash\nmake format\n```\n\nTo lint it, run:\n\n```bash\nmake lint\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An integration package connecting Neo4j and LangChain",
"version": "0.5.0",
"project_urls": {
"Homepage": "https://github.com/langchain-ai/langchain-neo4j",
"Release Notes": "https://github.com/langchain-ai/langchain-neo4j/releases",
"Repository": "https://github.com/langchain-ai/langchain-neo4j",
"Source Code": "https://github.com/langchain-ai/langchain-neo4j/tree/main/libs/neo4j"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "c6c8a3fb1ceadb4c26527a03258353ad5e51d77a50b805cf42378bbfbae1af05",
"md5": "88846b34443954578f6b826ada236104",
"sha256": "b202623222904ef38f3309fd62e51546290efcc34fc49ff5a5b34ace65979330"
},
"downloads": -1,
"filename": "langchain_neo4j-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "88846b34443954578f6b826ada236104",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.14,>=3.9",
"size": 31477,
"upload_time": "2025-07-25T12:23:10",
"upload_time_iso_8601": "2025-07-25T12:23:10.581255Z",
"url": "https://files.pythonhosted.org/packages/c6/c8/a3fb1ceadb4c26527a03258353ad5e51d77a50b805cf42378bbfbae1af05/langchain_neo4j-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "3ad3541635bab2cc1d9bcab59e61bdd68c7c43898ba4c6dce78dccede7094a77",
"md5": "44d504193f4adcf5ba6c9e2f50e5ccaf",
"sha256": "9a2e153cdb79dfccf6208d520cfe00b59dbe21bff21fbabae0108a6b6fad948c"
},
"downloads": -1,
"filename": "langchain_neo4j-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "44d504193f4adcf5ba6c9e2f50e5ccaf",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.14,>=3.9",
"size": 27228,
"upload_time": "2025-07-25T12:23:12",
"upload_time_iso_8601": "2025-07-25T12:23:12.972899Z",
"url": "https://files.pythonhosted.org/packages/3a/d3/541635bab2cc1d9bcab59e61bdd68c7c43898ba4c6dce78dccede7094a77/langchain_neo4j-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-25 12:23:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "langchain-ai",
"github_project": "langchain-neo4j",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "langchain-neo4j"
}