forked from neo4j/neo4j-graphrag-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a fixed size text splitter component (neo4j#139)
* Added fixed size text splitter class * Updated docs * Updated examples * Added init defaults to fixed size text splitter * Fixed bug in example * Updated E2E tests * Update docs/source/api.rst Co-authored-by: willtai <[email protected]> * Updated fixed size splitter defaults --------- Co-authored-by: willtai <[email protected]>
- Loading branch information
1 parent
fc7d319
commit 6851a0b
Showing
9 changed files
with
173 additions
and
24 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
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
65 changes: 65 additions & 0 deletions
65
src/neo4j_graphrag/experimental/components/text_splitters/fixed_size_splitter.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,65 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [https://neo4j.com] | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# # | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from pydantic import validate_call | ||
|
||
from neo4j_graphrag.experimental.components.text_splitters.base import TextSplitter | ||
from neo4j_graphrag.experimental.components.types import TextChunk, TextChunks | ||
|
||
|
||
class FixedSizeSplitter(TextSplitter): | ||
"""Text splitter which splits the input text into fixed size chunks with optional overlap. | ||
Args: | ||
chunk_size (int): The number of characters in each chunk. | ||
chunk_overlap (int): The number of characters from the previous chunk to overlap with each chunk. Must be less than `chunk_size`. | ||
Example: | ||
.. code-block:: python | ||
from neo4j_graphrag.experimental.components.text_splitters.fixed_size_splitter import FixedSizeSplitter | ||
from neo4j_graphrag.experimental.pipeline import Pipeline | ||
pipeline = Pipeline() | ||
text_splitter = FixedSizeSplitter(chunk_size=4000, chunk_overlap=200) | ||
pipeline.add_component(text_splitter, "text_splitter") | ||
""" | ||
|
||
@validate_call | ||
def __init__(self, chunk_size: int = 4000, chunk_overlap: int = 200) -> None: | ||
if chunk_overlap >= chunk_size: | ||
raise ValueError("chunk_overlap must be strictly less than chunk_size") | ||
self.chunk_size = chunk_size | ||
self.chunk_overlap = chunk_overlap | ||
|
||
@validate_call | ||
async def run(self, text: str) -> TextChunks: | ||
"""Splits a piece of text into chunks. | ||
Args: | ||
text (str): The text to be split. | ||
Returns: | ||
TextChunks: A list of chunks. | ||
""" | ||
chunks = [] | ||
index = 0 | ||
for i in range(0, len(text), self.chunk_size - self.chunk_overlap): | ||
start = i | ||
end = min(start + self.chunk_size, len(text)) | ||
chunk_text = text[start:end] | ||
chunks.append(TextChunk(text=chunk_text, index=index)) | ||
index += 1 | ||
return TextChunks(chunks=chunks) |
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
69 changes: 69 additions & 0 deletions
69
tests/unit/experimental/components/text_splitters/test_fixed_size_splitter.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,69 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [https://neo4j.com] | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# # | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pytest | ||
from neo4j_graphrag.experimental.components.text_splitters.fixed_size_splitter import ( | ||
FixedSizeSplitter, | ||
) | ||
from neo4j_graphrag.experimental.components.types import TextChunk | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_split_text_no_overlap() -> None: | ||
text = "may thy knife chip and shatter" | ||
chunk_size = 5 | ||
chunk_overlap = 0 | ||
splitter = FixedSizeSplitter(chunk_size, chunk_overlap) | ||
chunks = await splitter.run(text) | ||
expected_chunks = [ | ||
TextChunk(text="may t", index=0), | ||
TextChunk(text="hy kn", index=1), | ||
TextChunk(text="ife c", index=2), | ||
TextChunk(text="hip a", index=3), | ||
TextChunk(text="nd sh", index=4), | ||
TextChunk(text="atter", index=5), | ||
] | ||
assert chunks.chunks == expected_chunks | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_split_text_with_overlap() -> None: | ||
text = "may thy knife chip and shatter" | ||
chunk_size = 10 | ||
chunk_overlap = 2 | ||
splitter = FixedSizeSplitter(chunk_size, chunk_overlap) | ||
chunks = await splitter.run(text) | ||
expected_chunks = [ | ||
TextChunk(text="may thy kn", index=0), | ||
TextChunk(text="knife chip", index=1), | ||
TextChunk(text="ip and sha", index=2), | ||
TextChunk(text="hatter", index=3), | ||
] | ||
assert chunks.chunks == expected_chunks | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_split_text_empty_string() -> None: | ||
text = "" | ||
chunk_size = 5 | ||
chunk_overlap = 1 | ||
splitter = FixedSizeSplitter(chunk_size, chunk_overlap) | ||
chunks = await splitter.run(text) | ||
assert chunks.chunks == [] | ||
|
||
|
||
def test_invalid_chunk_overlap() -> None: | ||
with pytest.raises(ValueError) as excinfo: | ||
FixedSizeSplitter(5, 5) | ||
assert "chunk_overlap must be strictly less than chunk_size" in str(excinfo) |