-
Notifications
You must be signed in to change notification settings - Fork 16.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: repeated calls to split() will clear chunks before splitting
chore: documentation ipynb
- Loading branch information
1 parent
2cbf2b2
commit 4df70ed
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
docs/docs/modules/data_connection/document_transformers/recursive_json_text_splitter.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a678d550", | ||
"metadata": {}, | ||
"source": [ | ||
"# Recursively split JSON\n", | ||
"\n", | ||
"This text splitter traverses json data depth first and builds smaller json chunks. It attempts to keep nested json objects whole but will split them if needed to keep chunks between a min_chunk_size and the max_chunk_size. If the value is not a nested json, but rather a very large string the string will not be split. There is an optional pre-processing step to split lists, by first converting them to json (dict) and then splitting them as such.\n", | ||
"\n", | ||
"1. How the text is split: json value.\n", | ||
"2. How the chunk size is measured: by number of characters." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a504e1e7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import json\n", | ||
"import requests " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "3390ae1d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# This is a large nested json object and will be loaded as a python dict\n", | ||
"json_data = requests.get(\"https://api.smith.langchain.com/openapi.json\").json()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7bfe2c1e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain.text_splitter import RecursiveJsonTextSplitter" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2833c409", | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"json_splitter = RecursiveJsonTextSplitter(max_chunk_size=300)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f941aa56", | ||
"metadata": { | ||
"scrolled": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Recursively split json data\n", | ||
"json_splitter.split(json_data=json_data)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0839f4f0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# The splitter now has json chunks that can be accessed as a list of Documents\n", | ||
"docs = json_splitter.to_documents()\n", | ||
"\n", | ||
"# or strings\n", | ||
"texts = json_splitter.to_string()\n", | ||
"\n", | ||
"print(texts[0])\n", | ||
"print(texts[1])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c34b1f7f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Let's look at the size of the chunks\n", | ||
"print([len(text) for text in texts][:10])\n", | ||
"\n", | ||
"# Reviewing one of these chunks that was bigger we see there is a list object there\n", | ||
"print(texts[1])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "992477c2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# The json splitter by default does not split lists\n", | ||
"# the following will preprocess the json and convert list to dict with index:item key:val pairs\n", | ||
"json_splitter.split(json_data=json_data, convert_lists=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2d23b3aa", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# let's see the strings\n", | ||
"texts = json_splitter.to_string()\n", | ||
"\n", | ||
"# Let's look at the size of the chunks. Now they are all under the max\n", | ||
"print([len(text) for text in texts][:10])\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d2c2773e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# The list has been converted to a dict, but retains all the needed contextual information even if split into many chunks\n", | ||
"print(texts[1])" | ||
] | ||
} | ||
], | ||
"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.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters