Source code for langchain_community.graphs.memgraph_graph
from langchain_community.graphs.neo4j_graph import Neo4jGraph
SCHEMA_QUERY = """
CALL llm_util.schema("raw")
YIELD *
RETURN *
"""
[docs]class MemgraphGraph(Neo4jGraph):
"""Memgraph图操作的包装器。
*安全提示*: 确保数据库连接使用的凭据范围狭窄,仅包括必要的权限。
如果未这样做,可能会导致数据损坏或丢失,因为调用代码可能尝试执行会导致删除、变异数据(如果适当提示)或读取敏感数据(如果数据库中存在此类数据)的命令。
防范这些负面结果的最佳方法是(根据需要)限制授予此工具使用的凭据的权限。
有关更多信息,请参见 https://python.langchain.com/docs/security。"""
[docs] def __init__(
self, url: str, username: str, password: str, *, database: str = "memgraph"
) -> None:
"""创建一个新的Memgraph图包装实例。"""
super().__init__(url, username, password, database=database)
[docs] def refresh_schema(self) -> None:
"""
刷新Memgraph图模式信息。
"""
db_structured_schema = self.query(SCHEMA_QUERY)[0].get("schema")
assert db_structured_schema is not None
self.structured_schema = db_structured_schema
# Format node properties
formatted_node_props = []
for node_name, properties in db_structured_schema["node_props"].items():
formatted_node_props.append(
f"Node name: '{node_name}', Node properties: {properties}"
)
# Format relationship properties
formatted_rel_props = []
for rel_name, properties in db_structured_schema["rel_props"].items():
formatted_rel_props.append(
f"Relationship name: '{rel_name}', "
f"Relationship properties: {properties}"
)
# Format relationships
formatted_rels = [
f"(:{rel['start']})-[:{rel['type']}]->(:{rel['end']})"
for rel in db_structured_schema["relationships"]
]
self.schema = "\n".join(
[
"Node properties are the following:",
*formatted_node_props,
"Relationship properties are the following:",
*formatted_rel_props,
"The relationships are the following:",
*formatted_rels,
]
)