graphrag/tests/verbs/test_extract_graph.py
Nathan Evans c02ab0984a
Streamline workflows (#1674)
* Remove create_final_nodes

* Rename final entity output to "entities"

* Remove duplicate code from graph extraction

* Rename create_final_relationships output to "relationships"

* Rename create_final_communities output to "communities"

* Combine compute_communities and create_final_communities

* Rename create_final_covariates output to "covariates"

* Rename create_final_community_reports output to "community_reports"

* Rename create_final_text_units output to "text_units"

* Rename create_final_documents output to "documents"

* Remove transient snapshots config

* Move create_final_entities to finalize_entities operation

* Move create_final_relationships flow to finalize_relationships operation

* Reuse some community report functions

* Collapse most of graph and text unit-based report generation

* Unify schemas files

* Move community reports extractor

* Move NLP report prompt to prompts folder

* Fix a few pandas warnings

* Rename embeddings config to embed_text

* Rename claim_extraction config to extract_claims

* Remove nltk from standard graph extraction

* Fix verb tests

* Fix extract graph config naming

* Fix moved file reference

* Create v1-to-v2 migration notebook

* Semver

* Fix smoke test artifact count

* Raise tpm/rpm on smoke tests

* Update drift settings for smoke tests

* Reuse project directory var in api notebook

* Format

* Format
2025-02-07 11:11:03 -08:00

89 lines
3.3 KiB
Python

# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License
from graphrag.callbacks.noop_workflow_callbacks import NoopWorkflowCallbacks
from graphrag.config.create_graphrag_config import create_graphrag_config
from graphrag.config.enums import LLMType
from graphrag.index.workflows.extract_graph import (
run_workflow,
)
from graphrag.utils.storage import load_table_from_storage
from .util import (
DEFAULT_MODEL_CONFIG,
create_test_context,
load_test_table,
)
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()
]
async def test_extract_graph():
nodes_expected = load_test_table("entities")
edges_expected = load_test_table("relationships")
context = await create_test_context(
storage=["text_units"],
)
config = create_graphrag_config({"models": DEFAULT_MODEL_CONFIG})
extract_claims_llm_settings = config.get_language_model_config(
config.extract_graph.model_id
).model_dump()
extract_claims_llm_settings["type"] = LLMType.StaticResponse
extract_claims_llm_settings["responses"] = MOCK_LLM_ENTITY_RESPONSES
config.extract_graph.strategy = {
"type": "graph_intelligence",
"llm": extract_claims_llm_settings,
}
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
config.summarize_descriptions.strategy = {
"type": "graph_intelligence",
"llm": summarize_llm_settings,
}
await run_workflow(
config,
context,
NoopWorkflowCallbacks(),
)
nodes_actual = await load_table_from_storage("entities", context.storage)
edges_actual = await load_table_from_storage("relationships", context.storage)
assert len(nodes_actual.columns) == len(nodes_expected.columns), (
"Nodes dataframe columns differ"
)
assert len(edges_actual.columns) == len(edges_expected.columns), (
"Edges dataframe columns differ"
)
# 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
assert nodes_actual["description"].to_numpy()[0] == "Company_A is a test company"