MemgraphToolkit
This will help you get started with the Memgraphtoolkit.
Tools withinMemgraphToolkit
are designed for the interaction with theMemgraph
database.
Setup
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 atMemgraph docs
If you want to get automated tracing from runs of individual tools, you can also set yourLangSmith API key by uncommenting below:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
Installation
This toolkit lives in thelangchain-memgraph
package:
%pip install-qU langchain-memgraph
Instantiation
Now we can instantiate our toolkit:
from langchain.chat_modelsimport init_chat_model
from langchain_memgraphimport MemgraphToolkit
from langchain_memgraph.graphs.memgraphimport MemgraphLangChain
db= MemgraphLangChain(url=url, username=username, password=password)
llm= init_chat_model("gpt-4o-mini", model_provider="openai")
toolkit= MemgraphToolkit(
db=db,# Memgraph instance
llm=llm,# LLM chat model for LLM operations
)
API Reference:init_chat_model
Tools
View available tools:
toolkit.get_tools()
Invocation
Tools can be individually called by passing an arguments, for QueryMemgraphTool it would be:
from langchain_memgraph.toolsimport QueryMemgraphTool
# Rest of the code omitted for brevity
tool.invoke({QueryMemgraphTool({"query":"MATCH (n) RETURN n LIMIT 5"})})
Use within an agent
from langgraph.prebuiltimport create_react_agent
agent_executor= create_react_agent(llm, tools)
API Reference:create_react_agent
example_query="MATCH (n) RETURN n LIMIT 1"
events= agent_executor.stream(
{"messages":[("user", example_query)]},
stream_mode="values",
)
for eventin events:
event["messages"][-1].pretty_print()
API reference
For more details on API visitMemgraph integration docs
Related
- Toolconceptual guide
- Toolhow-to guides