|
| 1 | +# Copyright (c) 2024 Microsoft Corporation. |
| 2 | +# Licensed under the MIT License |
| 3 | + |
| 4 | +from graphrag.callbacks.noop_workflow_callbacks import NoopWorkflowCallbacks |
| 5 | +from graphrag.config.create_graphrag_config import create_graphrag_config |
| 6 | +from graphrag.data_model.schemas import ( |
| 7 | + ENTITIES_FINAL_COLUMNS, |
| 8 | + RELATIONSHIPS_FINAL_COLUMNS, |
| 9 | +) |
| 10 | +from graphrag.index.workflows.finalize_graph import ( |
| 11 | + run_workflow, |
| 12 | +) |
| 13 | +from graphrag.utils.storage import load_table_from_storage, write_table_to_storage |
| 14 | + |
| 15 | +from .util import ( |
| 16 | + DEFAULT_MODEL_CONFIG, |
| 17 | + create_test_context, |
| 18 | + load_test_table, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +async def test_finalize_graph(): |
| 23 | + context = await _prep_tables() |
| 24 | + |
| 25 | + config = create_graphrag_config({"models": DEFAULT_MODEL_CONFIG}) |
| 26 | + |
| 27 | + await run_workflow( |
| 28 | + config, |
| 29 | + context, |
| 30 | + NoopWorkflowCallbacks(), |
| 31 | + ) |
| 32 | + |
| 33 | + nodes_actual = await load_table_from_storage("entities", context.storage) |
| 34 | + edges_actual = await load_table_from_storage("relationships", context.storage) |
| 35 | + |
| 36 | + assert len(nodes_actual) == 251 |
| 37 | + assert len(edges_actual) == 372 |
| 38 | + |
| 39 | + # x and y will be zero with the default configuration, because we do not embed/umap |
| 40 | + assert nodes_actual["x"].sum() == 0 |
| 41 | + assert nodes_actual["y"].sum() == 0 |
| 42 | + |
| 43 | + for column in ENTITIES_FINAL_COLUMNS: |
| 44 | + assert column in nodes_actual.columns |
| 45 | + for column in RELATIONSHIPS_FINAL_COLUMNS: |
| 46 | + assert column in edges_actual.columns |
| 47 | + |
| 48 | + |
| 49 | +async def test_finalize_graph_umap(): |
| 50 | + context = await _prep_tables() |
| 51 | + |
| 52 | + config = create_graphrag_config({"models": DEFAULT_MODEL_CONFIG}) |
| 53 | + |
| 54 | + config.embed_graph.enabled = True |
| 55 | + config.umap.enabled = True |
| 56 | + |
| 57 | + await run_workflow( |
| 58 | + config, |
| 59 | + context, |
| 60 | + NoopWorkflowCallbacks(), |
| 61 | + ) |
| 62 | + |
| 63 | + nodes_actual = await load_table_from_storage("entities", context.storage) |
| 64 | + edges_actual = await load_table_from_storage("relationships", context.storage) |
| 65 | + |
| 66 | + assert len(nodes_actual) == 251 |
| 67 | + assert len(edges_actual) == 372 |
| 68 | + |
| 69 | + # x and y should have some value other than zero due to umap |
| 70 | + assert nodes_actual["x"].sum() != 0 |
| 71 | + assert nodes_actual["y"].sum() != 0 |
| 72 | + |
| 73 | + for column in ENTITIES_FINAL_COLUMNS: |
| 74 | + assert column in nodes_actual.columns |
| 75 | + for column in RELATIONSHIPS_FINAL_COLUMNS: |
| 76 | + assert column in edges_actual.columns |
| 77 | + |
| 78 | + |
| 79 | +async def _prep_tables(): |
| 80 | + context = await create_test_context( |
| 81 | + storage=["entities", "relationships"], |
| 82 | + ) |
| 83 | + |
| 84 | + # edit the tables to eliminate final fields that wouldn't be on the inputs |
| 85 | + entities = load_test_table("entities") |
| 86 | + entities.drop(columns=["x", "y", "degree"], inplace=True) |
| 87 | + await write_table_to_storage(entities, "entities", context.storage) |
| 88 | + relationships = load_test_table("relationships") |
| 89 | + relationships.drop(columns=["combined_degree"], inplace=True) |
| 90 | + await write_table_to_storage(relationships, "relationships", context.storage) |
| 91 | + return context |
0 commit comments