MCPcopy
hub / github.com/HKUDS/MiniRAG / _build_mini_query_context

Function _build_mini_query_context

minirag/operate.py:1241–1395  ·  view source on GitHub ↗
(
    ent_from_query,
    type_keywords,
    originalquery,
    knowledge_graph_inst: BaseGraphStorage,
    entities_vdb: BaseVectorStorage,
    entity_name_vdb: BaseVectorStorage,
    relationships_vdb: BaseVectorStorage,
    chunks_vdb: BaseVectorStorage,
    text_chunks_db: BaseKVStorage[TextChunkSchema],
    embedder,
    query_param: QueryParam,
)

Source from the content-addressed store, hash-verified

1239
1240
1241async def _build_mini_query_context(
1242 ent_from_query,
1243 type_keywords,
1244 originalquery,
1245 knowledge_graph_inst: BaseGraphStorage,
1246 entities_vdb: BaseVectorStorage,
1247 entity_name_vdb: BaseVectorStorage,
1248 relationships_vdb: BaseVectorStorage,
1249 chunks_vdb: BaseVectorStorage,
1250 text_chunks_db: BaseKVStorage[TextChunkSchema],
1251 embedder,
1252 query_param: QueryParam,
1253):
1254 imp_ents = []
1255 nodes_from_query_list = []
1256 ent_from_query_dict = {}
1257
1258 for ent in ent_from_query:
1259 ent_from_query_dict[ent] = []
1260 results_node = await entity_name_vdb.query(ent, top_k=query_param.top_k)
1261
1262 nodes_from_query_list.append(results_node)
1263 ent_from_query_dict[ent] = [e["entity_name"] for e in results_node]
1264
1265 candidate_reasoning_path = {}
1266
1267 for results_node_list in nodes_from_query_list:
1268 candidate_reasoning_path_new = {
1269 key["entity_name"]: {"Score": key["distance"], "Path": []}
1270 for key in results_node_list
1271 }
1272
1273 candidate_reasoning_path = {
1274 **candidate_reasoning_path,
1275 **candidate_reasoning_path_new,
1276 }
1277 for key in candidate_reasoning_path.keys():
1278 candidate_reasoning_path[key][
1279 "Path"
1280 ] = await knowledge_graph_inst.get_neighbors_within_k_hops(key, 2)
1281 imp_ents.append(key)
1282
1283 short_path_entries = {
1284 name: entry
1285 for name, entry in candidate_reasoning_path.items()
1286 if len(entry["Path"]) < 1
1287 }
1288 sorted_short_path_entries = sorted(
1289 short_path_entries.items(), key=lambda x: x[1]["Score"], reverse=True
1290 )
1291 save_p = max(1, int(len(sorted_short_path_entries) * 0.2))
1292 top_short_path_entries = sorted_short_path_entries[:save_p]
1293 top_short_path_dict = {name: entry for name, entry in top_short_path_entries}
1294 long_path_entries = {
1295 name: entry
1296 for name, entry in candidate_reasoning_path.items()
1297 if len(entry["Path"]) >= 1
1298 }

Callers 1

minirag_queryFunction · 0.85

Calls 14

cal_path_score_listFunction · 0.85
edge_vote_pathFunction · 0.85
path2chunkFunction · 0.85
list_of_list_to_csvFunction · 0.85
scorednode2chunkFunction · 0.85
kwd2chunkFunction · 0.85
getMethod · 0.80
insertMethod · 0.80
queryMethod · 0.45
get_node_from_typesMethod · 0.45

Tested by

no test coverage detected