{"id":29016497,"url":"https://github.com/memgraph/langchain-memgraph","last_synced_at":"2025-07-19T19:08:07.851Z","repository":{"id":281862303,"uuid":"946659644","full_name":"memgraph/langchain-memgraph","owner":"memgraph","description":null,"archived":true,"fork":false,"pushed_at":"2025-05-16T09:20:46.000Z","size":158,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-06-25T22:38:06.243Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/memgraph.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-11T13:35:50.000Z","updated_at":"2025-05-16T11:32:10.000Z","dependencies_parsed_at":"2025-03-11T15:24:21.304Z","dependency_job_id":"5906e7e5-d28a-4a99-b8e0-3cacdeeaa0ef","html_url":"https://github.com/memgraph/langchain-memgraph","commit_stats":null,"previous_names":["memgraph/langchain-memgraph"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/memgraph/langchain-memgraph","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memgraph%2Flangchain-memgraph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memgraph%2Flangchain-memgraph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memgraph%2Flangchain-memgraph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memgraph%2Flangchain-memgraph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/memgraph","download_url":"https://codeload.github.com/memgraph/langchain-memgraph/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memgraph%2Flangchain-memgraph/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265992788,"owners_count":23860977,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-25T22:30:27.848Z","updated_at":"2025-07-19T19:08:07.817Z","avatar_url":"https://github.com/memgraph.png","language":"Python","readme":"\u003e [!IMPORTANT]  \n\u003e This repository has been merged into the [Memgraph AI Toolkit](https://github.com/memgraph/ai-toolkit) monorepo to avoid duplicating tools.  \n\u003e It will be deleted in one month—please follow the [Langchain integration](https://github.com/memgraph/ai-toolkit/tree/main/integrations/langchain-memgraph) there for all future development, and feel free to open issues or PRs in that repo.\n\n# 🦜️🔗 LangChain Memgraph\n\nThis package contains the LangChain integration with [Memgraph](https://memgraph.com/) graph database.\n\n## 📦 Installation\n\n```bash\npip install -U langchain-memgraph\n```\n\n## 💻 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 Memgraph\n\nurl = os.getenv(\"MEMGRAPH_URI\", \"bolt://localhost:7687\")\nusername = os.getenv(\"MEMGRAPH_USERNAME\", \"\")\npassword = os.getenv(\"MEMGRAPH_PASSWORD\", \"\")\n\ngraph = Memgraph(url=url, username=username, password=password, refresh_schema=False)\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\n```python\nimport os\nfrom langchain_memgraph.graphs.memgraph import Memgraph\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_URI\", \"bolt://localhost:7687\")\nusername = os.getenv(\"MEMGRAPH_USERNAME\", \"\")\npassword = os.getenv(\"MEMGRAPH_PASSWORD\", \"\")\n\ngraph = Memgraph(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\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 Memgraph\nfrom langgraph.prebuilt import create_react_agent\n\nos.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"\")\nurl = os.getenv(\"MEMGRAPH_URI\", \"bolt://localhost:7687\")\nusername = os.getenv(\"MEMGRAPH_USERNAME\", \"\")\npassword = os.getenv(\"MEMGRAPH_PASSWORD\", \"\")\n\nllm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")\n\ndb = Memgraph(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## 🧪 Test\n\nInstall the test dependencies to run the tests:\n1. Install dependencies \n\n```bash\npoetry install --with test,test_integration\n```\n\n2. Start Memgraph in the background. \n   \n3. Create an `.env` file that points to Memgraph and OpenAI API\n```\nMEMGRAPH_URI=bolt://localhost:7687\nMEMGRAPH_USERNAME=\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\n make integration_tests\n ```\n\n## 🧹 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```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmemgraph%2Flangchain-memgraph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmemgraph%2Flangchain-memgraph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmemgraph%2Flangchain-memgraph/lists"}