Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cypher query extraction for node names with spaces #24

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion libs/neo4j/langchain_neo4j/chains/graph_qa/cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,20 @@ def extract_cypher(text: str) -> str:

# Find all matches in the input text
matches = re.findall(pattern, text, re.DOTALL)
if matches:
cypher_query = matches[0]
else:
return text

return matches[0] if matches else text
# Remove backticks
cypher_query = cypher_query.replace("`", "")

# Quote node labels if they contain spaces
cypher_query = re.sub(
r":\s*(\s*)([a-zA-Z0-9_]+(?:\s+[a-zA-Z0-9_]+)+)(\s*)", r":'\2'", cypher_query
)

return cypher_query


def construct_schema(
Expand Down
31 changes: 28 additions & 3 deletions libs/neo4j/tests/unit_tests/chains/test_graph_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,43 @@ def test_cypher_generation_failure() -> None:
assert response == []


def test_no_backticks() -> None:
def test_extract_cypher_on_no_backticks() -> None:
"""Test if there are no backticks, so the original text should be returned."""
query = "MATCH (n) RETURN n"
output = extract_cypher(query)
assert output == query


def test_backticks() -> None:
def test_extract_cypher_on_backticks() -> None:
"""Test if there are backticks. Query from within backticks should be returned."""
query = "You can use the following query: ```MATCH (n) RETURN n```"
expected_output = "MATCH (n) RETURN n"
output = extract_cypher(query)
assert output == "MATCH (n) RETURN n"
assert output == expected_output


def test_extract_cypher_on_label_with_spaces() -> None:
"""Test if node labels with spaces are quoted."""
query = "```MATCH (n:Label With Space) RETURN n```"
expected_output = "MATCH (n:'Label With Space') RETURN n"
output = extract_cypher(query)
assert output == expected_output


def test_extract_cypher_on_label_with_multi_spaces() -> None:
"""Test if node labels with multiple spaces are quoted."""
query = "```MATCH (n:Label With Space) RETURN n```"
expected_output = "MATCH (n:'Label With Space') RETURN n"
output = extract_cypher(query)
assert output == expected_output


def test_extract_cypher_on_label_without_spaces() -> None:
"""Test if node labels without spaces are not quoted."""
query = "```MATCH (n:LabelWithoutSpace) RETURN n```"
expected_output = "MATCH (n:LabelWithoutSpace) RETURN n"
output = extract_cypher(query)
assert output == expected_output


def test_exclude_types() -> None:
Expand Down
Loading