-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collapse create final documents (#1217)
* Collapse create_final_documents * Semver
- Loading branch information
Showing
6 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "patch", | ||
"description": "Collapse create-final-documents." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
graphrag/index/workflows/v1/subflows/create_final_documents.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright (c) 2024 Microsoft Corporation. | ||
# Licensed under the MIT License | ||
|
||
"""All the steps to transform final documents.""" | ||
|
||
from typing import cast | ||
|
||
import pandas as pd | ||
from datashaper import ( | ||
Table, | ||
VerbCallbacks, | ||
VerbInput, | ||
verb, | ||
) | ||
from datashaper.table_store.types import VerbResult, create_verb_result | ||
|
||
from graphrag.index.cache import PipelineCache | ||
from graphrag.index.verbs.text.embed.text_embed import text_embed_df | ||
|
||
|
||
@verb( | ||
name="create_final_documents", | ||
treats_input_tables_as_immutable=True, | ||
) | ||
async def create_final_documents( | ||
input: VerbInput, | ||
callbacks: VerbCallbacks, | ||
cache: PipelineCache, | ||
text_embed: dict, | ||
skip_embedding: bool = False, | ||
**_kwargs: dict, | ||
) -> VerbResult: | ||
"""All the steps to transform final documents.""" | ||
source = cast(pd.DataFrame, input.get_input()) | ||
|
||
source.rename(columns={"text_units": "text_unit_ids"}, inplace=True) | ||
|
||
if not skip_embedding: | ||
source = await text_embed_df( | ||
source, | ||
callbacks, | ||
cache, | ||
column="raw_content", | ||
strategy=text_embed["strategy"], | ||
to="raw_content_embedding", | ||
) | ||
|
||
return create_verb_result(cast(Table, source)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright (c) 2024 Microsoft Corporation. | ||
# Licensed under the MIT License | ||
|
||
from graphrag.index.workflows.v1.create_final_documents import ( | ||
build_steps, | ||
workflow_name, | ||
) | ||
|
||
from .util import ( | ||
compare_outputs, | ||
get_config_for_workflow, | ||
get_workflow_output, | ||
load_expected, | ||
load_input_tables, | ||
remove_disabled_steps, | ||
) | ||
|
||
|
||
async def test_create_final_documents(): | ||
input_tables = load_input_tables([ | ||
"workflow:create_base_documents", | ||
]) | ||
expected = load_expected(workflow_name) | ||
|
||
config = get_config_for_workflow(workflow_name) | ||
|
||
config["skip_raw_content_embedding"] = True | ||
|
||
steps = remove_disabled_steps(build_steps(config)) | ||
|
||
actual = await get_workflow_output( | ||
input_tables, | ||
{ | ||
"steps": steps, | ||
}, | ||
) | ||
|
||
compare_outputs(actual, expected) | ||
|
||
|
||
async def test_create_final_documents_with_embeddings(): | ||
input_tables = load_input_tables([ | ||
"workflow:create_base_documents", | ||
]) | ||
expected = load_expected(workflow_name) | ||
|
||
config = get_config_for_workflow(workflow_name) | ||
|
||
config["skip_raw_content_embedding"] = False | ||
# default config has a detailed standard embed config | ||
# just override the strategy to mock so the rest of the required parameters are in place | ||
config["document_raw_content_embed"]["strategy"]["type"] = "mock" | ||
|
||
steps = remove_disabled_steps(build_steps(config)) | ||
|
||
actual = await get_workflow_output( | ||
input_tables, | ||
{ | ||
"steps": steps, | ||
}, | ||
) | ||
|
||
assert "raw_content_embedding" in actual.columns | ||
assert len(actual.columns) == len(expected.columns) + 1 | ||
# the mock impl returns an array of 3 floats for each embedding | ||
assert len(actual["raw_content_embedding"][0]) == 3 |