(
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,
)
| 1239 | |
| 1240 | |
| 1241 | async 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 | } |
no test coverage detected