Skip to content

Commit

Permalink
docs: text splitters improvements (#4490)
Browse files Browse the repository at this point in the history
#docs: text splitters improvements

Changes are only in the Jupyter notebooks.
- added links to the source packages and a short description of these
packages
- removed " Text Splitters" suffixes from the TOC elements (they made
the list of the text splitters messy)
- moved text splitters, based on the length function into a separate
list. They can be mixed with any classes from the "Text Splitters", so
it is a different classification.

## Who can review?
        @hwchase17 - project lead
        @eyurtsev
        @vowelparrot

NOTE: please, check out the results of the `Python code` text splitter
example (text_splitters/examples/python.ipynb). It looks suboptimal.
  • Loading branch information
leo-gan authored May 18, 2023
1 parent 613bf9b commit c998569
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 73 deletions.
43 changes: 39 additions & 4 deletions docs/modules/indexes/text_splitters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,47 @@ For an introduction to the default text splitter and generic functionality see:
./text_splitters/getting_started.ipynb


We also have documentation for all the types of text splitters that are supported.
Please see below for that list.
Usage examples for the text splitters:

- `Character <./text_splitters/examples/character_text_splitter.html>`_
- `LaTeX <./text_splitters/examples/latex.html>`_
- `Markdown <./text_splitters/examples/markdown.html>`_
- `NLTK <./text_splitters/examples/nltk.html>`_
- `Python code <./text_splitters/examples/python.html>`_
- `Recursive Character <./text_splitters/examples/recursive_text_splitter.html>`_
- `spaCy <./text_splitters/examples/spacy.html>`_
- `tiktoken (OpenAI) <./text_splitters/examples/tiktoken_splitter.html>`_


.. toctree::
:maxdepth: 1
:glob:
:caption: Text Splitters
:name: text_splitters
:hidden:

./text_splitters/examples/character_text_splitter.ipynb
./text_splitters/examples/latex.ipynb
./text_splitters/examples/markdown.ipynb
./text_splitters/examples/nltk.ipynb
./text_splitters/examples/python.ipynb
./text_splitters/examples/recursive_text_splitter.ipynb
./text_splitters/examples/spacy.ipynb
./text_splitters/examples/tiktoken_splitter.ipynb


Most LLMs are constrained by the number of tokens that you can pass in, which is not the same as the number of characters.
In order to get a more accurate estimate, we can use tokenizers to count the number of tokens in the text.
We use this number inside the `..TextSplitter` classes.
This implemented as the `from_<tokenizer>` methods of the `..TextSplitter` classes:

- `Hugging Face tokenizer <./text_splitters/examples/huggingface_length_function.html>`_
- `tiktoken (OpenAI) tokenizer <./text_splitters/examples/tiktoken.html>`_

.. toctree::
:maxdepth: 1
:caption: Text Splitters with Tokens
:name: text_splitter_with_tokens
:hidden:

./text_splitters/examples/*
./text_splitters/examples/huggingface_length_function.ipynb
./text_splitters/examples/tiktoken.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
"id": "5c461b26",
"metadata": {},
"source": [
"# Character Text Splitter\n",
"# Character\n",
"\n",
"This is a more simple method. This splits based on characters (by default \"\\n\\n\") and measure chunk length by number of characters.\n",
"This is the simplest method. This splits based on characters (by default \"\\n\\n\") and measure chunk length by number of characters.\n",
"\n",
"1. How the text is split: by single character\n",
"2. How the chunk size is measured: by length function passed in (defaults to number of characters)"
"2. How the chunk size is measured: by number of characters"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9c21e679",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# This is a long document we can split up.\n",
Expand All @@ -29,7 +31,9 @@
"cell_type": "code",
"execution_count": 2,
"id": "79ff6737",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.text_splitter import CharacterTextSplitter\n",
Expand Down Expand Up @@ -87,6 +91,37 @@
"documents = text_splitter.create_documents([state_of_the_union, state_of_the_union], metadatas=metadatas)\n",
"print(documents[0])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "90ac0381-855a-469a-b8bf-e33230132bbe",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'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. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text_splitter.split_text(state_of_the_union)[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "875c20be-9f63-4aee-b05a-34e9c04c1091",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -105,7 +140,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.6"
},
"vscode": {
"interpreter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"id": "13dc0983",
"metadata": {},
"source": [
"# Hugging Face Length Function\n",
"Most LLMs are constrained by the number of tokens that you can pass in, which is not the same as the number of characters. In order to get a more accurate estimate, we can use Hugging Face tokenizers to count the text length.\n",
"# Hugging Face tokenizer\n",
"\n",
">[Hugging Face](https://huggingface.co/docs/tokenizers/index) has many tokenizers.\n",
"\n",
"We use Hugging Face tokenizer, the [GPT2TokenizerFast](https://huggingface.co/Ransaka/gpt2-tokenizer-fast) to count the text length in tokens.\n",
"\n",
"1. How the text is split: by character passed in\n",
"2. How the chunk size is measured: by Hugging Face tokenizer"
"2. How the chunk size is measured: by number of tokens calculated by the `Hugging Face` tokenizer\n"
]
},
{
Expand Down Expand Up @@ -89,7 +92,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.6"
},
"vscode": {
"interpreter": {
Expand Down
58 changes: 49 additions & 9 deletions docs/modules/indexes/text_splitters/examples/latex.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@
"id": "3a2f572e",
"metadata": {},
"source": [
"# Latex Text Splitter\n",
"# LaTeX\n",
"\n",
"LatexTextSplitter splits text along Latex headings, headlines, enumerations and more. It's implemented as a simple subclass of RecursiveCharacterSplitter with Latex-specific separators. See the source code to see the Latex syntax expected by default.\n",
">[LaTeX](https://en.wikipedia.org/wiki/LaTeX) is widely used in academia for the communication and publication of scientific documents in many fields, including mathematics, computer science, engineering, physics, chemistry, economics, linguistics, quantitative psychology, philosophy, and political science.\n",
"\n",
"1. How the text is split: by list of latex specific tags\n",
"2. How the chunk size is measured: by length function passed in (defaults to number of characters)"
"`LatexTextSplitter` splits text along `LaTeX` headings, headlines, enumerations and more. It's implemented as a subclass of `RecursiveCharacterSplitter` with LaTeX-specific separators. See the source code for more details.\n",
"\n",
"1. How the text is split: by list of `LaTeX` specific tags\n",
"2. How the chunk size is measured: by number of characters"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "c2503917",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.text_splitter import LatexTextSplitter"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "e46b753b",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"latex_text = \"\"\"\n",
Expand Down Expand Up @@ -84,6 +90,40 @@
"source": [
"docs"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "40e62829-9485-414e-9ea1-e1a8fc7c88cb",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"['\\\\documentclass{article}\\n\\n\\x08egin{document}\\n\\n\\\\maketitle',\n",
" 'Introduction}\\nLarge language models (LLMs) are a type of machine learning model that can be trained on vast amounts of text data to generate human-like language. In recent years, LLMs have made significant advances in a variety of natural language processing tasks, including language translation, text generation, and sentiment analysis.',\n",
" 'History of LLMs}\\nThe earliest LLMs were developed in the 1980s and 1990s, but they were limited by the amount of data that could be processed and the computational power available at the time. In the past decade, however, advances in hardware and software have made it possible to train LLMs on massive datasets, leading to significant improvements in performance.',\n",
" 'Applications of LLMs}\\nLLMs have many applications in industry, including chatbots, content creation, and virtual assistants. They can also be used in academia for research in linguistics, psychology, and computational linguistics.\\n\\n\\\\end{document}']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"latex_splitter.split_text(latex_text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7deb8f25-a062-4956-9f90-513802069667",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -102,7 +142,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.6"
},
"vscode": {
"interpreter": {
Expand Down
77 changes: 60 additions & 17 deletions docs/modules/indexes/text_splitters/examples/markdown.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@
"id": "80f6cd99",
"metadata": {},
"source": [
"# Markdown Text Splitter\n",
"# Markdown\n",
"\n",
"MarkdownTextSplitter splits text along Markdown headings, code blocks, or horizontal rules. It's implemented as a simple subclass of RecursiveCharacterSplitter with Markdown-specific separators. See the source code to see the Markdown syntax expected by default.\n",
">[Markdown](https://en.wikipedia.org/wiki/Markdown) is a lightweight markup language for creating formatted text using a plain-text editor.\n",
"\n",
"1. How the text is split: by list of markdown specific characters\n",
"2. How the chunk size is measured: by length function passed in (defaults to number of characters)"
"`MarkdownTextSplitter` splits text along Markdown headings, code blocks, or horizontal rules. It's implemented as a simple subclass of `RecursiveCharacterSplitter` with Markdown-specific separators. See the source code to see the Markdown syntax expected by default.\n",
"\n",
"1. How the text is split: by list of `markdown` specific separators\n",
"2. How the chunk size is measured: by number of characters"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"id": "96d64839",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.text_splitter import MarkdownTextSplitter"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"id": "cfb0da17",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"markdown_text = \"\"\"\n",
Expand All @@ -49,36 +55,73 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"id": "d59a4fe8",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"docs = markdown_splitter.create_documents([markdown_text])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"id": "cbb2e100",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[Document(page_content='# 🦜️🔗 LangChain\\n\\n⚡ Building applications with LLMs through composability ⚡', lookup_str='', metadata={}, lookup_index=0),\n",
" Document(page_content=\"Quick Install\\n\\n```bash\\n# Hopefully this code block isn't split\\npip install langchain\", lookup_str='', metadata={}, lookup_index=0),\n",
" Document(page_content='As an open source project in a rapidly developing field, we are extremely open to contributions.', lookup_str='', metadata={}, lookup_index=0)]"
"[Document(page_content='# 🦜️🔗 LangChain\\n\\n⚡ Building applications with LLMs through composability ⚡', metadata={}),\n",
" Document(page_content=\"Quick Install\\n\\n```bash\\n# Hopefully this code block isn't split\\npip install langchain\", metadata={}),\n",
" Document(page_content='As an open source project in a rapidly developing field, we are extremely open to contributions.', metadata={})]"
]
},
"execution_count": 7,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"docs"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "91b56e7e-b285-4ca4-a786-149544e0e3c6",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"['# 🦜️🔗 LangChain\\n\\n⚡ Building applications with LLMs through composability ⚡',\n",
" \"Quick Install\\n\\n```bash\\n# Hopefully this code block isn't split\\npip install langchain\",\n",
" 'As an open source project in a rapidly developing field, we are extremely open to contributions.']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"markdown_splitter.split_text(markdown_text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9bee7858-9175-4d99-bd30-68f2dece8601",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -97,7 +140,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.6"
},
"vscode": {
"interpreter": {
Expand Down
Loading

0 comments on commit c998569

Please sign in to comment.