Skip to content

Commit 20f8250

Browse files
antejavorccurme
andauthored
Community: Add Memgraph integration docs (#30457)
Thank you for contributing to LangChain! **Description:** Since we just implemented [langchain-memgraph](https://pypi.org/project/langchain-memgraph/) integration, we are adding basic docs to [your site based on this comment](#30197 (review)) from @ccurme . **Twitter handle:** [@memgraphdb](https://x.com/memgraphdb) - [x] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, eyurtsev, ccurme, vbarda, hwchase17. --------- Co-authored-by: Chester Curme <[email protected]>
1 parent 7e62e3a commit 20f8250

File tree

4 files changed

+263
-7
lines changed

4 files changed

+263
-7
lines changed

docs/docs/integrations/graphs/memgraph.ipynb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"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",
3939
"\n",
4040
"```\n",
41-
"pip install langchain langchain-openai neo4j --user\n",
41+
"pip install langchain langchain-openai langchain-memgraph --user\n",
4242
"```\n",
4343
"\n",
4444
"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 @@
5757
},
5858
{
5959
"cell_type": "code",
60-
"execution_count": 1,
60+
"execution_count": null,
6161
"metadata": {},
6262
"outputs": [],
6363
"source": [
6464
"import os\n",
6565
"\n",
66-
"from langchain_community.chains.graph_qa.memgraph import MemgraphQAChain\n",
67-
"from langchain_community.graphs import MemgraphGraph\n",
6866
"from langchain_core.prompts import PromptTemplate\n",
67+
"from langchain_memgraph.chains.graph_qa import MemgraphQAChain\n",
68+
"from langchain_memgraph.graphs.memgraph import Memgraph\n",
6969
"from langchain_openai import ChatOpenAI\n",
7070
"\n",
7171
"url = os.environ.get(\"MEMGRAPH_URI\", \"bolt://localhost:7687\")\n",
7272
"username = os.environ.get(\"MEMGRAPH_USERNAME\", \"\")\n",
7373
"password = os.environ.get(\"MEMGRAPH_PASSWORD\", \"\")\n",
7474
"\n",
75-
"graph = MemgraphGraph(\n",
76-
" url=url, username=username, password=password, refresh_schema=False\n",
77-
")"
75+
"graph = Memgraph(url=url, username=username, password=password, refresh_schema=False)"
7876
]
7977
},
8078
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Memgraph
2+
3+
>Memgraph is a high-performance, in-memory graph database that is optimized for real-time queries and analytics.
4+
>Get started with Memgraph by visiting [their website](https://memgraph.com/).
5+
6+
## Installation and Setup
7+
8+
- Install the Python SDK with `pip install langchain-memgraph`
9+
10+
## MemgraphQAChain
11+
12+
There exists a wrapper around Memgraph graph database that allows you to generate Cypher statements based on the user input
13+
and use them to retrieve relevant information from the database.
14+
15+
```python
16+
from langchain_memgraph.chains.graph_qa import MemgraphQAChain
17+
from langchain_memgraph.graphs.memgraph import Memgraph
18+
```
19+
20+
See a [usage example](/docs/integrations/graphs/memgraph)
21+
22+
## Constructing a Knowledge Graph from unstructured data
23+
24+
You can use the integration to construct a knowledge graph from unstructured data.
25+
26+
```python
27+
from langchain_memgraph.graphs.memgraph import Memgraph
28+
from langchain_experimental.graph_transformers import LLMGraphTransformer
29+
```
30+
31+
See a [usage example](/docs/integrations/graphs/memgraph)
32+
33+
## Memgraph Tools and Toolkit
34+
35+
Memgraph also provides a toolkit that allows you to interact with the Memgraph database.
36+
See a [usage example](/docs/integrations/tools/memgraph).
37+
38+
```python
39+
from langchain_memgraph import MemgraphToolkit
40+
```
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "raw",
5+
"id": "afaf8039",
6+
"metadata": {},
7+
"source": [
8+
"---\n",
9+
"sidebar_label: Memgraph\n",
10+
"---"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "e49f1e0d",
16+
"metadata": {},
17+
"source": [
18+
"# MemgraphToolkit\n",
19+
"\n",
20+
"## Overview\n",
21+
"\n",
22+
"This will help you getting started with the Memgraph [toolkit](/docs/concepts/tools/#toolkits). \n",
23+
"\n",
24+
"Tools within `MemgraphToolkit` are designed for the interaction with the `Memgraph` database.\n",
25+
"\n",
26+
"## Setup\n",
27+
"\n",
28+
"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",
29+
" "
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"id": "72ee0c4b-9764-423a-9dbf-95129e185210",
35+
"metadata": {},
36+
"source": [
37+
"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:"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"id": "a15d341e-3e26-4ca3-830b-5aab30ed66de",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n",
48+
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\""
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"id": "0730d6a1-c893-4840-9817-5e5251676d5d",
54+
"metadata": {},
55+
"source": [
56+
"### Installation\n",
57+
"\n",
58+
"This toolkit lives in the `langchain-memgraph` package:"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"%pip install -qU langchain-memgraph "
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
74+
"metadata": {},
75+
"source": [
76+
"## Instantiation\n",
77+
"\n",
78+
"Now we can instantiate our toolkit:"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"from langchain.chat_models import init_chat_model\n",
89+
"from langchain_memgraph import MemgraphToolkit\n",
90+
"from langchain_memgraph.graphs.memgraph import Memgraph\n",
91+
"\n",
92+
"db = Memgraph(url=url, username=username, password=password)\n",
93+
"\n",
94+
"llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")\n",
95+
"\n",
96+
"toolkit = MemgraphToolkit(\n",
97+
" db=db, # Memgraph instance\n",
98+
" llm=llm, # LLM chat model for LLM operations\n",
99+
")"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"id": "5c5f2839-4020-424e-9fc9-07777eede442",
105+
"metadata": {},
106+
"source": [
107+
"## Tools\n",
108+
"\n",
109+
"View available tools:"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": null,
115+
"id": "51a60dbe-9f2e-4e04-bb62-23968f17164a",
116+
"metadata": {},
117+
"outputs": [],
118+
"source": [
119+
"toolkit.get_tools()"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"id": "608af19d",
125+
"metadata": {},
126+
"source": [
127+
"## Invocation\n",
128+
"\n",
129+
"Tools can be individually called by passing an arguments, for QueryMemgraphTool it would be: \n"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"id": "ffa944db",
136+
"metadata": {},
137+
"outputs": [],
138+
"source": [
139+
"from langchain_memgraph.tools import QueryMemgraphTool\n",
140+
"\n",
141+
"# Rest of the code omitted for brevity\n",
142+
"\n",
143+
"tool.invoke({QueryMemgraphTool({\"query\": \"MATCH (n) RETURN n LIMIT 5\"})})"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"id": "dfe8aad4-8626-4330-98a9-7ea1ca5d2e0e",
149+
"metadata": {},
150+
"source": [
151+
"## Use within an agent"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"id": "310bf18e-6c9a-4072-b86e-47bc1fcca29d",
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"from langgraph.prebuilt import create_react_agent\n",
162+
"\n",
163+
"agent_executor = create_react_agent(llm, tools)"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": null,
169+
"id": "23e11cc9-abd6-4855-a7eb-799f45ca01ae",
170+
"metadata": {},
171+
"outputs": [],
172+
"source": [
173+
"example_query = \"MATCH (n) RETURN n LIMIT 1\"\n",
174+
"\n",
175+
"events = agent_executor.stream(\n",
176+
" {\"messages\": [(\"user\", example_query)]},\n",
177+
" stream_mode=\"values\",\n",
178+
")\n",
179+
"for event in events:\n",
180+
" event[\"messages\"][-1].pretty_print()"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"id": "29ca615b",
186+
"metadata": {},
187+
"source": [
188+
"## API reference\n",
189+
"\n",
190+
"For more details on API visit [Memgraph integration docs](https://memgraph.com/docs/ai-ecosystem/integrations#langchain)"
191+
]
192+
}
193+
],
194+
"metadata": {
195+
"kernelspec": {
196+
"display_name": "Python 3 (ipykernel)",
197+
"language": "python",
198+
"name": "python3"
199+
},
200+
"language_info": {
201+
"codemirror_mode": {
202+
"name": "ipython",
203+
"version": 3
204+
},
205+
"file_extension": ".py",
206+
"mimetype": "text/x-python",
207+
"name": "python",
208+
"nbconvert_exporter": "python",
209+
"pygments_lexer": "ipython3",
210+
"version": "3.10.4"
211+
}
212+
},
213+
"nbformat": 4,
214+
"nbformat_minor": 5
215+
}

libs/packages.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,6 @@ packages:
551551
provider_page: naver
552552
path: .
553553
repo: e7217/langchain-naver-community
554+
- name: langchain-memgraph
555+
path: .
556+
repo: memgraph/langchain-memgraph

0 commit comments

Comments
 (0)