Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class BasePromptConfig:
gremlin_generate_prompt: str = ""
doc_input_text: str = ""
graph_extract_split_type: str = "document"
graph_extract_max_workers: int = 1
schema_generator_query_examples: str = ""
schema_generator_few_shot_examples: str = ""
_language_generated: str = ""
Expand Down Expand Up @@ -140,6 +141,7 @@ def to_literal(val):
"gremlin_generate_prompt": to_literal(self.gremlin_generate_prompt),
"doc_input_text": to_literal(self.doc_input_text),
"graph_extract_split_type": to_literal(self.graph_extract_split_type),
"graph_extract_max_workers": self.graph_extract_max_workers,
"schema_generator_query_examples": to_literal(self.schema_generator_query_examples),
"schema_generator_few_shot_examples": to_literal(self.schema_generator_few_shot_examples),
"_language_generated": str(self.llm_settings.language).lower().strip(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,20 @@ def store_prompt(
schema,
example_prompt,
graph_extract_split_type="document",
graph_extract_max_workers=1,
):
if (
prompt.doc_input_text != doc
or prompt.graph_schema != schema
or prompt.extract_graph_prompt != example_prompt
or prompt.graph_extract_split_type != graph_extract_split_type
or prompt.graph_extract_max_workers != int(graph_extract_max_workers)
):
prompt.doc_input_text = doc
prompt.graph_schema = schema
prompt.extract_graph_prompt = example_prompt
prompt.graph_extract_split_type = graph_extract_split_type
prompt.graph_extract_max_workers = int(graph_extract_max_workers)
prompt.update_yaml_file()


Expand Down Expand Up @@ -405,6 +408,14 @@ def create_vector_graph_block():
label="Graph Extraction Split Type",
info=("document keeps the current behavior; paragraph/sentence split long docs before extraction."),
)
graph_extract_max_workers = gr.Slider(
minimum=1,
maximum=8,
value=prompt.graph_extract_max_workers,
step=1,
label="Graph Extraction Chunk Concurrency",
info="Maximum parallel chunk extraction calls. 1 keeps serial behavior.",
)
graph_extract_bt = gr.Button("Extract Graph Data (1)", variant="primary")
graph_loading_bt = gr.Button("Load into GraphDB (2)", interactive=True)
graph_index_rebuild_bt = gr.Button("Update Vid Embedding")
Expand Down Expand Up @@ -440,6 +451,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
vector_index_btn1.click(clean_vector_index).then(
Expand All @@ -449,6 +461,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
vector_import_bt.click(build_vector_index, inputs=[input_file, input_text], outputs=out).then(
Expand All @@ -458,6 +471,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_index_btn0.click(get_graph_index_info, outputs=out).then(
Expand All @@ -467,6 +481,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_index_btn1.click(clean_all_graph_index).then(
Expand All @@ -476,6 +491,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_data_btn0.click(clean_all_graph_data).then(
Expand All @@ -485,6 +501,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_index_rebuild_bt.click(update_vid_embedding, outputs=out).then(
Expand All @@ -494,6 +511,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)

Expand All @@ -506,6 +524,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
outputs=[out],
).then(
Expand All @@ -515,6 +534,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)

Expand All @@ -527,6 +547,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)

Expand All @@ -542,6 +563,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
], # Persist the updated schema-generator examples
)

Expand Down
10 changes: 10 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/flows/graph_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def prepare(
extract_type,
split_type=SPLIT_TYPE_DOCUMENT,
language="zh",
graph_extract_max_workers=1,
**kwargs,
):
# prepare input data
Expand All @@ -53,6 +54,13 @@ def prepare(
raise ValueError("split_type must be document, paragraph, or sentence")

prepared_input.split_type = split_type
try:
graph_extract_max_workers = int(graph_extract_max_workers)
except (TypeError, ValueError) as exc:
raise ValueError("graph_extract_max_workers must be a positive integer") from exc
if graph_extract_max_workers < 1:

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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_workers to 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.

raise ValueError("graph_extract_max_workers must be a positive integer")
prepared_input.graph_extract_max_workers = graph_extract_max_workers
prepared_input.example_prompt = example_prompt
prepared_input.schema = schema
prepared_input.extract_type = extract_type
Expand All @@ -76,6 +84,7 @@ def build_flow(
extract_type,
split_type=SPLIT_TYPE_DOCUMENT,
language="zh",
graph_extract_max_workers=1,
**kwargs,
):
pipeline = GPipeline()
Expand All @@ -89,6 +98,7 @@ def build_flow(
extract_type,
split_type,
language,
graph_extract_max_workers,
**kwargs,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ def node_init(self):
if extract_type == "triples":
self.info_extract = InfoExtract(llm, example_prompt)
elif extract_type == "property_graph":
self.property_graph_extract = PropertyGraphExtract(llm, example_prompt)
self.property_graph_extract = PropertyGraphExtract(
llm,
example_prompt,
max_workers=self.wk_input.graph_extract_max_workers,
)
else:
return CStatus(-1, f"Unsupported extract_type: {extract_type}")
return super().node_init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]]:
Expand All @@ -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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ A failed chunk does not stop queued LLM calls. Every chunk is submitted above, and when future.result() raises, leaving the ThreadPoolExecutor context waits for running work while queued calls may still start, delaying the error and consuming unnecessary rate limit or paid requests. Please cancel futures that have not started on the first failure, define the policy for already-running calls, and add a regression test proving later queued chunks are not invoked after an early failure.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

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 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 Graph extraction failed for chunk 2/5: ....

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:
Expand Down
2 changes: 2 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/state/ai_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class WkFlowInput(GParam):
texts: Optional[Union[str, List[str]]] = None # texts input used by ChunkSplit Node
language: Optional[str] = None # language configuration used by ChunkSplit Node
split_type: Optional[str] = None # split type used by ChunkSplit Node
graph_extract_max_workers: int = 1 # bounded chunk extraction concurrency
example_prompt: Optional[str] = None # need by graph information extract
schema: Optional[str] = None # Schema information requeired by SchemaNode
# Request-scoped HugeGraph connection; None falls back to global huge_settings.
Expand Down Expand Up @@ -86,6 +87,7 @@ def reset(self, _: CStatus) -> None:
self.texts = None
self.language = None
self.split_type = None
self.graph_extract_max_workers = 1
self.example_prompt = None
self.schema = None
self.graph_client_config = None
Expand Down
13 changes: 12 additions & 1 deletion hugegraph-llm/src/hugegraph_llm/utils/graph_index_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def extract_graph(
schema,
example_prompt,
split_type=SPLIT_TYPE_DOCUMENT,
graph_extract_max_workers=1,
) -> str:
texts = read_documents(input_file, input_text)
scheduler = SchedulerSingleton.get_instance()
Expand All @@ -95,13 +96,23 @@ def extract_graph(
if split_type not in VALID_SPLIT_TYPES:
raise gr.Error("split_type must be document, paragraph, or sentence")
try:
graph_extract_max_workers = int(graph_extract_max_workers)
except (TypeError, ValueError) as exc:
raise gr.Error("graph_extract_max_workers must be a positive integer") from exc
if graph_extract_max_workers < 1:
raise gr.Error("graph_extract_max_workers must be a positive integer")
try:
schedule_kwargs = {"split_type": split_type}
if graph_extract_max_workers != 1:
schedule_kwargs["graph_extract_max_workers"] = graph_extract_max_workers

return scheduler.schedule_flow(
FlowName.GRAPH_EXTRACT,
schema,
texts,
example_prompt,
"property_graph",
split_type=split_type,
**schedule_kwargs,
)
except Exception as e: # pylint: disable=broad-exception-caught
log.error(e)
Expand Down
Loading
Loading