Skip to content

Commit

Permalink
improve prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
imbajin committed Dec 22, 2024
1 parent 79c94cf commit 550cbd7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
8 changes: 5 additions & 3 deletions hugegraph-llm/src/hugegraph_llm/api/rag_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ def rag_answer_api(req: RAGRequest):
gremlin_tmpl_num=req.gremlin_tmpl_num,
gremlin_prompt=req.gremlin_prompt or prompt.gremlin_generate_prompt,
)
# TODO: we need more info in the response for users to understand the query logic
return {
key: value
for key, value in zip(["raw_answer", "vector_only", "graph_only", "graph_vector_answer"], result)
if getattr(req, key)
"query": req.query,
**{key: value
for key, value in zip(["raw_answer", "vector_only", "graph_only", "graph_vector_answer"], result)
if getattr(req, key)}
}

@router.post("/rag/graph", status_code=status.HTTP_200_OK)
Expand Down
27 changes: 15 additions & 12 deletions hugegraph-llm/src/hugegraph_llm/config/prompt_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,15 @@ class PromptConfig(BasePromptConfig):
- 格式示例:
KEYWORDS:关键词1,关键词2,...,关键词n
MAX_KEYWORDS:
{max_keywords}
MAX_KEYWORDS: {max_keywords}
文本:
{question}
"""
# pylint: disable=C0301
# keywords_extract_prompt_EN = """
# Instruction:
# Please perform the following tasks on the text below:
# 1. Extract Keywords and Generate Synonyms from text:
# 1. Extract Keywords and Generate Synonyms from the text:
# - At least 0, at most {max_keywords} keywords.
# - For each keyword, generate its synonyms or possible variant forms.
# Requirements:
Expand All @@ -174,29 +173,33 @@ class PromptConfig(BasePromptConfig):
# Output Format:
# - Output only one line, prefixed with KEYWORDS:, followed by all keywords and synonyms, separated by commas.No spaces or empty characters are allowed in the extracted keywords.
# - Format example:
# KEYWORDS: keyword1, keyword2, ..., keywordn, synonym1, synonym2, ..., synonymn
# KEYWORDS: keyword1, keyword2, ..., keywordN, synonym1, synonym2, ..., synonymN
# Text:
# {question}
# """

gremlin_generate_prompt = """
Given the graph schema:
You are an expert in graph query language(Gremlin), your role is to understand the schema of the graph and generate
accurate Gremlin code based on the given instructions.
# Graph Schema:
```json
{schema}
```
# Rule:
The output format must be like:
```gremlin
g.V().limit(10)
```
Given the extracted vertex vid:
# Extracted vertex vid:
{vertices}
Given the example query-gremlin pairs:
# Given the example query-gremlin pairs:
{example}
Generate gremlin from the following user query.
# Generate gremlin from the following user query.
{query}
The output format must be like:
```gremlin
g.V().limit(10)
```
The generated gremlin is:
"""
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
from hugegraph_llm.config import prompt, resource_path, huge_settings
from hugegraph_llm.models.embeddings.init_embedding import Embeddings
from hugegraph_llm.models.llms.init_llm import LLMs
from hugegraph_llm.operators.gremlin_generate_task import GremlinGenerator
from hugegraph_llm.operators.graph_rag_task import RAGPipeline
from hugegraph_llm.operators.gremlin_generate_task import GremlinGenerator
from hugegraph_llm.operators.hugegraph_op.schema_manager import SchemaManager
from hugegraph_llm.utils.hugegraph_utils import run_gremlin_query
from hugegraph_llm.utils.log import log


def store_schema(schema, question, gremlin_prompt):
if (
prompt.text2gql_graph_schema != schema
or prompt.default_question != question
or prompt.gremlin_generate_prompt != gremlin_prompt
prompt.text2gql_graph_schema != schema or
prompt.default_question != question or
prompt.gremlin_generate_prompt != gremlin_prompt
):
prompt.text2gql_graph_schema = schema
prompt.default_question = question
Expand Down Expand Up @@ -90,8 +90,7 @@ def gremlin_generate(
updated_schema = sm.simple_schema(schema) if short_schema else schema
store_schema(str(updated_schema), inp, gremlin_prompt)
context = (
generator.example_index_query(example_num)
.gremlin_generate_synthesize(updated_schema, gremlin_prompt)
generator.example_index_query(example_num).gremlin_generate_synthesize(updated_schema, gremlin_prompt)
.run(query=inp)
)
try:
Expand Down

0 comments on commit 550cbd7

Please sign in to comment.