이 문서는 Memgraph toolkit을 시작하는 데 도움이 됩니다. MemgraphToolkit 내의 tool들은 Memgraph 데이터베이스와의 상호작용을 위해 설계되었습니다.

Setup

아래 단계를 따르려면 로컬 호스트에서 실행 중인 Memgraph 인스턴스가 있는지 확인하세요. Memgraph 실행 방법에 대한 자세한 내용은 Memgraph docs를 참조하세요. 개별 tool 실행에서 자동화된 추적을 원하는 경우, 아래 주석을 해제하여 LangSmith API key를 설정할 수도 있습니다:
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"

Installation

이 toolkit은 langchain-memgraph 패키지에 있습니다:
pip install -qU langchain-memgraph

Instantiation

이제 toolkit을 인스턴스화할 수 있습니다:
from langchain.chat_models import init_chat_model
from langchain_memgraph import MemgraphToolkit
from langchain_memgraph.graphs.memgraph import MemgraphLangChain

db = MemgraphLangChain(url=url, username=username, password=password)

model = init_chat_model("gpt-4o-mini", model_provider="openai")

toolkit = MemgraphToolkit(
    db=db,  # Memgraph instance
    llm=model,  # LLM chat model for LLM operations
)

Tools

사용 가능한 tool들을 확인합니다:
toolkit.get_tools()

Invocation

Tool들은 인자를 전달하여 개별적으로 호출할 수 있습니다. QueryMemgraphTool의 경우 다음과 같습니다:
from langchain_memgraph.tools import QueryMemgraphTool

# Rest of the code omitted for brevity

tool.invoke({QueryMemgraphTool({"query": "MATCH (n) RETURN n LIMIT 5"})})

Use within an agent

from langchain.agents import create_agent


agent_executor = create_agent(model, tools)
example_query = "MATCH (n) RETURN n LIMIT 1"

events = agent_executor.stream(
    {"messages": [("user", example_query)]},
    stream_mode="values",
)
for event in events:
    event["messages"][-1].pretty_print()

API reference

API에 대한 자세한 내용은 Memgraph integration docs를 참조하세요.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I