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 fa2672a  refactor(llm): support mathjax in rag query block V1 (#157)
fa2672a is described below

commit fa2672a1376f2ddca5417e093750ccee9fec715c
Author: SoJGooo <[email protected]>
AuthorDate: Thu Jan 16 17:12:07 2025 +0800

    refactor(llm): support mathjax in rag query block V1 (#157)
    
    * Update gremlin_example_index_query.py
    
    ---------
    
    Co-authored-by: imbajin <[email protected]>
---
 .../src/hugegraph_llm/config/prompt_config.py        |  1 +
 .../src/hugegraph_llm/demo/rag_demo/rag_block.py     | 18 ++++++++++++------
 .../index_op/gremlin_example_index_query.py          |  7 ++++++-
 .../src/hugegraph_llm/resources/demo/css.py          | 20 ++++++++++++++++++++
 4 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py 
b/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
index fee85d2..51d84a1 100644
--- a/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
+++ b/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
@@ -26,6 +26,7 @@ Your task is to provide a precise and accurate answer based 
on the given context
 
 Given the context information and without using fictive knowledge, 
 answer the following query in a concise and professional manner.
+Please write your answer using Markdown with MathJax syntax, where inline math 
is wrapped with `$...$`
 
 Context information is below.
 ---------------------
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 477660b..ba2ab7e 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
@@ -112,15 +112,21 @@ def rag_answer(
 
 
 def create_rag_block():
-    # pylint: disable=R0915 (too-many-statements)
+    # pylint: disable=R0915 (too-many-statements),C0301
     gr.Markdown("""## 1. HugeGraph RAG Query""")
     with gr.Row():
         with gr.Column(scale=2):
-            inp = gr.Textbox(value=prompt.default_question, label="Question", 
show_copy_button=True, lines=2)
-            raw_out = gr.Textbox(label="Basic LLM Answer", 
show_copy_button=True)
-            vector_only_out = gr.Textbox(label="Vector-only Answer", 
show_copy_button=True)
-            graph_only_out = gr.Textbox(label="Graph-only Answer", 
show_copy_button=True)
-            graph_vector_out = gr.Textbox(label="Graph-Vector Answer", 
show_copy_button=True)
+            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
+            gr.Markdown("Basic LLM Answer", elem_classes="output-box-label")
+            raw_out = gr.Markdown(elem_classes="output-box", 
show_copy_button=True, latex_delimiters=[{"left":"$", "right":"$", 
"display":False}])
+            gr.Markdown("Vector-only Answer", elem_classes="output-box-label")
+            vector_only_out = gr.Markdown(elem_classes="output-box", 
show_copy_button=True, latex_delimiters=[{"left":"$", "right":"$", 
"display":False}])
+            gr.Markdown("Graph-only Answer", elem_classes="output-box-label")
+            graph_only_out = gr.Markdown(elem_classes="output-box", 
show_copy_button=True, latex_delimiters=[{"left":"$", "right":"$", 
"display":False}])
+            gr.Markdown("Graph-Vector Answer", elem_classes="output-box-label")
+            graph_vector_out = gr.Markdown(elem_classes="output-box", 
show_copy_button=True, latex_delimiters=[{"left":"$", "right":"$", 
"display":False}])
 
             answer_prompt_input = gr.Textbox(
                 value=prompt.answer_prompt, label="Query Prompt", 
show_copy_button=True, lines=7
diff --git 
a/hugegraph-llm/src/hugegraph_llm/operators/index_op/gremlin_example_index_query.py
 
b/hugegraph-llm/src/hugegraph_llm/operators/index_op/gremlin_example_index_query.py
index 4029995..31d9c2b 100644
--- 
a/hugegraph-llm/src/hugegraph_llm/operators/index_op/gremlin_example_index_query.py
+++ 
b/hugegraph-llm/src/hugegraph_llm/operators/index_op/gremlin_example_index_query.py
@@ -56,7 +56,12 @@ class GremlinExampleIndexQuery:
 
     def _build_default_example_index(self):
         properties = pd.read_csv(os.path.join(resource_path, "demo", 
"text2gremlin.csv")).to_dict(orient="records")
-        embeddings = [self.embedding.get_text_embedding(row["query"]) for row 
in tqdm(properties)]
+        from concurrent.futures import ThreadPoolExecutor
+        # TODO: use asyncio for IO tasks
+        with ThreadPoolExecutor() as executor:
+            embeddings = list(
+                tqdm(executor.map(self.embedding.get_text_embedding, 
[row["query"] for row in properties]),
+                     total=len(properties)))
         vector_index = VectorIndex(len(embeddings[0]))
         vector_index.add(embeddings, properties)
         vector_index.to_index_file(self.index_dir)
diff --git a/hugegraph-llm/src/hugegraph_llm/resources/demo/css.py 
b/hugegraph-llm/src/hugegraph_llm/resources/demo/css.py
index 117a068..0f2d48d 100644
--- a/hugegraph-llm/src/hugegraph_llm/resources/demo/css.py
+++ b/hugegraph-llm/src/hugegraph_llm/resources/demo/css.py
@@ -38,4 +38,24 @@ footer {
     white-space: -o-pre-wrap; /* Opera 7 */
     word-wrap: break-word; /* Internet Explorer 5.5+ */
 }
+
+.output-box {
+    border: 1px solid #ccc;
+    border-radius: 5px;
+    padding: 8px;
+    box-sizing: border-box;
+    min-height: 50px;
+    font-family: Arial, sans-serif;
+    line-height: 1.5;
+    margin-top: -5px;
+    width: 99.5%;
+    max-height: 300px;
+    overflow-y: auto;
+}
+
+.output-box-label {
+    font-size: 14px;
+    font-weight: bold;
+    margin-bottom: -5px;
+}
 """

Reply via email to