You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hugegraph.apache.org by "Thespica (via GitHub)" <gi...@apache.org> on 2025/04/17 14:30:26 UTC

[PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Thespica opened a new pull request, #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211

   See #210 
   
   Main change of regex: matching `(\[.*])` -> matching `({.*})`.
   
   tested models:
   - qwen-max
   - qwen-plus
   - deepseek-v3


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "imbajin (via GitHub)" <gi...@apache.org>.
imbajin commented on code in PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#discussion_r2050352681


##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,40 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)
+        if not json_match:
+            log.critical("Invalid property graph! No JSON object found.")

Review Comment:
   Also remind users to check the **output format example** in prompt



##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,40 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)
+        if not json_match:
+            log.critical("Invalid property graph! No JSON object found.")
+            return []
+        json_str = json_match.group(1).strip()
 
         items = []
         try:
-            property_graph = json.loads(longest_json_str)
+            property_graph = json.loads(json_str)
+            # Expect property_graph to be a dict with keys "vertices" and "edges"
+            if not (isinstance(property_graph, dict) and "vertices" in property_graph and "edges" in property_graph):

Review Comment:
   consider only check `vertices` here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "imbajin (via GitHub)" <gi...@apache.org>.
imbajin commented on code in PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#discussion_r2053264430


##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,41 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)
+        if not json_match:
+            log.critical("Invalid property graph! No JSON object found, " +
+                         "please check the output format example in prompt.")

Review Comment:
   no need to use `+` in py



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "imbajin (via GitHub)" <gi...@apache.org>.
imbajin merged PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#discussion_r2050331845


##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,40 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)

Review Comment:
   The regex pattern is greedy and might capture more than the intended JSON object when multiple curly-brace segments exist. Consider using a non-greedy quantifier (e.g., r'({.*?})') to ensure only the first JSON object is matched.
   ```suggestion
           json_match = re.search(r'({.*?})', text, re.DOTALL)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "Thespica (via GitHub)" <gi...@apache.org>.
Thespica commented on PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#issuecomment-2816945434

   @imbajin Please take a look again : )


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "Thespica (via GitHub)" <gi...@apache.org>.
Thespica commented on code in PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#discussion_r2051615935


##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,40 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)
+        if not json_match:
+            log.critical("Invalid property graph! No JSON object found.")

Review Comment:
   refined



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "imbajin (via GitHub)" <gi...@apache.org>.
imbajin commented on code in PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#discussion_r2053264430


##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,41 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)
+        if not json_match:
+            log.critical("Invalid property graph! No JSON object found, " +
+                         "please check the output format example in prompt.")

Review Comment:
   no need to use `+` in py (for better perf)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "Thespica (via GitHub)" <gi...@apache.org>.
Thespica commented on code in PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#discussion_r2051615976


##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,40 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)
+        if not json_match:
+            log.critical("Invalid property graph! No JSON object found.")
+            return []
+        json_str = json_match.group(1).strip()
 
         items = []
         try:
-            property_graph = json.loads(longest_json_str)
+            property_graph = json.loads(json_str)
+            # Expect property_graph to be a dict with keys "vertices" and "edges"
+            if not (isinstance(property_graph, dict) and "vertices" in property_graph and "edges" in property_graph):

Review Comment:
   "vertices" and "edges" are required by format



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org


Re: [PR] fix(llm): Align regex extraction of json to json format of prompt [incubator-hugegraph-ai]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #211:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/211#discussion_r2053261474


##########
hugegraph-llm/src/hugegraph_llm/operators/llm_op/property_graph_extract.py:
##########
@@ -125,33 +125,41 @@ def extract_property_graph_by_llm(self, schema, chunk):
         return self.llm.generate(prompt=prompt)
 
     def _extract_and_filter_label(self, schema, text) -> List[Dict[str, Any]]:
-        # analyze llm generated text to JSON
-        json_strings = re.findall(r'(\[.*?])', text, re.DOTALL)
-        longest_json = max(json_strings, key=lambda x: len(''.join(x)), default=('', ''))
-
-        longest_json_str = ''.join(longest_json).strip()
+        # Use regex to extract JSON object with curly braces
+        json_match = re.search(r'({.*})', text, re.DOTALL)

Review Comment:
   The regex pattern uses greedy matching which may capture more than expected when multiple JSON objects are present in the text. Consider using a non-greedy qualifier, e.g., '({.*?})', to ensure only the intended object is captured.
   ```suggestion
           json_match = re.search(r'({.*?})', text, re.DOTALL)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org