-
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 base documents (#1176)
* Collapse non-attribute verbs * Include document_column_attributes in collapse * Remove merge_override verb * Semver * Clean up some df/tests
- Loading branch information
Showing
11 changed files
with
178 additions
and
169 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_base_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
This file was deleted.
Oops, something went wrong.
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
84 changes: 84 additions & 0 deletions
84
graphrag/index/workflows/v1/subflows/create_base_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,84 @@ | ||
# Copyright (c) 2024 Microsoft Corporation. | ||
# Licensed under the MIT License | ||
|
||
"""All the steps to transform base documents.""" | ||
|
||
from typing import cast | ||
|
||
import pandas as pd | ||
from datashaper import ( | ||
Table, | ||
VerbInput, | ||
verb, | ||
) | ||
from datashaper.table_store.types import VerbResult, create_verb_result | ||
|
||
from graphrag.index.verbs.overrides.aggregate import aggregate_df | ||
|
||
|
||
@verb(name="create_base_documents", treats_input_tables_as_immutable=True) | ||
def create_base_documents( | ||
input: VerbInput, | ||
document_attribute_columns: list[str] | None = None, | ||
**_kwargs: dict, | ||
) -> VerbResult: | ||
"""All the steps to transform base documents.""" | ||
source = cast(pd.DataFrame, input.get_input()) | ||
text_units = cast(pd.DataFrame, input.get_others()[0]) | ||
|
||
text_units = cast( | ||
pd.DataFrame, text_units.explode("document_ids")[["id", "document_ids", "text"]] | ||
) | ||
text_units.rename( | ||
columns={ | ||
"document_ids": "chunk_doc_id", | ||
"id": "chunk_id", | ||
"text": "chunk_text", | ||
}, | ||
inplace=True, | ||
) | ||
|
||
joined = text_units.merge( | ||
source, | ||
left_on="chunk_doc_id", | ||
right_on="id", | ||
how="inner", | ||
) | ||
|
||
docs_with_text_units = aggregate_df( | ||
joined, | ||
groupby=["id"], | ||
aggregations=[ | ||
{ | ||
"column": "chunk_id", | ||
"operation": "array_agg", | ||
"to": "text_units", | ||
} | ||
], | ||
) | ||
|
||
rejoined = docs_with_text_units.merge( | ||
source, | ||
on="id", | ||
how="right", | ||
) | ||
rejoined.rename(columns={"text": "raw_content"}, inplace=True) | ||
rejoined["id"] = rejoined["id"].astype(str) | ||
|
||
# attribute columns are converted to strings and then collapsed into a single json object | ||
if document_attribute_columns: | ||
for column in document_attribute_columns: | ||
rejoined[column] = rejoined[column].astype(str) | ||
rejoined["attributes"] = rejoined[document_attribute_columns].apply( | ||
lambda row: {**row}, | ||
axis=1, | ||
) | ||
rejoined.drop(columns=document_attribute_columns, inplace=True) | ||
rejoined.reset_index() | ||
|
||
return create_verb_result( | ||
cast( | ||
Table, | ||
rejoined, | ||
) | ||
) |
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 |
---|---|---|
|
@@ -113,7 +113,7 @@ | |
1, | ||
2000 | ||
], | ||
"subworkflows": 8, | ||
"subworkflows": 1, | ||
"max_runtime": 10 | ||
}, | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,7 +132,7 @@ | |
1, | ||
2000 | ||
], | ||
"subworkflows": 8, | ||
"subworkflows": 1, | ||
"max_runtime": 10 | ||
}, | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright (c) 2024 Microsoft Corporation. | ||
# Licensed under the MIT License | ||
|
||
from graphrag.index.workflows.v1.create_base_documents import ( | ||
build_steps, | ||
workflow_name, | ||
) | ||
|
||
from .util import ( | ||
compare_outputs, | ||
get_config_for_workflow, | ||
get_workflow_output, | ||
load_expected, | ||
load_input_tables, | ||
) | ||
|
||
|
||
async def test_create_base_documents(): | ||
input_tables = load_input_tables(["workflow:create_final_text_units"]) | ||
expected = load_expected(workflow_name) | ||
|
||
config = get_config_for_workflow(workflow_name) | ||
|
||
steps = build_steps(config) | ||
|
||
actual = await get_workflow_output( | ||
input_tables, | ||
{ | ||
"steps": steps, | ||
}, | ||
) | ||
|
||
compare_outputs(actual, expected) | ||
|
||
|
||
async def test_create_base_documents_with_attribute_columns(): | ||
input_tables = load_input_tables(["workflow:create_final_text_units"]) | ||
expected = load_expected(workflow_name) | ||
|
||
config = get_config_for_workflow(workflow_name) | ||
|
||
config["document_attribute_columns"] = ["title"] | ||
|
||
steps = build_steps(config) | ||
|
||
actual = await get_workflow_output( | ||
input_tables, | ||
{ | ||
"steps": steps, | ||
}, | ||
) | ||
|
||
# we should have dropped "title" and added "attributes" | ||
# our test dataframe does not have attributes, so we'll assert without it | ||
# and separately confirm it is in the output | ||
compare_outputs(actual, expected, columns=["id", "text_units", "raw_content"]) | ||
assert len(actual.columns) == 4 | ||
assert "attributes" in actual.columns |
Oops, something went wrong.