Repository navigation
fix(llm): escape vertex IDs in Gremlin queries #383
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
Open
atharrva01
wants to merge
5
commits into
apache:main
Choose a base branch
from
atharrva01:fix/llm-escape-gremlin-vertex-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d49b43
fix(llm): escape vertex ids in Gremlin queries
atharrva01 9733d51
test(llm): accept list form in fake gremlin endpoint
atharrva01 9a8d3dd
fix(llm): keep numeric ids and escape control chars in Gremlin literals
atharrva01 e554885
fix(llm): handle numeric ids when formatting graph paths
atharrva01 9c94f31
test(llm): parse g.V() args in fake endpoint without cutting quoted ids
atharrva01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| from hugegraph_llm.nodes.hugegraph_node.graph_query_node import GraphQueryNode | ||
| from tests.utils.mock import FakeGremlinEndpoint | ||
|
|
||
|
|
||
| def _graph_query_node(endpoint): | ||
| node = GraphQueryNode() | ||
| node._client = SimpleNamespace(gremlin=lambda: endpoint) | ||
| node._schema = "Vertex properties: [name: person, properties: []]\nEdge properties: [name: knows, properties: []]\n" | ||
| node._prop_to_match = None | ||
| node._max_deep = 2 | ||
| node._max_items = 30 | ||
| return node | ||
|
|
||
|
|
||
| def test_subgraph_query_escapes_vertex_ids_with_apostrophes(): | ||
| # Fuzzy matching returns raw vertex ids from the vid index, so ids like 1:O'Brien reach the node unchanged. | ||
| endpoint = FakeGremlinEndpoint({"1:O'Brien": {"name": "O'Brien"}}) | ||
| node = _graph_query_node(endpoint) | ||
|
|
||
| result = node._subgraph_query({"match_vids": ["1:O'Brien"]}) | ||
|
|
||
| assert result["graph_result"] == ["1:O'Brien{name: O'Brien}"] | ||
| assert len(endpoint.scripts) == 2 | ||
| assert all("'1:O\\'Brien'" in script for script in endpoint.scripts) | ||
|
|
||
|
|
||
| def _knows_path(start_id, start_name, end_id, end_name): | ||
| return { | ||
| "objects": [ | ||
| {"label": "person", "id": start_id, "props": {"name": start_name}}, | ||
| {"label": "knows", "inV": end_id, "outV": start_id, "props": {}}, | ||
| {"label": "person", "id": end_id, "props": {"name": end_name}}, | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| def test_subgraph_query_keeps_numeric_vertex_ids_numeric(): | ||
| # Graphs with numeric ids store ints in the vid index; HugeGraph treats 123 and '123' as different ids. | ||
| # Two paths share the start vertex, so the second one hits the v_cache branch with an int id. | ||
| endpoint = FakeGremlinEndpoint( | ||
| {123: {"name": "n123"}}, | ||
| paths=[_knows_path(123, "n123", 1, "Alice"), _knows_path(123, "n123", 2, "Bob")], | ||
| ) | ||
| node = _graph_query_node(endpoint) | ||
|
|
||
| result = node._subgraph_query({"match_vids": [123]}) | ||
|
|
||
| assert [script.split(")", 1)[0] for script in endpoint.scripts] == ["g.V(123", "g.V(123"] | ||
| assert sorted(result["graph_result"]) == [ | ||
| "123--[knows]-->2{name: Bob}", | ||
| "123{name: n123}--[knows]-->1{name: Alice}", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
| from pyhugegraph.client import PyHugeClient | ||
|
|
||
| from hugegraph_llm.utils.graph_index_utils import get_vertex_details | ||
| from tests.utils.mock import FakeGremlinEndpoint | ||
|
|
||
|
|
||
| def test_get_vertex_details_escapes_apostrophes_in_vertex_ids(): | ||
| endpoint = FakeGremlinEndpoint({"1:O'Brien": {"name": "O'Brien"}}) | ||
| client = Mock(spec=PyHugeClient) | ||
| client.gremlin.return_value = endpoint | ||
|
|
||
| result = get_vertex_details(["1:O'Brien"], {"graph_client": client}) | ||
|
|
||
| assert [v["id"] for v in result] == ["1:O'Brien"] | ||
| assert endpoint.scripts == ["g.V('1:O\\'Brien').limit(20)"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("vertex_id", ["1:x).y", "1:a', 'b').limit(1", "1:[x](y)", '1:say "hi").z']) | ||
| def test_get_vertex_details_handles_ids_with_gremlin_syntax(vertex_id): | ||
| endpoint = FakeGremlinEndpoint({vertex_id: {"name": "n"}}) | ||
| client = Mock(spec=PyHugeClient) | ||
| client.gremlin.return_value = endpoint | ||
|
|
||
| result = get_vertex_details([vertex_id], {"graph_client": client}) | ||
|
|
||
| assert [v["id"] for v in result] == [vertex_id] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.