MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / find_path

Function find_path

database_utils.py:249–294  ·  view source on GitHub ↗
(entity1,entity2,working_dir,level,depth=5)

Source from the content-addressed store, hash-verified

247 return res
248
249def find_path(entity1,entity2,working_dir,level,depth=5):
250 db = pymysql.connect(host='localhost',port=4321, user='root',
251 passwd='123', charset='utf8mb4')
252 db_name=os.path.basename(working_dir)
253 cursor = db.cursor()
254
255 query = f"""
256 WITH RECURSIVE path_cte AS (
257 SELECT
258 src_tgt,
259 tgt_src,
260 CAST(CONCAT(src_tgt, '|', tgt_src) AS CHAR(5000)) AS path,
261 1 AS depth
262 FROM {db_name}.relations
263 WHERE src_tgt = %s
264 AND level = %s
265
266 UNION ALL
267
268 SELECT
269 p.src_tgt,
270 t.tgt_src,
271 CONCAT(p.path, '|', t.tgt_src),
272 p.depth + 1
273 FROM path_cte p
274 JOIN {db_name}.relations t ON p.tgt_src = t.src_tgt
275 WHERE NOT FIND_IN_SET(
276 CONVERT(t.tgt_src USING utf8mb4) COLLATE utf8mb4_unicode_ci,
277 CONVERT(p.path USING utf8mb4) COLLATE utf8mb4_unicode_ci
278 )
279 AND level = %s
280 AND p.depth < %s
281 )
282 SELECT path
283 FROM path_cte
284 WHERE tgt_src = %s
285 ORDER BY depth ASC
286 LIMIT 1;
287 """
288 cursor.execute(query, (entity1,level,level,depth,entity2))
289 result = cursor.fetchone()
290
291 if result:
292 return result[0].split('|') # 返回节点列表
293 else:
294 return None
295
296def search_nodes_link(entity1,entity2,working_dir,level=0):
297 # cursor = db.cursor()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected