การแบ่งโค้ด
บทนี้นำเสนอตัวตัดข้อความโค้ดของ LangChain หากคุณต้องการแบ่งโค้ดเป็นข้อมูลเล็ก ๆ คุณควรศึกษาเนื้อหาของบทนี้อย่างรอบคอบ ตัวตัดข้อความโค้ดสนับสนุนการแบ่งโค้ดสำหรับหลายภาษาโปรแกรมมิ่ง
การติดตั้ง Dependencies
%pip install -qU langchain-text-splitters
ต่อมานำ "Language" enumeration เข้ามาและดูว่ามีภาษาโปรแกรมใดที่สนับสนุนการแบ่งโค้ด
from langchain_text_splitters import (
Language,
RecursiveCharacterTextSplitter,
)
[e.value for e in Language]
['cpp',
'go',
'java',
'js',
'php',
'proto',
'python',
'rst',
'ruby',
'rust',
'scala',
'swift',
'markdown',
'latex',
'html',
'sol',]
RecursiveCharacterTextSplitter.get_separators_for_language(Language.PYTHON)
['\\nclass ', '\\ndef ', '\\n\\tdef ', '\\n\\n', '\\n', ' ', '']
Python
นี่คือตัวอย่างการใช้ PythonTextSplitter เพื่อแบ่งโค้ด
PYTHON_CODE = """
def hello_world():
print("Hello, World!")
hello_world()
"""
python_splitter = RecursiveCharacterTextSplitter.from_language(
language=Language.PYTHON, chunk_size=50, chunk_overlap=0
)
python_docs = python_splitter.create_documents([PYTHON_CODE])
python_docs
[Document(page_content='def hello_world():\\\\n print("Hello, World!")', metadata={}), Document(page_content='# Call the function\\\\nhello_world()', metadata={})]
JS
นี่คือตัวอย่างการใช้ตัวตัดข้อความ JS เพื่อแบ่งโค้ด JS
JS_CODE = """
function helloWorld() {
console.log("Hello, World!");
}
// Call the function
helloWorld();
"""
js_splitter = RecursiveCharacterTextSplitter.from_language(
language=Language.JS, chunk_size=60, chunk_overlap=0
)
js_docs = js_splitter.create_documents([JS_CODE])
js_docs
[Document(page_content='function helloWorld() {\n console.log("Hello, World!");\n}', metadata={}),
Document(page_content='// Call the function\nhelloWorld();', metadata={})]
Markdown
นี่คือตัวอย่างการแบ่งโค้ด markdown
markdown_text = """
⚡ สร้างโปรแกรมแอปพลิเคชันกับ LLM ผ่านความสามารถในการเชื่อมต่อ ⚡
\`\`\`bash
pip install langchain
\`\`\`
เป็นผลงานโอเพนซอร์สที่มีในภาควิวัฒนาอย่างรวดเร็ว เรายินดีต้อนรับความร่วมมือ
"""
md_splitter = RecursiveCharacterTextSplitter.from_language(
language=Language.MARKDOWN, chunk_size=60, chunk_overlap=0
)
md_docs = md_splitter.create_documents([markdown_text])
md_docs
[Document(page_content='# ?️? LangChain', metadata={}),
Document(page_content='⚡ สร้างโปรแกรมแอปพลิเคชันกับ LLM ผ่านความสามารถในการเชื่อมต่อ ⚡', metadata={}),
Document(page_content='## Quick Install', metadata={}),
Document(page_content="```bash\n# หวังว่าบล็อกโค้ดนี้ไม่ถูกรวมเข้าไป", metadata={}),
Document(page_content='pip install langchain', metadata={}),
Document(page_content='```', metadata={}),
Document(page_content='เป็นผลงานโอเพนซอร์สที่มีในภาควิวัฒนาอย่างรวดเร็ว เรา', metadata={}),
Document(page_content='ยินดีต้อนรับความร่วมมือ', metadata={})]
latex_text = """
\documentclass{article}
\begin{document}
\maketitle
\section{Introduction}
มโนภาษาขนาดใหญ่ (LLMs) เป็นประเภทของโมเดลเรียนรู้ของเครื่องที่สามารถฝึกฝนบนข้อมูลข้อความปริมาณมากเพื่อสร้างภาษาที่คล้ายมนุษย์ ในปีสุดท้ายมโนภาษาขนาดใหญ่ได้ก้าวหน้าอย่างมีนัยสำคัญในหลายภารกิจการประมวลผลภาษาธรรมชาติ รวมถึงการแปลภาษา การสร้างข้อความ และการวิเคราะห์อารมณ์
\subsection{ประวัติของมโนภาษาขนาดใหญ่}
มโนภาษาขนาดใหญ่เริ่มพัฒนาในปี 1980 และ 1990 แต่มีข้อจำกัดในปริมาณข้อมูลที่สามารถประมวลและพลังการคำนวณที่มีอยู่ในช่วงเวลานั้น ในทศวรรษที่ผ่านมา การพัฒนาฮาร์ดแวร์และซอฟต์แวร์ได้ทำให้เป็นไปได้ที่จะฝึกมโนภาษาขนาดใหญ่บนชุดข้อมูลขนาดใหญ่ ซึ่งทำให้มีการปรับปรุงอย่างมีนัยทางประสิทธิภาพ
\subsection{การนำมโนภาษาขนาดใหญ่ไปใช้}
มโนภาษาขนาดใหญ่มีการนำไปใช้ในอุตสาหกรรมหลายแห่ง รวมถึงแชทบอต สร้างเนื้อหา และผู้ช่วยส่วนตัวเสมือน นอกจากนี้ยังสามารถนำไปใช้ในวงการวิจัยทางภาษาวิทยา จิตวิทยา และภาษาวิทยาคอมพิวเตอร์
\end{document}
"""
HTML
html_text = """
<!DOCTYPE html>
<html>
<head>
<title>🌐 LangChain</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
color: darkblue;
}
</style>
</head>
<body>
<div>
<h1>🌐 LangChain</h1>
<p>⚡ สร้างแอปพลิเคชันด้วย LLMs ผ่านความสามารถในการผสมกัน ⚡</p>
</div>
<div>
เป็นโปรเจกต์โอเพนซอร์สในเขตกิจการที่กำลังพัฒนาอย่างรวดเร็ว ซึ่งเรายินดีต้อนรับการช่วยเหลือและการมีส่วนร่วมอย่างมาก
</div>
</body>
</html>
"""
html_splitter = RecursiveCharacterTextSplitter.from_language(
language=Language.HTML, chunk_size=60, chunk_overlap=0
)
html_docs = html_splitter.create_documents([html_text])
html_docs
[Document(page_content='<!DOCTYPE html>\n<html>', metadata={}),
Document(page_content='<head>\n <title>🌐 LangChain</title>', metadata={}),
Document(page_content='<style>\n body {\n font-family: Arial', metadata={}),
Document(page_content=', sans-serif;\n }\n h1 {', metadata={}),
Document(page_content='color: darkblue;\n }\n </style>\n </head', metadata={}),
Document(page_content='>', metadata={}),
Document(page_content='<body>', metadata={}),
Document(page_content='<div>\n <h1>🌐 LangChain</h1>', metadata={}),
Document(page_content='<p>⚡ สร้างแอปพลิเคชันด้วย LLMs ผ่านความสามารถในการผสมกัน ⚡', metadata={}),
Document(page_content='</p>\n </div>', metadata={}),
Document(page_content='<div>\n เป็นโปรเจกต์โอเพนซอร์สในเขตกิจการที่กำลังพัฒนาอย่างรวดเร็ว ', metadata={}),
Document(page_content='ซึ่งเรายินดีต้อนรับการช่วยเหลือและการมีส่วนร่วมอย่างมาก', metadata={}),
Document(page_content='</div>\n </body>\n</html>', metadata={})
]
Solidity
SOL_CODE = """
pragma solidity ^0.8.20;
contract HelloWorld {
function add(uint a, uint b) pure public returns(uint) {
return a + b;
}
}
"""
sol_splitter = RecursiveCharacterTextSplitter.from_language(
language=Language.SOL, chunk_size=128, chunk_overlap=0
)
sol_docs = sol_splitter.create_documents([SOL_CODE])
sol_docs
[
Document(page_content='pragma solidity ^0.8.20;', metadata={}),
Document(page_content='contract HelloWorld {\n function add(uint a, uint b) pure public returns(uint) {\n return a + b;\n }\n}', metadata={})
]