@@ -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
0 commit comments