2023-03-27 02:49:46 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "c350765d",
"metadata": {},
"source": [
2023-05-18 04:33:34 +00:00
"# Python Code\n",
2023-03-27 02:49:46 +00:00
"\n",
2023-05-18 04:33:34 +00:00
"`PythonCodeTextSplitter` splits text along python class and method definitions. It's implemented as a simple subclass of `RecursiveCharacterSplitter` with Python-specific separators. See the source code to see the Python syntax expected by default.\n",
2023-03-27 02:49:46 +00:00
"\n",
2023-05-18 04:33:34 +00:00
"1. How the text is split: by list of python specific separators\n",
"2. How the chunk size is measured: by number of characters"
2023-03-27 02:49:46 +00:00
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1703463f",
2023-05-18 04:33:34 +00:00
"metadata": {
"tags": []
},
2023-03-27 02:49:46 +00:00
"outputs": [],
"source": [
"from langchain.text_splitter import PythonCodeTextSplitter"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f17a1854",
2023-05-18 04:33:34 +00:00
"metadata": {
"tags": []
},
2023-03-27 02:49:46 +00:00
"outputs": [],
"source": [
"python_text = \"\"\"\n",
"class Foo:\n",
"\n",
" def bar():\n",
" \n",
" \n",
"def foo():\n",
"\n",
2023-05-29 23:56:31 +00:00
"def testing_func_with_long_name():\n",
2023-03-27 02:49:46 +00:00
"\n",
"def bar():\n",
"\"\"\"\n",
2023-05-29 23:56:31 +00:00
"python_splitter = PythonCodeTextSplitter(chunk_size=40, chunk_overlap=0)"
2023-03-27 02:49:46 +00:00
]
},
{
"cell_type": "code",
"execution_count": 3,
2023-05-29 23:56:31 +00:00
"id": "8cc33770",
2023-03-27 02:49:46 +00:00
"metadata": {},
"outputs": [],
"source": [
"docs = python_splitter.create_documents([python_text])"
]
},
{
"cell_type": "code",
"execution_count": 4,
2023-05-29 23:56:31 +00:00
"id": "f5f70775",
2023-03-27 02:49:46 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2023-05-29 23:56:31 +00:00
"[Document(page_content='class Foo:\\n\\n def bar():', metadata={}),\n",
" Document(page_content='def foo():', metadata={}),\n",
" Document(page_content='def testing_func_with_long_name():', metadata={}),\n",
" Document(page_content='def bar():', metadata={})]"
2023-03-27 02:49:46 +00:00
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"docs"
]
2023-05-18 04:33:34 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2023-05-29 23:56:31 +00:00
"id": "6e096d42",
2023-05-18 04:33:34 +00:00
"metadata": {},
"outputs": [],
"source": []
2023-03-27 02:49:46 +00:00
}
],
"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",
2023-05-29 23:56:31 +00:00
"version": "3.9.1"
2023-03-27 02:49:46 +00:00
},
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}