2023-02-18 22:25:37 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "53049ff5",
|
2023-02-18 22:25:37 +00:00
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"# TiktokenText Splitter\n",
|
|
|
|
"\n",
|
|
|
|
"1. How the text is split: by `tiktoken` tokens\n",
|
|
|
|
"2. How the chunk size is measured: by `tiktoken` tokens"
|
2023-02-18 22:25:37 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-03-27 02:49:46 +00:00
|
|
|
"execution_count": 3,
|
|
|
|
"id": "8c73186a",
|
2023-02-18 22:25:37 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"# This is a long document we can split up.\n",
|
|
|
|
"with open('../../../state_of_the_union.txt') as f:\n",
|
|
|
|
" state_of_the_union = f.read()"
|
2023-02-18 22:25:37 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-03-27 02:49:46 +00:00
|
|
|
"execution_count": 4,
|
|
|
|
"id": "a1a118b1",
|
2023-02-18 22:25:37 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"from langchain.text_splitter import TokenTextSplitter"
|
2023-02-18 22:25:37 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-03-27 02:49:46 +00:00
|
|
|
"execution_count": 5,
|
|
|
|
"id": "ef37c5d3",
|
2023-02-18 22:25:37 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"text_splitter = TokenTextSplitter(chunk_size=10, chunk_overlap=0)"
|
2023-02-18 22:25:37 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-03-27 02:49:46 +00:00
|
|
|
"execution_count": 6,
|
|
|
|
"id": "5750228a",
|
|
|
|
"metadata": {
|
|
|
|
"scrolled": false
|
|
|
|
},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"Madam Speaker, Madam Vice President, our\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2023-02-18 22:25:37 +00:00
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"texts = text_splitter.split_text(state_of_the_union)\n",
|
|
|
|
"print(texts[0])"
|
2023-02-18 22:25:37 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "9a87dc30",
|
2023-02-18 22:25:37 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": []
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "Python 3 (ipykernel)",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
|
|
|
"version": "3.9.1"
|
2023-03-27 02:49:46 +00:00
|
|
|
},
|
|
|
|
"vscode": {
|
|
|
|
"interpreter": {
|
|
|
|
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
|
|
|
|
}
|
2023-02-18 22:25:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 5
|
|
|
|
}
|