mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-20 13:54:48 +00:00
community[patch]: Update Memgraph support (#16360)
- **Description:** I removed two queries to the database and left just one whose results were formatted afterward into other type of schema (avoided two calls to DB) - **Issue:** / - **Dependencies:** / - **Twitter handle:** @supe_katarina
This commit is contained in:
parent
369e90d427
commit
01c2f27ffa
@ -1,12 +1,6 @@
|
||||
from langchain_community.graphs.neo4j_graph import Neo4jGraph
|
||||
|
||||
SCHEMA_QUERY = """
|
||||
CALL llm_util.schema("prompt_ready")
|
||||
YIELD *
|
||||
RETURN *
|
||||
"""
|
||||
|
||||
RAW_SCHEMA_QUERY = """
|
||||
CALL llm_util.schema("raw")
|
||||
YIELD *
|
||||
RETURN *
|
||||
@ -39,10 +33,39 @@ class MemgraphGraph(Neo4jGraph):
|
||||
Refreshes the Memgraph graph schema information.
|
||||
"""
|
||||
|
||||
db_schema = self.query(SCHEMA_QUERY)[0].get("schema")
|
||||
assert db_schema is not None
|
||||
self.schema = db_schema
|
||||
|
||||
db_structured_schema = self.query(RAW_SCHEMA_QUERY)[0].get("schema")
|
||||
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,
|
||||
]
|
||||
)
|
||||
|
@ -0,0 +1,62 @@
|
||||
import os
|
||||
|
||||
from langchain_community.graphs import MemgraphGraph
|
||||
|
||||
|
||||
def test_cypher_return_correct_schema() -> None:
|
||||
"""Test that chain returns direct results."""
|
||||
url = os.environ.get("MEMGRAPH_URI", "bolt://localhost:7687")
|
||||
username = os.environ.get("MEMGRAPH_USERNAME", "")
|
||||
password = os.environ.get("MEMGRAPH_PASSWORD", "")
|
||||
assert url is not None
|
||||
assert username is not None
|
||||
assert password is not None
|
||||
|
||||
graph = MemgraphGraph(
|
||||
url=url,
|
||||
username=username,
|
||||
password=password,
|
||||
)
|
||||
# Delete all nodes in the graph
|
||||
graph.query("MATCH (n) DETACH DELETE n")
|
||||
# Create two nodes and a relationship
|
||||
graph.query(
|
||||
"""
|
||||
CREATE (la:LabelA {property_a: 'a'})
|
||||
CREATE (lb:LabelB)
|
||||
CREATE (lc:LabelC)
|
||||
MERGE (la)-[:REL_TYPE]-> (lb)
|
||||
MERGE (la)-[:REL_TYPE {rel_prop: 'abc'}]-> (lc)
|
||||
"""
|
||||
)
|
||||
# Refresh schema information
|
||||
graph.refresh_schema()
|
||||
relationships = graph.query(
|
||||
"CALL llm_util.schema('raw') YIELD schema "
|
||||
"WITH schema.relationships AS relationships "
|
||||
"UNWIND relationships AS relationship "
|
||||
"RETURN relationship['start'] AS start, "
|
||||
"relationship['type'] AS type, "
|
||||
"relationship['end'] AS end "
|
||||
"ORDER BY start, type, end;"
|
||||
)
|
||||
|
||||
node_props = graph.query(
|
||||
"CALL llm_util.schema('raw') YIELD schema "
|
||||
"WITH schema.node_props AS nodes "
|
||||
"WITH nodes['LabelA'] AS properties "
|
||||
"UNWIND properties AS property "
|
||||
"RETURN property['property'] AS prop, "
|
||||
"property['type'] AS type "
|
||||
"ORDER BY prop ASC;"
|
||||
)
|
||||
|
||||
expected_relationships = [
|
||||
{"start": "LabelA", "type": "REL_TYPE", "end": "LabelB"},
|
||||
{"start": "LabelA", "type": "REL_TYPE", "end": "LabelC"},
|
||||
]
|
||||
|
||||
expected_node_props = [{"prop": "property_a", "type": "str"}]
|
||||
|
||||
assert relationships == expected_relationships
|
||||
assert node_props == expected_node_props
|
Loading…
Reference in New Issue
Block a user