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

Tiktoken special params #924

Merged
merged 7 commits into from
Feb 10, 2023
Merged
Changes from 5 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
41 changes: 35 additions & 6 deletions langchain/text_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@

import logging
from abc import ABC, abstractmethod
from typing import Any, Callable, Iterable, List, Optional
from typing import (
AbstractSet,
Any,
Callable,
Collection,
Iterable,
List,
Literal,
Optional,
Union,
)

from langchain.docstore.document import Document

Expand Down Expand Up @@ -114,7 +124,11 @@ def _huggingface_tokenizer_length(text: str) -> int:

@classmethod
def from_tiktoken_encoder(
cls, encoding_name: str = "gpt2", **kwargs: Any
cls,
encoding_name: str = "gpt2",
allowed_special: Union[Literal["all"], AbstractSet[str]] = set(),
disallowed_special: Union[Literal["all"], Collection[str]] = "all",
**kwargs: Any,
) -> TextSplitter:
"""Text splitter that uses tiktoken encoder to count length."""
try:
Expand All @@ -125,11 +139,19 @@ def from_tiktoken_encoder(
"This is needed in order to calculate max_tokens_for_prompt. "
"Please it install it with `pip install tiktoken`."
)

# create a GPT-3 encoder instance
enc = tiktoken.get_encoding(encoding_name)

def _tiktoken_encoder(text: str) -> int:
return len(enc.encode(text))
def _tiktoken_encoder(text: str, **kwargs: Any) -> int:
return len(
enc.encode(
text,
allowed_special=allowed_special,
disallowed_special=disallowed_special,
**kwargs,
)
)

return cls(length_function=_tiktoken_encoder, **kwargs)

Expand Down Expand Up @@ -169,10 +191,17 @@ def __init__(self, encoding_name: str = "gpt2", **kwargs: Any):
# create a GPT-3 encoder instance
self._tokenizer = tiktoken.get_encoding(encoding_name)

def split_text(self, text: str) -> List[str]:
def split_text(
self,
text: str,
allowed_special: Union[Literal["all"], AbstractSet[str]] = set(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on putting this in the init, eg self.allowed_special etc?

would be more consistent with other splitters (taking all the params at init rather than run time)

if you agree, im also happy to make this change myself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey Harrison, yes this makes sense, please feel free to make the change, thanks!

disallowed_special: Union[Literal["all"], Collection[str]] = "all",
) -> List[str]:
"""Split incoming text and return chunks."""
splits = []
input_ids = self._tokenizer.encode(text)
input_ids = self._tokenizer.encode(
text, allowed_special=allowed_special, disallowed_special=disallowed_special
)
start_idx = 0
cur_idx = min(start_idx + self._chunk_size, len(input_ids))
chunk_ids = input_ids[start_idx:cur_idx]
Expand Down