This is an automated email from the ASF dual-hosted git repository.
jin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-ai.git
The following commit(s) were added to refs/heads/main by this push:
new 4f2f123 fix(llm): enable tasks concurrency configs in Gradio (#188)
4f2f123 is described below
commit 4f2f123014b7793f346f396e006e76d5204e2395
Author: Kryst4lDem0ni4s <[email protected]>
AuthorDate: Wed Apr 2 17:59:56 2025 +0530
fix(llm): enable tasks concurrency configs in Gradio (#188)
fix #176
Blocking and Queuing Behavior: Gradio, by default, has a concurrency
limit of 1. This causes blocking when multiple users or windows are
accessing the application simultaneously.
No Asynchronous Execution: The current implementation does not specify
concurrency_limit, leading to sequential execution.
Queue Configuration: If queue() is not configured properly, tasks are
processed one at a time.
---------
Co-authored-by: imbajin <[email protected]>
---
.../src/hugegraph_llm/demo/rag_demo/rag_block.py | 10 ++++++----
hugegraph-llm/src/hugegraph_llm/indices/vector_index.py | 2 +-
hugegraph-llm/src/hugegraph_llm/utils/decorators.py | 17 +++++++++++++++++
.../src/pyhugegraph/utils/huge_config.py | 13 ++++++++-----
4 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/rag_block.py
b/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/rag_block.py
index e51e25e..baf7ef9 100644
--- a/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/rag_block.py
+++ b/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/rag_block.py
@@ -26,10 +26,10 @@ from gradio.utils import NamedString
from hugegraph_llm.config import resource_path, prompt, huge_settings,
llm_settings
from hugegraph_llm.operators.graph_rag_task import RAGPipeline
+from hugegraph_llm.utils.decorators import with_task_id
from hugegraph_llm.operators.llm_op.answer_synthesize import AnswerSynthesize
from hugegraph_llm.utils.log import log
-
def rag_answer(
text: str,
raw_answer: bool,
@@ -145,7 +145,6 @@ def update_ui_configs(
graph_search = graph_only_answer or graph_vector_answer
return graph_search, gremlin_prompt, vector_search
-
async def rag_answer_streaming(
text: str,
raw_answer: bool,
@@ -169,7 +168,6 @@ async def rag_answer_streaming(
4. Synthesize the final answer.
5. Run the pipeline and return the results.
"""
-
graph_search, gremlin_prompt, vector_search = update_ui_configs(
answer_prompt,
custom_related_information,
@@ -229,12 +227,13 @@ async def rag_answer_streaming(
log.critical(e)
raise gr.Error(f"An unexpected error occurred: {str(e)}")
-
+@with_task_id
def create_rag_block():
# pylint: disable=R0915 (too-many-statements),C0301
gr.Markdown("""## 1. HugeGraph RAG Query""")
with gr.Row():
with gr.Column(scale=2):
+ # with gr.Blocks().queue(max_size=20, default_concurrency_limit=5):
inp = gr.Textbox(value=prompt.default_question, label="Question",
show_copy_button=True, lines=3)
# TODO: Only support inline formula now. Should support block
formula
@@ -272,6 +271,7 @@ def create_rag_block():
show_copy_button=True,
lines=7,
)
+
with gr.Column(scale=1):
with gr.Row():
raw_radio = gr.Radio(choices=[True, False], value=False,
label="Basic LLM Answer")
@@ -325,6 +325,8 @@ def create_rag_block():
example_num,
],
outputs=[raw_out, vector_only_out, graph_only_out, graph_vector_out],
+ queue=True, # Enable queueing for this event
+ concurrency_limit=5, # Maximum of 5 concurrent executions
)
gr.Markdown(
diff --git a/hugegraph-llm/src/hugegraph_llm/indices/vector_index.py
b/hugegraph-llm/src/hugegraph_llm/indices/vector_index.py
index 7f93c3d..5e810ed 100644
--- a/hugegraph-llm/src/hugegraph_llm/indices/vector_index.py
+++ b/hugegraph-llm/src/hugegraph_llm/indices/vector_index.py
@@ -100,7 +100,7 @@ class VectorIndex:
results.append(deepcopy(self.properties[i]))
log.debug("[✓] Add valid distance %s to results.", dist)
else:
- log.debug("[x] Distance %s ≥ threshold %s, ignore this
result.", dist, dis_threshold)
+ log.debug("[x] Distance %s >= threshold %s, ignore this
result.", dist, dis_threshold)
return results
@staticmethod
diff --git a/hugegraph-llm/src/hugegraph_llm/utils/decorators.py
b/hugegraph-llm/src/hugegraph_llm/utils/decorators.py
index 394acda..7ffda08 100644
--- a/hugegraph-llm/src/hugegraph_llm/utils/decorators.py
+++ b/hugegraph-llm/src/hugegraph_llm/utils/decorators.py
@@ -90,3 +90,20 @@ def record_qps(func: Callable) -> Callable:
log.debug("%s QPS: %f/s", args[0].__class__.__name__, qps)
return result
return wrapper
+
+def with_task_id(func: Callable) -> Callable:
+ def wrapper(*args: Any, **kwargs: Any) -> Any:
+ import uuid
+ task_id = f"task_{str(uuid.uuid4())[:8]}"
+ log.debug("New task created with id: %s", task_id)
+
+ # Store the original return value
+ result = func(*args, **kwargs)
+
+ # Add the task_id to the function's context
+ if hasattr(result, "__closure__") and result.__closure__:
+ # If it's a closure, we can add the task_id to its context
+ setattr(result, "task_id", task_id)
+
+ return result
+ return wrapper
diff --git a/hugegraph-python-client/src/pyhugegraph/utils/huge_config.py
b/hugegraph-python-client/src/pyhugegraph/utils/huge_config.py
index 4794b7e..210f971 100644
--- a/hugegraph-python-client/src/pyhugegraph/utils/huge_config.py
+++ b/hugegraph-python-client/src/pyhugegraph/utils/huge_config.py
@@ -16,6 +16,7 @@
# under the License.
import re
+import sys
import traceback
from dataclasses import dataclass, field
from typing import List, Optional
@@ -63,8 +64,10 @@ class HGraphConfig:
)
except Exception as e: # pylint: disable=broad-exception-caught
- traceback.print_exception(e)
- self.gs_supported = False
- log.warning(
- "Failed to retrieve API version information from the
server, reverting to default v1."
- )
+ try:
+ traceback.print_exception(e)
+ self.gs_supported = False
+ except Exception: # pylint: disable=broad-exception-caught
+ exc_type, exc_value, tb = sys.exc_info()
+ traceback.print_exception(exc_type, exc_value, tb)
+ log.warning("Failed to retrieve API version information
from the server, reverting to default v1.")