~/tools / 埋め込みと検索(RAG) / RAG Text Chunker
RAG Text Chunker — RAG用にテキストを分割し、Ollama /api/embed を作成
RAGやセマンティック検索のためにembeddingを作る前に、長い文書を最大長とオーバーラップを指定してチャンクに分割します。テキストはまず段落、次に行、文、スペースの順で区切り、ほかに区切れる場所がないときだけ単語の途中で切ります。チャンクの一覧と、すべてのチャンクを1回のリクエストに入れたOllamaの /api/embed 用のcurlコマンドがそのまま使える形で得られます。データがブラウザの外に出ることはありません。
長さは文字数です。モデルのコンテキストウィンドウはトークン数で、テキストが何トークンになるかはモデルによって変わるので、余裕を持たせてください。
curlコマンド
Ollamaに接続できるマシンで実行します。レスポンスには、チャンクごとに1つずつ、この順番でベクトルが入っています。
curl http://localhost:11434/api/embed -d '{
"model": "embeddinggemma",
"input": [
"Embeddings turn text into numeric vectors. You can store them in a vector database and search them with cosine similarity, or use them in a RAG pipeline.\n\nThe length of each vector depends on the embedding model. Use the same model when you index your documents and when you embed a question.\n\n",
"Ollama'\''s /api/embed endpoint takes a single string or an array of strings. By default, input that is longer than the model'\''s context window is truncated. Set truncate to false to get an error instead.\n\n",
"Split long documents into smaller chunks before you embed them, so that each chunk fits in the context window and a search can return the part that answers the question."
],
"truncate": false
}'チャンク
チャンクは3個。最長は294文字です。
#0 · 294文字 · 開始位置: オフセット0
Embeddings turn text into numeric vectors. You can store them in a vector database and search them with cosine similarity, or use them in a RAG pipeline. The length of each vector depends on the embedding model. Use the same model when you index your documents and when you embed a question.
#1 · 202文字 · 開始位置: オフセット294
Ollama's /api/embed endpoint takes a single string or an array of strings. By default, input that is longer than the model's context window is truncated. Set truncate to false to get an error instead.
#2 · 169文字 · 開始位置: オフセット496
Split long documents into smaller chunks before you embed them, so that each chunk fits in the context window and a search can return the part that answers the question.
/api/embed 用のJSONボディ
{
"model": "embeddinggemma",
"input": [
"Embeddings turn text into numeric vectors. You can store them in a vector database and search them with cosine similarity, or use them in a RAG pipeline.\n\nThe length of each vector depends on the embedding model. Use the same model when you index your documents and when you embed a question.\n\n",
"Ollama's /api/embed endpoint takes a single string or an array of strings. By default, input that is longer than the model's context window is truncated. Set truncate to false to get an error instead.\n\n",
"Split long documents into smaller chunks before you embed them, so that each chunk fits in the context window and a search can return the part that answers the question."
],
"truncate": false
}/api/embed のリクエストフィールドは、OllamaのAPIリファレンスの記載に従っています(2026-11-04に確認)。 Ollama API: Generate embeddings
すべてブラウザの中で行います。ツールはコマンドを書くだけで、何も送信しません。
Ollamaの /api/embed エンドポイントは、1つの文字列、または文字列の配列を受け取ります。APIリファレンスによると、デフォルトでは、モデルのコンテキストウィンドウより長い入力は切り詰められます。そのため、長い文書を1つの文字列として送ると、エラーなしで末尾が失われることがあります。また、文書全体を1つのベクトルにすると、検索で質問に答えている部分だけを返すのが難しくなります。よくある対処は、文書を小さなチャンクに分けて、チャンクごとにembeddingを作ることです。このツールは、収まる範囲でいちばん大きな区切りでテキストを分割します。まず段落の間の空行、次に改行、次に文末(。!?を含む)、次にスペースで、1文字ずつ切るのは最後の手段です。区切り文字は残すので、チャンクをつなぎ合わせると元のテキストに戻ります。オーバーラップは、あるチャンクの終わりを次のチャンクの先頭に、まとまり単位で繰り返すもので、境界で切れた文も、どちらかのチャンクに丸ごと含まれるようになります。長さは文字数で数えます。結果は、すべてのチャンクを input 配列に入れた /api/embed のリクエストで、エラーにしたい場合(黙って切り詰めるのではなく)は truncate を false にします。分割は、手で数えた例と、すべてのチャンクが表示された位置で元のテキストの一部になっていることの確認で検証しました。
このツールの計算部分は、オープンソース(MIT ライセンス)で公開しています。 github.com/seigo5555/toolkitbox-lib/src/text-chunk.ts
こんな時に使う
- マニュアル、FAQ、READMEを、自分のOllamaサーバーでのRAG検索用に準備し、すべてのチャンクを1回のリクエストでembeddingにする
- サイズを変えながらチャンクの境界がどこに来るかを見て、段落がまとまるチャンクサイズを探す
- 黙って切り捨てられないようにする: truncate false をオンにすると、チャンクがモデルに対して長すぎるときに Ollama がエラーを返す
使い方
- テキストを貼り付けます。段落は空行で区切ると、できるだけまとめて残されます。
- チャンクの最大長とオーバーラップを文字数で、embeddingモデル名を、そしてOllamaサーバーが http://localhost:11434 でない場合はそのアドレスを指定します。
- curlコマンドをコピーして、Ollamaに接続できるマシンで実行します。レスポンスには、チャンクごとに1つずつ、同じ順番でベクトルが入っています。このサイトのEmbedding Similarity Calculatorで、それらを比べられます。
FAQ
チャンクサイズはどれくらいにすればよいですか?
唯一の正解はありません。各チャンクはモデルのコンテキストウィンドウ(トークン数で数えます)に収まる必要があり、チャンクが小さいほど、検索はより具体的な文章を指せます。まず1〜2段落がまとまるサイズから始めて、Embedding Similarity Calculatorで検索結果を比べてください。
トークンではなく文字数なのはなぜですか?
embeddingモデルごとにトークナイザーが違い、同じテキストでもモデルによってトークン数が変わります。ブラウザのツールでは、すべてのモデルについて正確に数えることはできません。Ollamaライブラリには各モデルのコンテキストウィンドウがトークン数で載っています。たとえば、embeddinggemma と nomic-embed-text は2K、mxbai-embed-large と all-minilm は512です。余裕を持たせ、長すぎるチャンクは truncate false で見つけてください。
truncate false は何をしますか?
OllamaのAPIリファレンスによると、truncate のデフォルトは true で、コンテキストウィンドウを超える入力が切り詰められます。false にすると、代わりに Ollama がエラーを返します。このツールは、チェックボックスをオンにすると "truncate": false を追加するので、長すぎるチャンクは短くされずに、はっきり失敗します。
オーバーラップはどう働きますか?
新しいチャンクを始めるとき、前のチャンクの最後のまとまり(文、行、単語)が、オーバーラップの長さに収まり、かつチャンクが最大長を超えない範囲で、その先頭に繰り返されます。オーバーラップを埋めるためにまとまりを途中で切ることはないので、実際のオーバーラップは、指定した数値より短くなることがあります。
Markdown、HTML、コードは理解しますか?
いいえ。すべてをプレーンテキストとして扱い、段落、行、文、スペースで分割します。embeddingに入れたくないマークアップは、テキストを貼り付ける前に取り除いてください。
テキストはどこかに送信されますか?
いいえ。テキストはブラウザの中で分割され、ツールはコマンドを書くだけです。ご自身で実行するまで、何も送信されません。