mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-12 06:13:36 +00:00
fix neo4j schema query (#6381)
Fix issue #6380 <!-- Remove if not applicable --> Fixes #6380 (issue) #### Before submitting <!-- If you're adding a new integration, please include: 1. a test for the integration - favor unit tests that does not rely on network access. 2. an example notebook showing its use See contribution guidelines for more information on how to write tests, lint etc: https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md --> #### Who can review? Tag maintainers/contributors who might be interested: @hwchase17 <!-- For a quicker response, figure out the right person to tag with @ @hwchase17 - project lead Tracing / Callbacks - @agola11 Async - @agola11 DataLoaders - @eyurtsev Models - @hwchase17 - @agola11 Agents / Tools / Toolkits - @hwchase17 VectorStores / Retrievers / Memory - @dev2049 --> --------- Co-authored-by: HubertKl <HubertKl>
This commit is contained in:
parent
b0d80c4b3e
commit
22601b0b63
@ -21,7 +21,8 @@ rel_query = """
|
|||||||
CALL apoc.meta.data()
|
CALL apoc.meta.data()
|
||||||
YIELD label, other, elementType, type, property
|
YIELD label, other, elementType, type, property
|
||||||
WHERE type = "RELATIONSHIP" AND elementType = "node"
|
WHERE type = "RELATIONSHIP" AND elementType = "node"
|
||||||
RETURN "(:" + label + ")-[:" + property + "]->(:" + toString(other[0]) + ")" AS output
|
UNWIND other AS other_node
|
||||||
|
RETURN "(:" + label + ")-[:" + property + "]->(:" + toString(other_node) + ")" AS output
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,6 +4,11 @@ import os
|
|||||||
from langchain.chains.graph_qa.cypher import GraphCypherQAChain
|
from langchain.chains.graph_qa.cypher import GraphCypherQAChain
|
||||||
from langchain.chains.loading import load_chain
|
from langchain.chains.loading import load_chain
|
||||||
from langchain.graphs import Neo4jGraph
|
from langchain.graphs import Neo4jGraph
|
||||||
|
from langchain.graphs.neo4j_graph import (
|
||||||
|
node_properties_query,
|
||||||
|
rel_properties_query,
|
||||||
|
rel_query,
|
||||||
|
)
|
||||||
from langchain.llms.openai import OpenAI
|
from langchain.llms.openai import OpenAI
|
||||||
|
|
||||||
|
|
||||||
@ -171,11 +176,8 @@ def test_cypher_return_direct() -> None:
|
|||||||
assert output == expected_output
|
assert output == expected_output
|
||||||
|
|
||||||
|
|
||||||
def test_cypher_save_load() -> None:
|
def test_cypher_return_correct_schema() -> None:
|
||||||
"""Test saving and loading."""
|
"""Test that chain returns direct results."""
|
||||||
|
|
||||||
FILE_PATH = "cypher.yaml"
|
|
||||||
|
|
||||||
url = os.environ.get("NEO4J_URL")
|
url = os.environ.get("NEO4J_URL")
|
||||||
username = os.environ.get("NEO4J_USERNAME")
|
username = os.environ.get("NEO4J_USERNAME")
|
||||||
password = os.environ.get("NEO4J_PASSWORD")
|
password = os.environ.get("NEO4J_PASSWORD")
|
||||||
@ -188,7 +190,60 @@ def test_cypher_save_load() -> None:
|
|||||||
username=username,
|
username=username,
|
||||||
password=password,
|
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()
|
||||||
|
|
||||||
|
node_properties = graph.query(node_properties_query)
|
||||||
|
relationships_properties = graph.query(rel_properties_query)
|
||||||
|
relationships = graph.query(rel_query)
|
||||||
|
|
||||||
|
expected_node_properties = [
|
||||||
|
{
|
||||||
|
"properties": [{"property": "property_a", "type": "STRING"}],
|
||||||
|
"labels": "LabelA",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
expected_relationships_properties = [
|
||||||
|
{"type": "REL_TYPE", "properties": [{"property": "rel_prop", "type": "STRING"}]}
|
||||||
|
]
|
||||||
|
expected_relationships = [
|
||||||
|
"(:LabelA)-[:REL_TYPE]->(:LabelB)",
|
||||||
|
"(:LabelA)-[:REL_TYPE]->(:LabelC)",
|
||||||
|
]
|
||||||
|
|
||||||
|
assert node_properties == expected_node_properties
|
||||||
|
assert relationships_properties == expected_relationships_properties
|
||||||
|
assert relationships == expected_relationships
|
||||||
|
|
||||||
|
|
||||||
|
def test_cypher_save_load() -> None:
|
||||||
|
"""Test saving and loading."""
|
||||||
|
|
||||||
|
FILE_PATH = "cypher.yaml"
|
||||||
|
url = os.environ.get("NEO4J_URL")
|
||||||
|
username = os.environ.get("NEO4J_USERNAME")
|
||||||
|
password = os.environ.get("NEO4J_PASSWORD")
|
||||||
|
assert url is not None
|
||||||
|
assert username is not None
|
||||||
|
assert password is not None
|
||||||
|
|
||||||
|
graph = Neo4jGraph(
|
||||||
|
url=url,
|
||||||
|
username=username,
|
||||||
|
password=password,
|
||||||
|
)
|
||||||
chain = GraphCypherQAChain.from_llm(
|
chain = GraphCypherQAChain.from_llm(
|
||||||
OpenAI(temperature=0), graph=graph, return_direct=True
|
OpenAI(temperature=0), graph=graph, return_direct=True
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user