Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't init tokenizer when import TextSplitter #321

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions adalflow/adalflow/components/data_process/text_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
DEFAULT_CHUNK_SIZE = 800
DEFAULT_CHUNK_OVERLAP = 200

tokenizer = Tokenizer()


class TextSplitter(Component):
Expand Down Expand Up @@ -156,6 +155,8 @@ class TextSplitter(Component):
# Document(id=e7b617b2-3927-4248-afce-ec0fc247ac8b, text='to illustrate.', meta_data=None, vector=[], parent_doc_id=doc1, order=2, score=None)
"""

tokenizer = Tokenizer()

def __init__(
self,
split_by: Literal["word", "sentence", "page", "passage", "token"] = "word",
Expand Down Expand Up @@ -301,7 +302,7 @@ def call(self, documents: DocumentSplitterInputType) -> DocumentSplitterOutputTy
def _split_text_into_units(self, text: str, separator: str) -> List[str]:
"""Split text based on the specified separator."""
if self.split_by == "token":
splits = tokenizer.encode(text)
splits = TextSplitter.tokenizer.encode(text)
else:
splits = text.split(separator)
log.info(f"Text split by '{separator}' into {len(splits)} parts.")
Expand Down Expand Up @@ -344,7 +345,7 @@ def _merge_units_to_chunks(

if self.split_by == "token":
# decode each chunk here
chunks = [tokenizer.decode(chunk) for chunk in chunks]
chunks = [TextSplitter.tokenizer.decode(chunk) for chunk in chunks]

log.info(f"Merged into {len(chunks)} chunks.")
return chunks
Expand Down
Loading