Skip to main content

🦜️🔗 LangChain Memgraph

This package contains the LangChain integration with Memgraph 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:

pip install -U langchain-memgraph

Before running the examples below, make sure to start Memgraph, you can do it via following command:

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.

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:

pip install lanchain_openai
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. RunQueryTool - Cypher query execution
  2. RunSearchSchemaTool - Search the graph schema by regex pattern
  3. RunNodeSchemaTool - Get full schema definition of a node by its labels
  4. RunRelationshipSchemaTool - Get full schema definition of a relationship
  5. RunEnumSchemaTool - Get schema definition of an enum by its name
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
poetry install --with test,test_integration
  1. Start Memgraph in the background.
  2. 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:

make tests

Run the integration test using:

make integration_tests

🧹 Code Formatting and Linting

Install the codespell, lint, and typing dependencies to lint and format your code:

poetry install --with codespell,lint,typing

To format your code, run:

make format

To lint it, run:

make lint

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_memgraph-0.1.15.tar.gz (213.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_memgraph-0.1.15-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file langchain_memgraph-0.1.15.tar.gz.

File metadata

  • Download URL: langchain_memgraph-0.1.15.tar.gz
  • Upload date:
  • Size: 213.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for langchain_memgraph-0.1.15.tar.gz
Algorithm Hash digest
SHA256 d2414e0bd22af8ad52f0d256657aae98c09c7ed9f0efce4dc27e0612005def08
MD5 ba1bc0545e8eb5ead49ff0696181159a
BLAKE2b-256 7fefa881f2a845356fe177a4d6662b9ab94c17abc1372d9b7517e92371a2d340

See more details on using hashes here.

File details

Details for the file langchain_memgraph-0.1.15-py3-none-any.whl.

File metadata

  • Download URL: langchain_memgraph-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for langchain_memgraph-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 5fe10f841b7dc559ea1eacfaeb8235095e111cd9e8d0a4933cec79de7d34cc68
MD5 43e47da57314aa865ef2299b00fefdef
BLAKE2b-256 1ec8ec97651f82b5c157b605814fc5dbd514636b86f4ad15cdcfe2f324e71194

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.15 This release

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page