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 6e25d23 refactor(llm): add doc & disable text2gql by default (#216)
6e25d23 is described below
commit 6e25d231b918e1b27e5851ad9efa70b3ee7e37b5
Author: SoJGooo <[email protected]>
AuthorDate: Wed May 7 15:40:48 2025 +0800
refactor(llm): add doc & disable text2gql by default (#216)
Co-authored-by: imbajin <[email protected]>
---
hugegraph-llm/quick_start.md | 1 +
hugegraph-llm/src/hugegraph_llm/api/admin_api.py | 4 +++-
.../src/hugegraph_llm/config/prompt_config.py | 22 ++++++++++++++--------
.../hugegraph_llm/demo/rag_demo/configs_block.py | 2 +-
.../src/hugegraph_llm/demo/rag_demo/rag_block.py | 6 +++---
.../src/hugegraph_llm/operators/graph_rag_task.py | 2 +-
.../operators/hugegraph_op/graph_rag_query.py | 5 +++--
7 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/hugegraph-llm/quick_start.md b/hugegraph-llm/quick_start.md
index 4dd340c..dbbfe92 100644
--- a/hugegraph-llm/quick_start.md
+++ b/hugegraph-llm/quick_start.md
@@ -103,6 +103,7 @@ graph TD;
- **Question:** Input the query
- **Query** **Prompt:** The prompt template used to ask the final question to
the LLM
- **Keywords Extraction Prompt:** The prompt template for extracting keywords
from the question
+- **Template Num:** < 0 means disable text2gql; = 0 means no
template(zero-shot); > 0 means using the specified number of templates
### Query Scope Selection:
diff --git a/hugegraph-llm/src/hugegraph_llm/api/admin_api.py
b/hugegraph-llm/src/hugegraph_llm/api/admin_api.py
index a234c50..05648d4 100644
--- a/hugegraph-llm/src/hugegraph_llm/api/admin_api.py
+++ b/hugegraph-llm/src/hugegraph_llm/api/admin_api.py
@@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
+
import os
from fastapi import status, APIRouter
@@ -30,7 +31,8 @@ def admin_http_api(router: APIRouter, log_stream):
@router.post("/logs", status_code=status.HTTP_200_OK)
async def log_stream_api(req: LogStreamRequest):
if admin_settings.admin_token != req.admin_token:
- raise
generate_response(RAGResponse(status_code=status.HTTP_403_FORBIDDEN,
message="Invalid admin_token")) #pylint: disable=E0702
+ raise
generate_response(RAGResponse(status_code=status.HTTP_403_FORBIDDEN, #pylint:
disable=E0702
+ message="Invalid admin_token"))
log_path = os.path.join("logs", req.log_file)
# Create a StreamingResponse that reads from the log stream generator
diff --git a/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
b/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
index ca2b7b7..919c906 100644
--- a/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
+++ b/hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
@@ -26,7 +26,7 @@ class PromptConfig(BasePromptConfig):
Please provide precise and accurate answers based on the following context
information, which is sorted in order of importance from high to low, without
using any fabricated knowledge.
-Given the context information and without using fictive knowledge,
+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 `$...$`
@@ -53,7 +53,7 @@ Graph Schema:
- "edges": [List of edge labels, their source and target vertex labels, and
properties]
### Content Rule:
-Please read the provided text carefully and identify any information that
corresponds to the vertices and edges defined in the schema.
+Please read the provided text carefully and identify any information that
corresponds to the vertices and edges defined in the schema.
You are not allowed to modify the schema contraints. Your task is to format
the provided information into the required schema, without missing any keyword.
For each piece of information that matches a vertex or edge, format it
strictly according to the following JSON structures:
@@ -82,7 +82,7 @@ where:
- "sourceVertexId": "vertexLabelID:entityName"
- "targetVertexId": "vertexLabelID:entityName"
-Strictly follow these rules:
+Strictly follow these rules:
1. Don't extract property fields or labels that doesn't exist in the given
schema. Do not generate new information.
2. Ensure the extracted property set in the same type as the given schema
(like 'age' should be a number, 'select' should be a boolean).
3. If there are multiple primary keys, the strategy for generating VID is:
vertexlabelID:pk1!pk2!pk3 (pk means primary key, and '!' is the separator).
This id must be generated ONLY if there are multiple primary keys. If there is
only one primary key, the strategy for generating VID is: int (sequencially
increasing).
@@ -93,7 +93,7 @@ Refer to the following baseline example to understand the
output generation requ
## Example:
### Input example:
#### text:
-Meet Sarah, a 30-year-old attorney, and her roommate, James, whom she's shared
a home with since 2010. James, in his professional life, works as a journalist.
+Meet Sarah, a 30-year-old attorney, and her roommate, James, whom she's shared
a home with since 2010. James, in his professional life, works as a journalist.
#### graph schema example:
{"vertices":[{"vertex_label":"person","properties":["name","age","occupation"]}],
"edges":[{"edge_label":"roommate",
"source_vertex_label":"person","target_vertex_label":"person","properties":["date"]]}
@@ -165,22 +165,22 @@ Meet Sarah, a 30-year-old attorney, and her roommate,
James, whom she's shared a
- For these keywords that need rewriting, generate synonyms or similar
terms in the given context.
- Replace the corresponding keywords in the original text with
generated synonyms.
- If no suitable synonym exists for a keyword, keep the original
keyword unchanged.
-
+
Requirements:
- Keywords should be meaningful and specific entities; avoid meaningless
or overly broad terms, or single-character words (e.g., "items", "actions",
"effects", "functions", "the", "he").
- Prioritize extracting subjects, verbs, and objects; avoid function words
or auxiliary words.
- Maintain semantic integrity: Extracted keywords should preserve their
semantic and informational completeness in the original context (e.g., "Apple
computer" should be extracted as a whole, not split into "Apple" and
"computer").
- Avoid generalization: Do not expand into unrelated generalized
categories.
-
+
Notes:
- Only consider context-relevant synonyms: Only consider semantic synonyms
and words with similar meanings in the given context.
- Adjust keyword length: If keywords are relatively broad, you can
appropriately increase individual keyword length based on context (e.g.,
"illegal behavior" can be extracted as a single keyword, or as "illegal", but
should not be split into "illegal" and "behavior").
-
+
Output Format:
- Output only one line, prefixed with KEYWORDS:, followed by all keywords
or corresponding synonyms, separated by commas. No spaces or empty characters
are allowed in the extracted keywords.
- Format example:
KEYWORDS:keyword1,keyword2,...,keywordN
-
+
MAX_KEYWORDS: {max_keywords}
Text:
{question}
@@ -233,6 +233,9 @@ Referenced Extracted Vertex IDs Related to the Query:
Generate Gremlin from the Following User Query:
{query}
+
+**Important: Do NOT output any analysis, reasoning steps, explanations or any
other text. ONLY return the Gremlin query wrapped in a code block with
```gremlin``` fences.**
+
The generated Gremlin is:
"""
@@ -345,6 +348,9 @@ g.V().limit(10)
从以下用户查询生成Gremlin:
{query}
+
+**重要提示:请勿输出任何分析、推理步骤、解释或其他文本。仅返回用 ```gremlin``` 标记包装的 Gremlin 查询。**
+
生成的Gremlin是:
"""
diff --git a/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/configs_block.py
b/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/configs_block.py
index 74527d1..73d0600 100644
--- a/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/configs_block.py
+++ b/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/configs_block.py
@@ -213,7 +213,7 @@ def apply_llm_config(current_llm_config, arg1, arg2, arg3,
arg4, origin_call=Non
test_url = getattr(llm_settings,
f"openai_{current_llm_config}_api_base") + "/chat/completions"
data = {
"model": arg3,
- "temperature": 0.0,
+ "temperature": 0.01,
"messages": [{"role": "user", "content": "test"}],
}
headers = {"Authorization": f"Bearer {arg1}"}
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 baf7ef9..cc6bb44 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
@@ -42,7 +42,7 @@ def rag_answer(
custom_related_information: str,
answer_prompt: str,
keywords_extract_prompt: str,
- gremlin_tmpl_num: Optional[int] = 2,
+ gremlin_tmpl_num: Optional[int] = -1,
gremlin_prompt: Optional[str] = None,
max_graph_items=30,
topk_return_results=20,
@@ -157,7 +157,7 @@ async def rag_answer_streaming(
custom_related_information: str,
answer_prompt: str,
keywords_extract_prompt: str,
- gremlin_tmpl_num: Optional[int] = 2,
+ gremlin_tmpl_num: Optional[int] = -1,
gremlin_prompt: Optional[str] = None,
) -> AsyncGenerator[Tuple[str, str, str, str], None]:
"""
@@ -291,7 +291,7 @@ def create_rag_block():
value="reranker" if online_rerank else "bleu",
label="Rerank method",
)
- example_num = gr.Number(value=2, label="Template Num (0 to
disable it) ", precision=0)
+ example_num = gr.Number(value=-1, label="Template Num (<0
means disable text2gql) ", precision=0)
graph_ratio = gr.Slider(0, 1, 0.6, label="Graph Ratio",
step=0.1, interactive=False)
graph_vector_radio.change(
diff --git a/hugegraph-llm/src/hugegraph_llm/operators/graph_rag_task.py
b/hugegraph-llm/src/hugegraph_llm/operators/graph_rag_task.py
index 9c3dcf9..9f3d64d 100644
--- a/hugegraph-llm/src/hugegraph_llm/operators/graph_rag_task.py
+++ b/hugegraph-llm/src/hugegraph_llm/operators/graph_rag_task.py
@@ -128,7 +128,7 @@ class RAGPipeline:
max_v_prop_len: int = 2048,
max_e_prop_len: int = 256,
prop_to_match: Optional[str] = None,
- num_gremlin_generate_example: Optional[int] = 1,
+ num_gremlin_generate_example: Optional[int] = -1,
gremlin_prompt: Optional[str] = prompt.gremlin_generate_prompt,
):
"""
diff --git
a/hugegraph-llm/src/hugegraph_llm/operators/hugegraph_op/graph_rag_query.py
b/hugegraph-llm/src/hugegraph_llm/operators/hugegraph_op/graph_rag_query.py
index 53aff68..4b94c07 100644
--- a/hugegraph-llm/src/hugegraph_llm/operators/hugegraph_op/graph_rag_query.py
+++ b/hugegraph-llm/src/hugegraph_llm/operators/hugegraph_op/graph_rag_query.py
@@ -84,7 +84,7 @@ class GraphRAGQuery:
embedding: Optional[BaseEmbedding] = None,
max_v_prop_len: Optional[int] = 2048,
max_e_prop_len: Optional[int] = 256,
- num_gremlin_generate_example: Optional[int] = 1,
+ num_gremlin_generate_example: Optional[int] = -1,
gremlin_prompt: Optional[str] = None,
):
self._client = PyHugeClient(
@@ -115,7 +115,8 @@ class GraphRAGQuery:
# initial flag: -1 means no result, 0 means subgraph query, 1 means
gremlin query
context["graph_result_flag"] = -1
# 1. Try to perform a query based on the generated gremlin
- context = self._gremlin_generate_query(context)
+ if self._num_gremlin_generate_example >= 0:
+ context = self._gremlin_generate_query(context)
# 2. Try to perform a query based on subgraph-search if the previous
query failed
if not context.get("graph_result"):
context = self._subgraph_query(context)