トークンの分割

言語モデルにはトークンの制限があります。そのため、トークン数の制限を超えないようにしましょう。テキストをチャンクに分割する際は、トークンの数を計算するのが最良です。多くのトークン処理器(トークナイザー)が利用可能です。テキストのトークン数を計算する際は、言語モデルで使用されている同じトークン処理器を使用する必要があります。

この章では、LangChainがさまざまなトークン分割器を使用してテキストコンテンツをトークンに基づいて分割する方法を紹介します。

tiktoken

tiktokenはOpenAIによってオープンソース化された高速なBPEトークナイザーです。

これを使用して使用されたトークンの数を推定できます。OpenAIモデルに対してより正確かもしれません。

  1. テキストの分割方法:入力文字に基づいて分割します。
  2. チャンクのサイズの測定方法:tiktoken トークナイザーを使用します。
%pip install --upgrade --quiet langchain-text-splitters tiktoken
with open("../../../state_of_the_union.txt") as f:
    state_of_the_union = f.read()
	
from langchain_text_splitters import CharacterTextSplitter
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
    chunk_size=100, chunk_overlap=0
)
texts = text_splitter.split_text(state_of_the_union)
print(texts[0])
Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.  
    
    Last year COVID-19 kept us apart. This year we are finally together again. 
    
    Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. 
    
    With a duty to one another to the American people to the Constitution.

tiktoken 分割器を直接読み込むこともできます。

from langchain_text_splitters import TokenTextSplitter

text_splitter = TokenTextSplitter(chunk_size=10, chunk_overlap=0)

texts = text_splitter.split_text(state_of_the_union)
print(texts[0])

spaCy

spaCy はPythonとCythonで書かれた高度な自然言語処理向けオープンソースソフトウェアライブラリです。

NLTKの代替として、spaCyトークナイザーを使用する別の選択肢があります。

  1. テキストの分割方法:spaCy トークナイザーを使用します。
  2. チャンクのサイズの測定方法:文字数を数えます。
with open("../../../state_of_the_union.txt") as f:
    state_of_the_union = f.read()
from langchain_text_splitters import SpacyTextSplitter

text_splitter = SpacyTextSplitter(chunk_size=1000)
texts = text_splitter.split_text(state_of_the_union)
print(texts[0])

SentenceTransformers

SentenceTransformersTokenTextSplitter は、特定のsentence-transformerモデルに特化したテキスト分割器です。デフォルトでは、指定されたsentence-transformerモデルに適したトークンウィンドウサイズのブロックにテキストを分割します。

from langchain_text_splitters import SentenceTransformersTokenTextSplitter
splitter = SentenceTransformersTokenTextSplitter(chunk_overlap=0)
text = "Lorem "
count_start_and_stop_tokens = 2
text_token_count = splitter.count_tokens(text=text) - count_start_and_stop_tokens
print(text_token_count)
2
token_multiplier = splitter.maximum_tokens_per_chunk // text_token_count + 1

text_to_split = text * token_multiplier

print(f"分割するテキストのトークン数:{splitter.count_tokens(text=text_to_split)}")
分割するテキストのトークン数:514
text_chunks = splitter.split_text(text=text_to_split)

print(text_chunks[1])
lorem

NLTK(Natural Language Toolkit)

NLTK(Natural Language Toolkit)は、Pythonプログラミング言語で書かれたシンボリックおよび統計的自然言語処理(NLP)用のライブラリやプログラムのセットであり、英語用に使用されます。

単純に "\n\n" に基づいてテキストを分割する代わりに、NLTKトークナイザを使用して分割できます。

  1. テキスト分割方法:NLTKトークナイザを使用します。
  2. チャンクサイズの測定:文字数で測定します。
with open("../../../state_of_the_union.txt") as f:
    state_of_the_union = f.read()
from langchain_text_splitters import NLTKTextSplitter

text_splitter = NLTKTextSplitter(chunk_size=1000)
texts = text_splitter.split_text(state_of_the_union)
print(texts[0])

Hugging Face トークナイザ

Hugging Faceには多くのトークナイザがあります。

Hugging FaceトークナイザからGPT2TokenizerFastを使用して、テキスト中のトークン数を算出します。

  1. テキストのセグメンテーション方法:入力文字に基づいてセグメンテーションされます。
  2. チャンクサイズの算出方法:Hugging Faceトークナイザで算出されたトークン数で測定します。
from transformers import GPT2TokenizerFast

tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
with open("../../../state_of_the_union.txt") as f:
    state_of_the_union = f.read()

from langchain_text_splitters import CharacterTextSplitter
text_splitter = CharacterTextSplitter.from_huggingface_tokenizer(
    tokenizer, chunk_size=100, chunk_overlap=0
)
texts = text_splitter.split_text(state_of_the_union)
print(texts[0])