|
| 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 | +} |
0 commit comments