Skip to content

Commit 7410a8a

Browse files
committed
refine
1 parent 2042962 commit 7410a8a

6 files changed

Lines changed: 95 additions & 211 deletions

File tree

dataflow/operators/graph_rag/generate/graphrag_prompt_generator.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class KGGraphRAGSubgraphRetrieval(OperatorABC):
1818
- question: List[str] # 单个行包含多个问题
1919
- entities: List[List[str]] # 每个子列表对应一个问题的实体
2020
- relations: List[List[str]]
21-
- triplet: List[str]
21+
- triple: List[str]
2222
2323
Output columns:
2424
- subgraph_prompt: List[str] # 每个问题对应一个prompt
@@ -33,27 +33,27 @@ def get_desc(lang: str = "en") -> tuple:
3333
if lang == "zh":
3434
return (
3535
"KGGraphRAGSubgraphRetrieval 用于围绕问题实体检索子图并生成 GraphRAG 提示词。",
36-
"输入: question + entities + triplet; 输出: subgraph_prompt",
36+
"输入: question + entities + triple; 输出: subgraph_prompt",
3737
)
3838
return (
3939
"KGGraphRAGSubgraphRetrieval is used to retrieve subgraphs around question entities and build GraphRAG prompts.",
40-
"Input: question + entities + triplet; Output: subgraph_prompt",
40+
"Input: question + entities + triple; Output: subgraph_prompt",
4141
)
4242

4343
# --------------------------------------------------
44-
# Triplet parsing
44+
# triple parsing
4545
# --------------------------------------------------
4646
@staticmethod
47-
def _parse_triplet(triplet: str):
47+
def _parse_triple(triple: str):
4848
"""
4949
Parse:
5050
"<subj> Henry <obj> Maria Rodriguez <rel> is_trained_by"
5151
-> (Henry, is_trained_by, Maria Rodriguez)
5252
"""
5353
try:
54-
subj = triplet.split("<subj>")[1].split("<obj>")[0].strip()
55-
obj = triplet.split("<obj>")[1].split("<rel>")[0].strip()
56-
rel = triplet.split("<rel>")[1].strip()
54+
subj = triple.split("<subj>")[1].split("<obj>")[0].strip()
55+
obj = triple.split("<obj>")[1].split("<rel>")[0].strip()
56+
rel = triple.split("<rel>")[1].strip()
5757
return subj, rel, obj
5858
except Exception:
5959
return None, None, None
@@ -62,10 +62,10 @@ def _parse_triplet(triplet: str):
6262
# Entity catalog induction
6363
# --------------------------------------------------
6464
@classmethod
65-
def _build_entity_catalog(cls, triplets: List[str]):
65+
def _build_entity_catalog(cls, triples: List[str]):
6666
entities = set()
67-
for t in triplets:
68-
h, _, o = cls._parse_triplet(t)
67+
for t in triples:
68+
h, _, o = cls._parse_triple(t)
6969
if h:
7070
entities.add(h)
7171
if o:
@@ -78,21 +78,21 @@ def _build_entity_catalog(cls, triplets: List[str]):
7878
@classmethod
7979
def _k_hop_subgraph(
8080
cls,
81-
triplets: List[str],
81+
triples: List[str],
8282
start_entity: str,
8383
hop: int,
8484
):
8585
adj = defaultdict(list)
8686

87-
for t in triplets:
88-
h, r, o = cls._parse_triplet(t)
87+
for t in triples:
88+
h, r, o = cls._parse_triple(t)
8989
if h is None:
9090
continue
9191
adj[h].append((o, t))
9292
adj[o].append((h, t)) # treat as undirected
9393

9494
visited_entities = {start_entity}
95-
visited_triplets = set()
95+
visited_triples = set()
9696
queue = deque([(start_entity, 0)])
9797

