~/tools / Embeddings and search (RAG) / RAG Text Chunker
RAG Text Chunker — Split text into overlapping chunks for RAG and build an Ollama /api/embed request
Split a long document into chunks with a maximum length and an overlap before you embed it for RAG or semantic search. The text is cut at paragraphs first, then lines, sentences and spaces, and only cut inside a word when nothing else fits. You get the chunks and a ready-to-run curl command for Ollama's /api/embed with every chunk in one request. Nothing leaves your browser.
Lengths are in characters. The model's context window is in tokens, and how many tokens a text uses depends on the model, so leave a margin.
curl command
Run it on a machine that can reach Ollama. The response has one vector per chunk, in this order.
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
}'Chunks
3 chunks. The longest has 294 characters.
#0 · 294 characters · starts at offset 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 characters · starts at offset 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 characters · starts at offset 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.
JSON body for /api/embed
{
"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
}Request fields as given in the Ollama API reference for /api/embed, checked on 2026-11-04. Ollama API: Generate embeddings
Everything is done in your browser. The tool only writes the command and sends nothing.
Ollama's /api/embed endpoint takes a single string or an array of strings, and the API reference says that by default input longer than the model's context window is truncated. A long document sent as one string can therefore lose its end without any error, and a single vector for a whole document makes it hard for a search to return the part that answers a question. The usual fix is to split the document into smaller chunks and embed each one. This tool splits text at the largest boundary that fits: blank lines between paragraphs first, then line breaks, then the ends of sentences (including 。!?), then spaces, and single characters only as a last resort. It keeps the separators, so the chunks put back together give the original text. An overlap repeats the end of one chunk at the start of the next, in whole pieces, so that a sentence cut at a border still appears whole in one chunk. Lengths are counted in characters. The result is an /api/embed request with all chunks in the input array, with truncate set to false if you want an error instead of silent truncation. The splitting was checked with hand-counted examples and by confirming that every chunk is a piece of the original text at the position shown.
The core logic of this tool is open source under the MIT License. github.com/seigo5555/toolkitbox-lib/src/text-chunk.ts
Use Cases
- Prepare a manual, an FAQ or a README for a RAG search on your own Ollama server, and embed all the chunks in one request
- Find a chunk size that keeps paragraphs together, by changing the size and watching where the borders fall
- Make sure nothing is cut off silently: turn on truncate false, and Ollama returns an error when a chunk is too long for the model
Usage
- Paste the text. Separate paragraphs with a blank line so that they are kept together when possible.
- Set the maximum chunk length and the overlap in characters, the embedding model name, and the address of your Ollama server if it is not http://localhost:11434.
- Copy the curl command and run it on a machine that can reach Ollama. The response contains one vector per chunk, in the same order. The Embedding Similarity Calculator on this site can compare them.
FAQ
What chunk size should I use?
There is no single right size. Each chunk must fit in the model's context window, which is counted in tokens, and smaller chunks let a search point to a more specific passage. Start with a size that keeps one or two paragraphs together, then compare search results with the Embedding Similarity Calculator.
Why characters and not tokens?
Each embedding model has its own tokenizer, so the same text is a different number of tokens in different models, and a browser tool cannot count them exactly for every model. The Ollama library lists each model's context window in tokens; for example, it shows 2K for embeddinggemma and nomic-embed-text and 512 for mxbai-embed-large and all-minilm. Leave a margin, and use truncate false to find chunks that are too long.
What does truncate false do?
The Ollama API reference says truncate defaults to true, which truncates inputs that exceed the context window. With false, Ollama returns an error instead. The tool adds "truncate": false when the box is checked, so that a chunk that is too long fails loudly instead of being shortened.
How does the overlap work?
When a new chunk starts, the last pieces of the previous chunk (sentences, lines or words) are repeated at its start, as long as they fit within the overlap length and the chunk stays within the maximum. Pieces are never cut to fill the overlap, so the actual overlap can be shorter than the number you set.
Does it understand Markdown, HTML or code?
No. It treats everything as plain text and splits at paragraphs, lines, sentences and spaces. Remove markup you do not want in the embeddings before you paste the text.
Is my text sent anywhere?
No. The text is split in your browser, and the tool only writes the command. Nothing is sent until you run it yourself.
Related articles
- Embeddings with Ollama: the basics for search and RAGMake embeddings with Ollama's /api/embed: the models and their sizes, truncate and dimensions, unit-length vectors and cosine similarity, from the official docs.
- Store and search Ollama embeddings in PostgreSQL with pgvectorKeep Ollama embeddings in PostgreSQL with pgvector, search them with HNSW or IVFFlat, and pass the results to /api/chat so that the answer cites its sources.