2024-09-30 15:39:42 -07:00
|
|
|
# Copyright (c) 2024 Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License
|
|
|
|
|
2025-01-06 10:58:59 -08:00
|
|
|
from graphrag.callbacks.noop_workflow_callbacks import NoopWorkflowCallbacks
|
2025-01-03 13:59:26 -08:00
|
|
|
from graphrag.config.create_graphrag_config import create_graphrag_config
|
2024-10-15 12:58:58 -07:00
|
|
|
from graphrag.config.enums import LLMType
|
2025-01-03 13:59:26 -08:00
|
|
|
from graphrag.index.workflows.extract_graph import (
|
|
|
|
run_workflow,
|
2024-09-30 15:39:42 -07:00
|
|
|
)
|
2025-01-03 13:59:26 -08:00
|
|
|
from graphrag.utils.storage import load_table_from_storage
|
2024-09-30 15:39:42 -07:00
|
|
|
|
|
|
|
from .util import (
|
2025-01-21 15:52:06 -08:00
|
|
|
DEFAULT_MODEL_CONFIG,
|
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 15:39:42 -07:00
|
|
|
)
|
|
|
|
|
2024-10-15 12:58:58 -07:00
|
|
|
MOCK_LLM_ENTITY_RESPONSES = [
|
|
|
|
"""
|
|
|
|
("entity"<|>COMPANY_A<|>COMPANY<|>Company_A is a test company)
|
|
|
|
##
|
|
|
|
("entity"<|>COMPANY_B<|>COMPANY<|>Company_B owns Company_A and also shares an address with Company_A)
|
|
|
|
##
|
|
|
|
("entity"<|>PERSON_C<|>PERSON<|>Person_C is director of Company_A)
|
|
|
|
##
|
|
|
|
("relationship"<|>COMPANY_A<|>COMPANY_B<|>Company_A and Company_B are related because Company_A is 100% owned by Company_B and the two companies also share the same address)<|>2)
|
|
|
|
##
|
|
|
|
("relationship"<|>COMPANY_A<|>PERSON_C<|>Company_A and Person_C are related because Person_C is director of Company_A<|>1))
|
|
|
|
""".strip()
|
|
|
|
]
|
|
|
|
|
|
|
|
MOCK_LLM_SUMMARIZATION_RESPONSES = [
|
|
|
|
"""
|
|
|
|
This is a MOCK response for the LLM. It is summarized!
|
|
|
|
""".strip()
|
|
|
|
]
|
|
|
|
|
2024-09-30 15:39:42 -07:00
|
|
|
|
2024-12-11 13:41:16 -08:00
|
|
|
async def test_extract_graph():
|
2025-02-07 11:11:03 -08:00
|
|
|
nodes_expected = load_test_table("entities")
|
|
|
|
edges_expected = load_test_table("relationships")
|
2024-09-30 15:39:42 -07:00
|
|
|
|
2025-01-03 13:59:26 -08:00
|
|
|
context = await create_test_context(
|
2025-02-07 11:11:03 -08:00
|
|
|
storage=["text_units"],
|
2024-10-24 10:20:03 -07:00
|
|
|
)
|
2024-10-02 08:57:08 -07:00
|
|
|
|
2025-01-21 15:52:06 -08:00
|
|
|
config = create_graphrag_config({"models": DEFAULT_MODEL_CONFIG})
|
2025-02-07 11:11:03 -08:00
|
|
|
extract_claims_llm_settings = config.get_language_model_config(
|
|
|
|
config.extract_graph.model_id
|
2025-01-21 15:52:06 -08:00
|
|
|
).model_dump()
|
2025-02-07 11:11:03 -08:00
|
|
|
extract_claims_llm_settings["type"] = LLMType.StaticResponse
|
|
|
|
extract_claims_llm_settings["responses"] = MOCK_LLM_ENTITY_RESPONSES
|
|
|
|
config.extract_graph.strategy = {
|
2025-01-03 13:59:26 -08:00
|
|
|
"type": "graph_intelligence",
|
2025-02-07 11:11:03 -08:00
|
|
|
"llm": extract_claims_llm_settings,
|
2025-01-03 13:59:26 -08:00
|
|
|
}
|
2025-01-21 15:52:06 -08:00
|
|
|
summarize_llm_settings = config.get_language_model_config(
|
|
|
|
config.summarize_descriptions.model_id
|
|
|
|
).model_dump()
|
|
|
|
summarize_llm_settings["type"] = LLMType.StaticResponse
|
|
|
|
summarize_llm_settings["responses"] = MOCK_LLM_SUMMARIZATION_RESPONSES
|
2025-01-03 13:59:26 -08:00
|
|
|
config.summarize_descriptions.strategy = {
|
|
|
|
"type": "graph_intelligence",
|
2025-01-21 15:52:06 -08:00
|
|
|
"llm": summarize_llm_settings,
|
2025-01-03 13:59:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
await run_workflow(
|
|
|
|
config,
|
|
|
|
context,
|
2025-01-06 10:58:59 -08:00
|
|
|
NoopWorkflowCallbacks(),
|
2024-09-30 15:39:42 -07:00
|
|
|
)
|
|
|
|
|
2025-02-07 11:11:03 -08:00
|
|
|
nodes_actual = await load_table_from_storage("entities", context.storage)
|
|
|
|
edges_actual = await load_table_from_storage("relationships", context.storage)
|
2024-12-05 09:57:26 -08:00
|
|
|
|
2024-12-06 14:08:24 -06:00
|
|
|
assert len(nodes_actual.columns) == len(nodes_expected.columns), (
|
|
|
|
"Nodes dataframe columns differ"
|
|
|
|
)
|
2024-11-04 17:23:29 -08:00
|
|
|
|
2024-12-06 14:08:24 -06:00
|
|
|
assert len(edges_actual.columns) == len(edges_expected.columns), (
|
|
|
|
"Edges dataframe columns differ"
|
|
|
|
)
|
2024-09-30 15:39:42 -07:00
|
|
|
|
2024-10-15 12:58:58 -07:00
|
|
|
# TODO: with the combined verb we can't force summarization
|
|
|
|
# this is because the mock responses always result in a single description, which is returned verbatim rather than summarized
|
|
|
|
# we need to update the mocking to provide somewhat unique graphs so a true merge happens
|
|
|
|
# the assertion should grab a node and ensure the description matches the mock description, not the original as we are doing below
|
2024-12-19 14:43:21 -05:00
|
|
|
assert nodes_actual["description"].to_numpy()[0] == "Company_A is a test company"
|