2024-09-30 10:46:07 -07:00
|
|
|
# Copyright (c) 2024 Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License
|
|
|
|
|
2024-10-09 13:46:44 -07:00
|
|
|
|
2025-01-03 13:59:26 -08:00
|
|
|
from graphrag.config.create_graphrag_config import create_graphrag_config
|
2025-02-20 08:56:20 -06:00
|
|
|
from graphrag.config.enums import ModelType
|
2025-02-27 09:31:46 -08:00
|
|
|
from graphrag.data_model.schemas import COMMUNITY_REPORTS_FINAL_COLUMNS
|
2025-02-07 11:11:03 -08:00
|
|
|
from graphrag.index.operations.summarize_communities.community_reports_extractor import (
|
2024-12-05 16:07:47 -08:00
|
|
|
CommunityReportResponse,
|
|
|
|
FindingModel,
|
|
|
|
)
|
2025-02-07 11:11:03 -08:00
|
|
|
from graphrag.index.workflows.create_community_reports import (
|
2025-01-03 13:59:26 -08:00
|
|
|
run_workflow,
|
2024-09-30 10:46:07 -07:00
|
|
|
)
|
2025-01-03 13:59:26 -08:00
|
|
|
from graphrag.utils.storage import load_table_from_storage
|
2024-09-30 10:46:07 -07:00
|
|
|
|
|
|
|
from .util import (
|
2025-01-21 15:52:06 -08:00
|
|
|
DEFAULT_MODEL_CONFIG,
|
2024-09-30 10:46:07 -07:00
|
|
|
compare_outputs,
|
2025-01-03 13:59:26 -08:00
|
|
|
create_test_context,
|
2024-12-05 09:57:26 -08:00
|
|
|
load_test_table,
|
2024-09-30 10:46:07 -07:00
|
|
|
)
|
|
|
|
|
2024-10-09 13:46:44 -07:00
|
|
|
MOCK_RESPONSES = [
|
2024-12-05 16:07:47 -08:00
|
|
|
CommunityReportResponse(
|
|
|
|
title="<report_title>",
|
|
|
|
summary="<executive_summary>",
|
|
|
|
rating=2,
|
|
|
|
rating_explanation="<rating_explanation>",
|
|
|
|
findings=[
|
|
|
|
FindingModel(
|
|
|
|
summary="<insight_1_summary>", explanation="<insight_1_explanation"
|
|
|
|
),
|
|
|
|
FindingModel(
|
|
|
|
summary="<insight_2_summary>", explanation="<insight_2_explanation"
|
|
|
|
),
|
2024-10-09 13:46:44 -07:00
|
|
|
],
|
2024-12-05 16:07:47 -08:00
|
|
|
)
|
2024-10-09 13:46:44 -07:00
|
|
|
]
|
|
|
|
|
2024-09-30 10:46:07 -07:00
|
|
|
|
2025-02-07 11:11:03 -08:00
|
|
|
async def test_create_community_reports():
|
|
|
|
expected = load_test_table("community_reports")
|
2024-09-30 10:46:07 -07:00
|
|
|
|
2025-01-03 13:59:26 -08:00
|
|
|
context = await create_test_context(
|
|
|
|
storage=[
|
2025-02-07 11:11:03 -08:00
|
|
|
"covariates",
|
|
|
|
"relationships",
|
|
|
|
"entities",
|
|
|
|
"communities",
|
2025-01-03 13:59:26 -08:00
|
|
|
]
|
|
|
|
)
|
2024-09-30 10:46:07 -07:00
|
|
|
|
2025-01-21 15:52:06 -08:00
|
|
|
config = create_graphrag_config({"models": DEFAULT_MODEL_CONFIG})
|
|
|
|
llm_settings = config.get_language_model_config(
|
|
|
|
config.community_reports.model_id
|
|
|
|
).model_dump()
|
2025-02-20 08:56:20 -06:00
|
|
|
llm_settings["type"] = ModelType.MockChat
|
2025-01-21 15:52:06 -08:00
|
|
|
llm_settings["responses"] = MOCK_RESPONSES
|
|
|
|
llm_settings["parse_json"] = True
|
2025-01-03 13:59:26 -08:00
|
|
|
config.community_reports.strategy = {
|
|
|
|
"type": "graph_intelligence",
|
2025-01-21 15:52:06 -08:00
|
|
|
"llm": llm_settings,
|
2025-02-12 10:41:39 -08:00
|
|
|
"graph_prompt": "",
|
2025-01-03 13:59:26 -08:00
|
|
|
}
|
2024-09-30 10:46:07 -07:00
|
|
|
|
2025-02-28 09:31:48 -08:00
|
|
|
await run_workflow(config, context)
|
2024-09-30 10:46:07 -07:00
|
|
|
|
2025-02-07 11:11:03 -08:00
|
|
|
actual = await load_table_from_storage("community_reports", context.storage)
|
2025-01-03 13:59:26 -08:00
|
|
|
|
2024-09-30 10:46:07 -07:00
|
|
|
assert len(actual.columns) == len(expected.columns)
|
|
|
|
|
|
|
|
# only assert a couple of columns that are not mock - most of this table is LLM-generated
|
|
|
|
compare_outputs(actual, expected, columns=["community", "level"])
|
|
|
|
|
|
|
|
# assert a handful of mock data items to confirm they get put in the right spot
|
|
|
|
assert actual["rank"][:1][0] == 2
|
2025-02-25 11:14:42 -08:00
|
|
|
assert actual["rating_explanation"][:1][0] == "<rating_explanation>"
|
2025-02-27 09:31:46 -08:00
|
|
|
|
|
|
|
for column in COMMUNITY_REPORTS_FINAL_COLUMNS:
|
|
|
|
assert column in actual.columns
|