-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 text units (#1178)
* Collapse non-attribute verbs * Include document_column_attributes in collapse * Remove merge_override verb * Semver * Setup initial test and config * Collapse create_base_text_units * Semver * Spelling * Fix smoke tests * Addres PR comments --------- Co-authored-by: Alonso Guevara <[email protected]>
- Loading branch information
1 parent
be7d3eb
commit 1755afb
Showing
11 changed files
with
180 additions
and
96 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_text_units." | ||
} |
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 |
---|---|---|
|
@@ -100,6 +100,7 @@ aembed | |
dedupe | ||
dropna | ||
dtypes | ||
notna | ||
|
||
# LLM Terms | ||
AOAI | ||
|
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 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
86 changes: 86 additions & 0 deletions
86
graphrag/index/workflows/v1/subflows/create_base_text_units.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,86 @@ | ||
# Copyright (c) 2024 Microsoft Corporation. | ||
# Licensed under the MIT License | ||
|
||
"""All the steps to transform base text_units.""" | ||
|
||
from typing import Any, 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.verbs.genid import genid_df | ||
from graphrag.index.verbs.overrides.aggregate import aggregate_df | ||
from graphrag.index.verbs.text.chunk.text_chunk import chunk_df | ||
|
||
|
||
@verb(name="create_base_text_units", treats_input_tables_as_immutable=True) | ||
def create_base_text_units( | ||
input: VerbInput, | ||
callbacks: VerbCallbacks, | ||
chunk_column_name: str, | ||
n_tokens_column_name: str, | ||
chunk_by_columns: list[str], | ||
strategy: dict[str, Any] | None = None, | ||
**_kwargs: dict, | ||
) -> VerbResult: | ||
"""All the steps to transform base text_units.""" | ||
table = cast(pd.DataFrame, input.get_input()) | ||
|
||
sort = table.sort_values(by=["id"], ascending=[True]) | ||
|
||
sort["text_with_ids"] = list( | ||
zip(*[sort[col] for col in ["id", "text"]], strict=True) | ||
) | ||
|
||
aggregated = aggregate_df( | ||
sort, | ||
groupby=[*chunk_by_columns] if len(chunk_by_columns) > 0 else None, | ||
aggregations=[ | ||
{ | ||
"column": "text_with_ids", | ||
"operation": "array_agg", | ||
"to": "texts", | ||
} | ||
], | ||
) | ||
|
||
chunked = chunk_df( | ||
aggregated, | ||
column="texts", | ||
to="chunks", | ||
callbacks=callbacks, | ||
strategy=strategy, | ||
) | ||
|
||
chunked = cast(pd.DataFrame, chunked[[*chunk_by_columns, "chunks"]]) | ||
chunked = chunked.explode("chunks") | ||
chunked.rename( | ||
columns={ | ||
"chunks": chunk_column_name, | ||
}, | ||
inplace=True, | ||
) | ||
|
||
chunked = genid_df( | ||
chunked, to="chunk_id", method="md5_hash", hash=[chunk_column_name] | ||
) | ||
|
||
chunked[["document_ids", chunk_column_name, n_tokens_column_name]] = pd.DataFrame( | ||
chunked[chunk_column_name].tolist(), index=chunked.index | ||
) | ||
chunked["id"] = chunked["chunk_id"] | ||
|
||
filtered = chunked[chunked[chunk_column_name].notna()].reset_index(drop=True) | ||
|
||
return create_verb_result( | ||
cast( | ||
Table, | ||
filtered, | ||
) | ||
) |
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 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,35 @@ | ||
# Copyright (c) 2024 Microsoft Corporation. | ||
# Licensed under the MIT License | ||
|
||
from graphrag.index.workflows.v1.create_base_text_units 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_text_units(): | ||
input_tables = load_input_tables(inputs=[]) | ||
expected = load_expected(workflow_name) | ||
|
||
config = get_config_for_workflow(workflow_name) | ||
# test data was created with 4o, so we need to match the encoding for chunks to be identical | ||
config["text_chunk"]["strategy"]["encoding_name"] = "o200k_base" | ||
|
||
steps = build_steps(config) | ||
|
||
actual = await get_workflow_output( | ||
input_tables, | ||
{ | ||
"steps": steps, | ||
}, | ||
) | ||
|
||
compare_outputs(actual, expected) |
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