9898
while queue:
@@ -101,12 +101,12 @@ def _k_hop_subgraph(
101101
continue
102102

103103
for nxt, raw_t in adj.get(cur, []):
104-
visited_triplets.add(raw_t)
104+
visited_triples.add(raw_t)
105105
if nxt not in visited_entities:
106106
visited_entities.add(nxt)
107107
queue.append((nxt, depth + 1))
108108

109-
return list(visited_triplets)
109+
return list(visited_triples)
110110

111111
# --------------------------------------------------
112112
# Prompt construction (单个问题)
@@ -115,7 +115,7 @@ def _build_single_prompt(
115115
self,
116116
question: str,
117117
entities: List[str],
118-
triplets: List[str],
118+
triples: List[str],
119119
) -> str:
120120
# 标准化实体列表(处理嵌套列表情况)
121121
normalized_entities = []
@@ -129,7 +129,7 @@ def _build_single_prompt(
129129
normalized_entities = list(set(normalized_entities))
130130

131131
# 1. 构建KG中的实体目录
132-
entity_catalog = self._build_entity_catalog(triplets)
132+
entity_catalog = self._build_entity_catalog(triples)
133133

134134
# 2. 种子实体 = 提取的实体 ∩ KG中的实体
135135
seed_entities = [
@@ -144,7 +144,7 @@ def _build_single_prompt(
144144
subgraphs = {}
145145
for ent in seed_entities:
146146
subgraphs[ent] = self._k_hop_subgraph(
147-
triplets,
147+
triples,
148148
start_entity=ent,
149149
hop=self.hop,
150150
)
@@ -163,7 +163,7 @@ def _build_single_prompt(
163163
for t in sg:
164164
lines.append(f"- {t}")
165165
else:
166-
lines.append("- No relevant triplets found")
166+
lines.append("- No relevant triples found")
167167
lines.append("")
168168

169169
lines.append("Answer the question based on the above knowledge graph subgraphs.")
@@ -216,9 +216,9 @@ def run(
216216
entities_list = [[] for _ in questions]
217217

218218
# 3. 提取三元组
219-
triplets = row.get("triple", [])
220-
if triplets is None or not isinstance(triplets, list):
221-
triplets = []
219+
triples = row.get("triple", [])
220+
if triples is None or not isinstance(triples, list):
221+
triples = []
222222

223223
# 4. 确保问题数量和实体列表数量匹配
224224
max_len = max(len(questions), len(entities_list))
@@ -238,7 +238,7 @@ def run(
238238
prompt = self._build_single_prompt(
239239
question=q,
240240
entities=ents,
241-
triplets=triplets,
241+
triples=triples,
242242
)
243243
row_prompts.append(prompt)
244244

dataflow/prompts/core_kg/attri_triple.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ def build_system_prompt(self):
354354
355355
=== OUTPUT FORMAT (STRICT JSON) ===
356356
{
357-
"QA_pairs": [
358-
"Question: ... Answer: ...",
359-
"Question: ... Answer: ..."
360-
]
357+
"QA_pairs": [{
358+
"question": "...", "answer": "..."},
359+
{"question": "...", "answer": "..."
360+
}]
361361
}
362362
363363
Do NOT explain reasoning.
@@ -413,10 +413,10 @@ def build_system_prompt(self):
413413
414414
=== 输出格式(严格 JSON)===
415415
{
416-
"QA_pairs": [
417-
"Question: ... Answer: ...",
418-
"Question: ... Answer: ..."
419-
]
416+
"QA_pairs": [{
417+
"question": "...", "answer": "..."},
418+
{"question": "...", "answer": "..."
419+
}]
420420
}
421421
422422
不输出推理过程,不提及三元组本身。

dataflow/prompts/core_kg/rel_triple_eval.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ class KGQAConcisenessPrompt(PromptABC):
522522
Evaluate the conciseness of QA pairs.
523523
524524
Each QA pair is formatted as:
525-
"Question: ... Answer: ..."
525+
{"question": "...", "answer": "..."}
526526
527527
The model should score each QA pair independently based on how concise
528528
and direct the answer is.
@@ -619,7 +619,7 @@ class KGQACorrelationPrompt(PromptABC):
619619
Evaluate the correlation between question and answer in QA pairs.
620620
621621
Each QA pair is formatted as:
622-
"Question: ... Answer: ..."
622+
{"question": "...", "answer": "..."}
623623
624624
The model should determine whether the answer actually responds
625625
to the question.
@@ -722,7 +722,7 @@ class KGQANaturalnessPrompt(PromptABC):
722722
Evaluate the naturalness of QA pairs.
723723
724724
Each QA pair is formatted as:
725-
"Question: ... Answer: ..."
725+
{"question": "...", "answer": "..."}
726726
727727
The model should judge whether the QA pair sounds natural,
728728
fluent, and human-like.

dataflow/prompts/core_kg/rel_triple_generate.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,10 @@ def build_system_prompt(self):
552552
553553
Output format (STRICT JSON):
554554
{
555-
"QA_pairs": [
556-
"Question: ... Answer: ...",
557-
"Question: ... Answer: ..."
558-
]
555+
"QA_pairs": [{
556+
"question": "...", "answer": "..."},
557+
{"question": "...", "answer": "..."
558+
}]
559559
}
560560
561561
Triples:
@@ -588,10 +588,10 @@ def build_system_prompt(self):
588588
589589
【输出格式(严格 JSON)】:
590590
{
591-
"QA_pairs": [
592-
"Question: ... Answer: ...",
593-
"Question: ... Answer: ..."
594-
]
591+
"QA_pairs": [{
592+
"question": "...", "answer": "..."},
593+
{"question": "...", "answer": "..."
594+
}]
595595
}
596596
597597
待处理三元组:
@@ -672,10 +672,10 @@ def build_system_prompt(self):
672672
673673
=== OUTPUT FORMAT (STRICT JSON, DO NOT CHANGE) ===
674674
{
675-
"QA_pairs": [
676-
"Question: ... Answer: ...",
677-
"Question: ... Answer: ..."
678-
]
675+
"QA_pairs": [{
676+
"question": "...", "answer": "..."},
677+
{"question": "...", "answer": "..."
678+
}]
679679
}
680680
681681
Two-hop paths:
@@ -715,10 +715,10 @@ def build_system_prompt(self):
715715
716716
=== 输出格式(严格 JSON,不得更改)===
717717
{
718-
"QA_pairs": [
719-
"Question: ... Answer: ...",
720-
"Question: ... Answer: ..."
721-
]
718+
"QA_pairs": [{
719+
"question": "...", "answer": "..."},
720+
{"question": "...", "answer": "..."
721+
}]
722722
}
723723
724724
二跳路径:

0 commit comments

Comments
 (0)