mirror of
https://github.com/hwchase17/langchain
synced 2024-10-29 17:07:25 +00:00
9d658aaa5a
As the title says, I added more code splitters. The implementation is trivial, so i don't add separate tests for each splitter. Let me know if any concerns. Fixes # (issue) https://github.com/hwchase17/langchain/issues/5170 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @eyurtsev @hwchase17 --------- Signed-off-by: byhsu <byhsu@linkedin.com> Co-authored-by: byhsu <byhsu@linkedin.com>
159 lines
3.7 KiB
Plaintext
159 lines
3.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# CodeTextSplitter\n",
|
|
"\n",
|
|
"CodeTextSplitter allows you to split your code with multiple language support. Import enum `Language` and specify the language. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.text_splitter import (\n",
|
|
" CodeTextSplitter,\n",
|
|
" Language,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Choose a language to use"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"python_splitter = CodeTextSplitter(\n",
|
|
" language=Language.PYTHON, chunk_size=16, chunk_overlap=0\n",
|
|
")\n",
|
|
"js_splitter = CodeTextSplitter(\n",
|
|
" language=Language.JS, chunk_size=16, chunk_overlap=0\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Split the code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Document(page_content='def', metadata={}),\n",
|
|
" Document(page_content='hello_world():', metadata={}),\n",
|
|
" Document(page_content='print(\"Hello,', metadata={}),\n",
|
|
" Document(page_content='World!\")', metadata={}),\n",
|
|
" Document(page_content='# Call the', metadata={}),\n",
|
|
" Document(page_content='function', metadata={}),\n",
|
|
" Document(page_content='hello_world()', metadata={})]"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"PYTHON_CODE = \"\"\"\n",
|
|
"def hello_world():\n",
|
|
" print(\"Hello, World!\")\n",
|
|
"\n",
|
|
"# Call the function\n",
|
|
"hello_world()\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
"python_docs = python_splitter.create_documents([PYTHON_CODE])\n",
|
|
"python_docs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Document(page_content='function', metadata={}),\n",
|
|
" Document(page_content='helloWorld() {', metadata={}),\n",
|
|
" Document(page_content='console.log(\"He', metadata={}),\n",
|
|
" Document(page_content='llo,', metadata={}),\n",
|
|
" Document(page_content='World!\");', metadata={}),\n",
|
|
" Document(page_content='}', metadata={}),\n",
|
|
" Document(page_content='// Call the', metadata={}),\n",
|
|
" Document(page_content='function', metadata={}),\n",
|
|
" Document(page_content='helloWorld();', metadata={})]"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"JS_CODE = \"\"\"\n",
|
|
"function helloWorld() {\n",
|
|
" console.log(\"Hello, World!\");\n",
|
|
"}\n",
|
|
"\n",
|
|
"// Call the function\n",
|
|
"helloWorld();\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
"js_docs = js_splitter.create_documents([JS_CODE])\n",
|
|
"js_docs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "langchain",
|
|
"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.12"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|