Skip to content

The agent engineering platform.

Community: Add Memgraph integration docs (#30457)

Ante Javor20f82502e59b
diff --git a/docs/docs/integrations/graphs/memgraph.ipynb b/docs/docs/integrations/graphs/memgraph.ipynb
index 1040d43f9e..4ccfb6989b 100644
--- a/docs/docs/integrations/graphs/memgraph.ipynb
+++ b/docs/docs/integrations/graphs/memgraph.ipynb
@@ -38,7 +38,7 @@
"To use LangChain, install and import all the necessary packages. We'll use the package manager [pip](https://pip.pypa.io/en/stable/installation/), along with the `--user` flag, to ensure proper permissions. If you've installed Python 3.4 or a later version, `pip` is included by default. You can install all the required packages using the following command:\n",
"\n",
"```\n",
- "pip install langchain langchain-openai neo4j --user\n",
+ "pip install langchain langchain-openai langchain-memgraph --user\n",
"```\n",
"\n",
"You can either run the provided code blocks in this notebook or use a separate Python file to experiment with Memgraph and LangChain."
@@ -57,24 +57,22 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
- "from langchain_community.chains.graph_qa.memgraph import MemgraphQAChain\n",
- "from langchain_community.graphs import MemgraphGraph\n",
"from langchain_core.prompts import PromptTemplate\n",
+ "from langchain_memgraph.chains.graph_qa import MemgraphQAChain\n",
+ "from langchain_memgraph.graphs.memgraph import Memgraph\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"url = os.environ.get(\"MEMGRAPH_URI\", \"bolt://localhost:7687\")\n",
"username = os.environ.get(\"MEMGRAPH_USERNAME\", \"\")\n",
"password = os.environ.get(\"MEMGRAPH_PASSWORD\", \"\")\n",
"\n",
- "graph = MemgraphGraph(\n",
- " url=url, username=username, password=password, refresh_schema=False\n",
- ")"
+ "graph = Memgraph(url=url, username=username, password=password, refresh_schema=False)"
]
},
{
diff --git a/docs/docs/integrations/providers/memgraph.mdx b/docs/docs/integrations/providers/memgraph.mdx
new file mode 100644
index 0000000000..a64203a85b
--- /dev/null
+++ b/docs/docs/integrations/providers/memgraph.mdx
@@ -0,0 +1,40 @@
+# Memgraph
+
+>Memgraph is a high-performance, in-memory graph database that is optimized for real-time queries and analytics.
+>Get started with Memgraph by visiting [their website](https://memgraph.com/).
+
+## Installation and Setup
+
+- Install the Python SDK with `pip install langchain-memgraph`
+
+## MemgraphQAChain
+
+There exists a wrapper around Memgraph graph database that allows you to generate Cypher statements based on the user input
+and use them to retrieve relevant information from the database.
+
+```python
+from langchain_memgraph.chains.graph_qa import MemgraphQAChain
+from langchain_memgraph.graphs.memgraph import Memgraph
+```
+
+See a [usage example](/docs/integrations/graphs/memgraph)
+
+## Constructing a Knowledge Graph from unstructured data
+
+You can use the integration to construct a knowledge graph from unstructured data.
+
+```python
+from langchain_memgraph.graphs.memgraph import Memgraph
+from langchain_experimental.graph_transformers import LLMGraphTransformer
+```
+
+See a [usage example](/docs/integrations/graphs/memgraph)
+
+## Memgraph Tools and Toolkit
+
+Memgraph also provides a toolkit that allows you to interact with the Memgraph database.
+See a [usage example](/docs/integrations/tools/memgraph).
+
+```python
+from langchain_memgraph import MemgraphToolkit
+```
diff --git a/docs/docs/integrations/tools/memgraph.ipynb b/docs/docs/integrations/tools/memgraph.ipynb
new file mode 100644
index 0000000000..0797e30309
--- /dev/null
+++ b/docs/docs/integrations/tools/memgraph.ipynb
@@ -0,0 +1,215 @@
+{
+ "cells": [
+ {
+ "cell_type": "raw",
+ "id": "afaf8039",
+ "metadata": {},
+ "source": [
+ "---\n",
+ "sidebar_label: Memgraph\n",
+ "---"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e49f1e0d",
+ "metadata": {},
+ "source": [
+ "# MemgraphToolkit\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This will help you getting started with the Memgraph [toolkit](/docs/concepts/tools/#toolkits). \n",
+ "\n",
+ "Tools within `MemgraphToolkit` are designed for the interaction with the `Memgraph` database.\n",
+ "\n",
+ "## Setup\n",
+ "\n",
+ "To be able tot follow the steps below, make sure you have a running Memgraph instance on your local host. For more details on how to run Memgraph, take a look at [Memgraph docs](https://memgraph.com/docs/getting-started)\n",
+ " "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "72ee0c4b-9764-423a-9dbf-95129e185210",
+ "metadata": {},
+ "source": [
+ "If you want to get automated tracing from runs of individual tools, you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a15d341e-3e26-4ca3-830b-5aab30ed66de",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n",
+ "# os.environ[\"LANGSMITH_TRACING\"] = \"true\""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "0730d6a1-c893-4840-9817-5e5251676d5d",
+ "metadata": {},
+ "source": [
+ "### Installation\n",
+ "\n",
+ "This toolkit lives in the `langchain-memgraph` package:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "652d6238-1f87-422a-b135-f5abbb8652fc",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "%pip install -qU langchain-memgraph "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a38cde65-254d-4219-a441-068766c0d4b5",
+ "metadata": {},
+ "source": [
+ "## Instantiation\n",
+ "\n",
+ "Now we can instantiate our toolkit:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from langchain.chat_models import init_chat_model\n",
+ "from langchain_memgraph import MemgraphToolkit\n",
+ "from langchain_memgraph.graphs.memgraph import Memgraph\n",
+ "\n",
+ "db = Memgraph(url=url, username=username, password=password)\n",
+ "\n",
+ "llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")\n",
+ "\n",
+ "toolkit = MemgraphToolkit(\n",
+ " db=db, # Memgraph instance\n",
+ " llm=llm, # LLM chat model for LLM operations\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "5c5f2839-4020-424e-9fc9-07777eede442",
+ "metadata": {},
+ "source": [
+ "## Tools\n",
+ "\n",
+ "View available tools:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "51a60dbe-9f2e-4e04-bb62-23968f17164a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "toolkit.get_tools()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "608af19d",
+ "metadata": {},
+ "source": [
+ "## Invocation\n",
+ "\n",
+ "Tools can be individually called by passing an arguments, for QueryMemgraphTool it would be: \n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ffa944db",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from langchain_memgraph.tools import QueryMemgraphTool\n",
+ "\n",
+ "# Rest of the code omitted for brevity\n",
+ "\n",
+ "tool.invoke({QueryMemgraphTool({\"query\": \"MATCH (n) RETURN n LIMIT 5\"})})"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "dfe8aad4-8626-4330-98a9-7ea1ca5d2e0e",
+ "metadata": {},
+ "source": [
+ "## Use within an agent"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "310bf18e-6c9a-4072-b86e-47bc1fcca29d",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from langgraph.prebuilt import create_react_agent\n",
+ "\n",
+ "agent_executor = create_react_agent(llm, tools)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "23e11cc9-abd6-4855-a7eb-799f45ca01ae",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "example_query = \"MATCH (n) RETURN n LIMIT 1\"\n",
+ "\n",
+ "events = agent_executor.stream(\n",
+ " {\"messages\": [(\"user\", example_query)]},\n",
+ " stream_mode=\"values\",\n",
+ ")\n",
+ "for event in events:\n",
+ " event[\"messages\"][-1].pretty_print()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "29ca615b",
+ "metadata": {},
+ "source": [
+ "## API reference\n",
+ "\n",
+ "For more details on API visit [Memgraph integration docs](https://memgraph.com/docs/ai-ecosystem/integrations#langchain)"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.10.4"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/libs/packages.yml b/libs/packages.yml
index b3d79811c2..af7aee1899 100644
--- a/libs/packages.yml
+++ b/libs/packages.yml
@@ -551,3 +551,6 @@ packages:
provider_page: naver
path: .
repo: e7217/langchain-naver-community
+- name: langchain-memgraph
+ path: .
+ repo: memgraph/langchain-memgraph