# ๐ฆ๏ธ๐ LangChain Memgraph
This package contains the LangChain integration with [Memgraph](https://memgraph.com/) graph database.
## ๐ฆ Installation
In order to start running the examples or tests you need to install the LangChain integration.
You can do it via pip:
```bash
pip install -U langchain-memgraph
```
Before running the examples below, make sure to start Memgraph, you can do it via following command:
```bash
docker run -p 7687:7687 \
--name memgraph \
memgraph/memgraph-mage:latest \
--schema-info-enabled=true
```
## ๐ป Integration features
### Memgraph
The `Memgraph` class is a wrapper around the database client that supports the
query operation.
```python
import os
from langchain_memgraph.graphs.memgraph import MemgraphLangChain
url = os.getenv("MEMGRAPH_URL", "bolt://localhost:7687")
username = os.getenv("MEMGRAPH_USER", "")
password = os.getenv("MEMGRAPH_PASSWORD", "")
graph = MemgraphLangChain(url=url, username=username, password=password)
results = graph.query("MATCH (n) RETURN n LIMIT 1")
print(results)
```
### MemgraphQAChain
The `MemgraphQAChain` class enables natural language interactions with a Memgraph 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.
For the example below you need to install an extra dependency the `lanchain_openai`, you can do it by running:
```bash
pip install lanchain_openai
```
```python
import os
from langchain_memgraph.graphs.memgraph import MemgraphLangChain
from langchain_memgraph.chains.graph_qa import MemgraphQAChain
from langchain_openai import ChatOpenAI
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
url = os.getenv("MEMGRAPH_URL", "bolt://localhost:7687")
username = os.getenv("MEMGRAPH_USER", "")
password = os.getenv("MEMGRAPH_PASSWORD", "")
graph = MemgraphLangChain(url=url, username=username, password=password, refresh_schema=False)
chain = MemgraphQAChain.from_llm(
ChatOpenAI(temperature=0),
graph=graph,
model_name="gpt-4-turbo",
allow_dangerous_requests=True,
)
response = chain.invoke("Is there a any Person node in the dataset?")
result = response["result"].lower()
print(result)
```
### Memgraph toolkit
The `MemgraphToolkit` contains different tools agents can leverage to perform specific tasks the user has given them. Toolkit
needs a database object and LLM access since different tools leverage different operations.
Currently supported tools:
1. **QueryMemgraphTool** - Basic Cypher query execution tool
```python
import os
import pytest
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from langchain_memgraph import MemgraphToolkit
from langchain_memgraph.graphs.memgraph import MemgraphLangChain
from langgraph.prebuilt import create_react_agent
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
url = os.getenv("MEMGRAPH_URL", "bolt://localhost:7687")
username = os.getenv("MEMGRAPH_USER", "")
password = os.getenv("MEMGRAPH_PASSWORD", "")
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
db = MemgraphLangChain(url=url, username=username, password=password)
toolkit = MemgraphToolkit(db=db, llm=llm)
agent_executor = create_react_agent(
llm, toolkit.get_tools(), prompt="You will get a cypher query, try to execute it on the Memgraph database."
)
example_query = "MATCH (n) WHERE n.name = 'Jon Snow' RETURN n"
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
last_event = None
for event in events:
last_event = event
event["messages"][-1].pretty_print()
print(last_event)
```
## ๐งช Test
Install the test dependencies to run the tests:
1. Install dependencies
```bash
poetry install --with test,test_integration
```
2. Start Memgraph in the background.
3. Create an `.env` file that points to Memgraph and OpenAI API
```
MEMGRAPH_URL=bolt://localhost:7687
MEMGRAPH_USER=
MEMGRAPH_PASSWORD=
OPENAI_API_KEY=your_openai_api_key
```
### Run tests
Run the unit tests using:
```bash
make tests
```
Run the integration test using:
```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
```
To format your code, run:
```bash
make format
```
To lint it, run:
```bash
make lint
```
Raw data
{
"_id": null,
"home_page": null,
"name": "langchain-memgraph",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "ai, integration, langchain, memgraph",
"author": null,
"author_email": "Ante Javor <ante.javor@memgraph.com>",
"download_url": "https://files.pythonhosted.org/packages/55/ee/37401885931d12120fedbc30a5d494d786d9e65a912cf74355fab67fe21a/langchain_memgraph-0.1.6.tar.gz",
"platform": null,
"description": "# \ud83e\udd9c\ufe0f\ud83d\udd17 LangChain Memgraph\n\nThis package contains the LangChain integration with [Memgraph](https://memgraph.com/) graph database.\n\n## \ud83d\udce6 Installation\n\nIn order to start running the examples or tests you need to install the LangChain integration.\n\nYou can do it via pip:\n\n```bash\npip install -U langchain-memgraph\n```\n\nBefore running the examples below, make sure to start Memgraph, you can do it via following command:\n\n```bash\ndocker run -p 7687:7687 \\\n --name memgraph \\\n memgraph/memgraph-mage:latest \\\n --schema-info-enabled=true\n```\n\n## \ud83d\udcbb Integration features\n\n### Memgraph\n\nThe `Memgraph` class is a wrapper around the database client that supports the\nquery operation.\n\n```python\nimport os\nfrom langchain_memgraph.graphs.memgraph import MemgraphLangChain\n\nurl = os.getenv(\"MEMGRAPH_URL\", \"bolt://localhost:7687\")\nusername = os.getenv(\"MEMGRAPH_USER\", \"\")\npassword = os.getenv(\"MEMGRAPH_PASSWORD\", \"\")\n\ngraph = MemgraphLangChain(url=url, username=username, password=password)\nresults = graph.query(\"MATCH (n) RETURN n LIMIT 1\")\nprint(results)\n```\n\n### MemgraphQAChain\n\nThe `MemgraphQAChain` class enables natural language interactions with a Memgraph 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\nFor the example below you need to install an extra dependency the `lanchain_openai`, you can do it by running:\n\n```bash\npip install lanchain_openai\n```\n\n```python\nimport os\nfrom langchain_memgraph.graphs.memgraph import MemgraphLangChain\nfrom langchain_memgraph.chains.graph_qa import MemgraphQAChain\nfrom langchain_openai import ChatOpenAI\n\nos.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"\")\nurl = os.getenv(\"MEMGRAPH_URL\", \"bolt://localhost:7687\")\nusername = os.getenv(\"MEMGRAPH_USER\", \"\")\npassword = os.getenv(\"MEMGRAPH_PASSWORD\", \"\")\n\ngraph = MemgraphLangChain(url=url, username=username, password=password, refresh_schema=False)\n\nchain = MemgraphQAChain.from_llm(\n ChatOpenAI(temperature=0),\n graph=graph,\n model_name=\"gpt-4-turbo\",\n allow_dangerous_requests=True,\n)\nresponse = chain.invoke(\"Is there a any Person node in the dataset?\")\nresult = response[\"result\"].lower()\nprint(result)\n```\n\n### Memgraph toolkit\n\nThe `MemgraphToolkit` contains different tools agents can leverage to perform specific tasks the user has given them. Toolkit\nneeds a database object and LLM access since different tools leverage different operations.\n\nCurrently supported tools:\n\n1. **QueryMemgraphTool** - Basic Cypher query execution tool\n\n```python\nimport os\nimport pytest\nfrom dotenv import load_dotenv\nfrom langchain.chat_models import init_chat_model\nfrom langchain_memgraph import MemgraphToolkit\nfrom langchain_memgraph.graphs.memgraph import MemgraphLangChain\nfrom langgraph.prebuilt import create_react_agent\n\nos.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"\")\nurl = os.getenv(\"MEMGRAPH_URL\", \"bolt://localhost:7687\")\nusername = os.getenv(\"MEMGRAPH_USER\", \"\")\npassword = os.getenv(\"MEMGRAPH_PASSWORD\", \"\")\n\nllm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")\n\ndb = MemgraphLangChain(url=url, username=username, password=password)\ntoolkit = MemgraphToolkit(db=db, llm=llm)\n\nagent_executor = create_react_agent(\n llm, toolkit.get_tools(), prompt=\"You will get a cypher query, try to execute it on the Memgraph database.\"\n)\n\nexample_query = \"MATCH (n) WHERE n.name = 'Jon Snow' RETURN n\"\nevents = agent_executor.stream(\n {\"messages\": [(\"user\", example_query)]},\n stream_mode=\"values\",\n)\n\nlast_event = None\nfor event in events:\n last_event = event\n event[\"messages\"][-1].pretty_print()\n\nprint(last_event)\n\n```\n\n## \ud83e\uddea Test\n\nInstall the test dependencies to run the tests:\n\n1. Install dependencies\n\n```bash\npoetry install --with test,test_integration\n```\n\n2. Start Memgraph in the background.\n3. Create an `.env` file that points to Memgraph and OpenAI API\n\n```\nMEMGRAPH_URL=bolt://localhost:7687\nMEMGRAPH_USER=\nMEMGRAPH_PASSWORD=\nOPENAI_API_KEY=your_openai_api_key\n```\n\n### Run tests\n\nRun the unit tests using:\n\n```bash\nmake tests\n```\n\nRun the integration test using:\n\n```bash\nmake 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\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",
"bugtrack_url": null,
"license": "MIT",
"summary": "An integration package connecting Memgraph and LangChain",
"version": "0.1.6",
"project_urls": null,
"split_keywords": [
"ai",
" integration",
" langchain",
" memgraph"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "6c833580cd837bd4a2bef86e8607759d64033f66a6b274eb12be84cfad1736bb",
"md5": "4e2a9ff498048c770d04a945b85be9a6",
"sha256": "2240ec9a536e83a2c2ad47a451cc7bb39f9a63732c7b117f188367d652478c1b"
},
"downloads": -1,
"filename": "langchain_memgraph-0.1.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4e2a9ff498048c770d04a945b85be9a6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 20057,
"upload_time": "2025-08-06T11:46:51",
"upload_time_iso_8601": "2025-08-06T11:46:51.138311Z",
"url": "https://files.pythonhosted.org/packages/6c/83/3580cd837bd4a2bef86e8607759d64033f66a6b274eb12be84cfad1736bb/langchain_memgraph-0.1.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "55ee37401885931d12120fedbc30a5d494d786d9e65a912cf74355fab67fe21a",
"md5": "39206619964e733b3f4c81e99fe10e81",
"sha256": "43f210fd9e47fb857f460e88ab4a26e4d75ffed4f2670b0d9882bb10d911c016"
},
"downloads": -1,
"filename": "langchain_memgraph-0.1.6.tar.gz",
"has_sig": false,
"md5_digest": "39206619964e733b3f4c81e99fe10e81",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 146330,
"upload_time": "2025-08-06T11:46:52",
"upload_time_iso_8601": "2025-08-06T11:46:52.607168Z",
"url": "https://files.pythonhosted.org/packages/55/ee/37401885931d12120fedbc30a5d494d786d9e65a912cf74355fab67fe21a/langchain_memgraph-0.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-06 11:46:52",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "langchain-memgraph"
}