-
Notifications
You must be signed in to change notification settings - Fork 86
feat: add bounded graph extraction concurrency #370
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
3719fb3
fd572e3
182e1a3
5e3ac82
fea7c35
e80f434
43d9bd6
8f43b44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| import json | ||
| import re | ||
| from concurrent.futures import ThreadPoolExecutor, as_completed | ||
| from typing import Any, Dict, List | ||
|
|
||
| from hugegraph_llm.config import prompt | ||
|
|
@@ -78,9 +79,15 @@ def filter_item(schema, items) -> List[Dict[str, Any]]: | |
|
|
||
|
|
||
| class PropertyGraphExtract: | ||
| def __init__(self, llm: BaseLLM, example_prompt: str = prompt.extract_graph_prompt) -> None: | ||
| def __init__( | ||
| self, | ||
| llm: BaseLLM, | ||
| example_prompt: str = prompt.extract_graph_prompt, | ||
| max_workers: int = 1, | ||
| ) -> None: | ||
| self.llm = llm | ||
| self.example_prompt = example_prompt | ||
| self.max_workers = max(1, int(max_workers or 1)) | ||
| self.NECESSARY_ITEM_KEYS = {"label", "type", "properties"} # pylint: disable=invalid-name | ||
|
|
||
| def run(self, context: Dict[str, Any]) -> Dict[str, List[Any]]: | ||
|
|
@@ -90,26 +97,60 @@ def run(self, context: Dict[str, Any]) -> Dict[str, List[Any]]: | |
| context["vertices"] = [] | ||
| if "edges" not in context: | ||
| context["edges"] = [] | ||
|
|
||
| items = [] | ||
| for chunk in chunks: | ||
| proceeded_chunk = self.extract_property_graph_by_llm(schema, chunk) | ||
| log.debug( | ||
| "[LLM] %s input: %s \n output:%s", | ||
| self.__class__.__name__, | ||
| chunk, | ||
| proceeded_chunk, | ||
| ) | ||
| items.extend(self._extract_and_filter_label(schema, proceeded_chunk)) | ||
| if self.max_workers == 1 or len(chunks) <= 1: | ||
| chunk_results = [ | ||
| self._extract_chunk_items(schema, chunk, index, len(chunks)) for index, chunk in enumerate(chunks) | ||
| ] | ||
| else: | ||
| chunk_results = self._extract_chunks_concurrently(schema, chunks) | ||
|
|
||
| for chunk_items in chunk_results: | ||
| items.extend(chunk_items) | ||
|
|
||
| items = filter_item(schema, items) | ||
| for item in items: | ||
| if item["type"] == "vertex": | ||
| context["vertices"].append(item) | ||
| elif item["type"] == "edge": | ||
| context["edges"].append(item) | ||
|
|
||
| context["call_count"] = context.get("call_count", 0) + len(chunks) | ||
| return context | ||
|
|
||
| def _extract_chunks_concurrently(self, schema, chunks): | ||
| worker_count = min(self.max_workers, len(chunks)) | ||
| chunk_results = [None] * len(chunks) | ||
| with ThreadPoolExecutor(max_workers=worker_count) as executor: | ||
| future_to_index = { | ||
| executor.submit( | ||
| self._extract_chunk_items, | ||
| schema, | ||
| chunk, | ||
| index, | ||
| len(chunks), | ||
| ): index | ||
| for index, chunk in enumerate(chunks) | ||
| } | ||
| for future in as_completed(future_to_index): | ||
| index = future_to_index[future] | ||
| chunk_results[index] = future.result() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return chunk_results | ||
|
|
||
| def _extract_chunk_items(self, schema, chunk, chunk_index, chunk_count): | ||
| try: | ||
| proceeded_chunk = self.extract_property_graph_by_llm(schema, chunk) | ||
| except Exception as exc: | ||
| raise RuntimeError(f"Graph extraction failed for chunk {chunk_index + 1}/{chunk_count}: {exc}") from exc | ||
|
|
||
| log.debug( | ||
| "[LLM] %s input: %s \noutput:%s", | ||
| self.__class__.__name__, | ||
| chunk, | ||
| proceeded_chunk, | ||
| ) | ||
| return self._extract_and_filter_label(schema, proceeded_chunk) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still treats malformed model output as a successful empty chunk in several cases. _extract_chunk_items only wraps exceptions raised by extract_property_graph_by_llm, but _extract_and_filter_label returns [] when no JSON is found, when the graph shape is invalid, or when JSON decoding fails. With multiple chunks, one bad chunk can therefore be silently dropped and the overall extraction still returns success, which loses the chunk-level failure visibility described in the PR. Please make parse/format failures distinguishable from a valid empty extraction and raise or report them with the chunk index.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. I updated the chunk extraction path so malformed LLM responses are no longer silently treated as empty results. Each chunk response is now validated for JSON extraction and expected property-graph shape before filtering. If a chunk returns malformed JSON or an invalid graph format, extraction fails with chunk context such as I also added regression tests for malformed chunk output, backend concurrency cap validation, and REST request validation. |
||
|
|
||
| def extract_property_graph_by_llm(self, schema, chunk): | ||
| prompt = generate_extract_property_graph_prompt(chunk, schema) | ||
| if self.example_prompt is not None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only rejects values below 1, while the UI advertises a maximum of 8. Programmatic callers can pass a much larger graph_extract_max_workers value, and PropertyGraphExtract will create up to min(max_workers, len(chunks)) threads. Please enforce the same backend cap here, or in a shared validator used by both this flow and graph_index_utils, so the concurrency limit stays bounded outside the Gradio slider path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. I addressed this by adding
graph_extract_max_workersto the REST request model and forwarding it through the REST graph extraction path as well.The same bounded concurrency setting is now available from both the demo UI path and the REST API path, and the backend validates it with a shared cap of 1–8 workers instead of relying only on the UI slider.