การแบ่งแยกตามตัวอักษร
LangChain เป็นวิธีที่ง่ายที่สุดในการแบ่งข้อความ โดยใช้อักขระ (โดยค่าเริ่มต้นคือ "\n\n") เพื่อการแบ่งและวัดขนาดของชิ้นงานโดยจำนวนของตัวอักษร
- วิธีการแบ่งข้อความ: แบ่งตามตัวอักษรแต่ละตัว
- วิธีการวัดขนาดของชิ้นงาน: วัดโดยจำนวนของตัวอักษร
การติดตั้งแพ็คเกจ
%pip install -qU langchain-text-splitters
ตัวอย่าง
with open('../../../state_of_the_union.txt') as f:
state_of_the_union = f.read()
from langchain_text_splitters import CharacterTextSplitter
text_splitter = CharacterTextSplitter(
separator="\n\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len,
)
texts = text_splitter.create_documents([state_of_the_union])
print(texts[0])
page_content='Madam Speaker, Madam Vice President ...' lookup_str='' metadata={} lookup_index=0
นี่คือตัวอย่างการส่งข้อมูลเชิงวาณิชััที่พร้อมกับเอกสาร โปรดสังเกตว่าวิธีที่มันถูกแบ่งพร้อมกับเอกสาร
metadatas = [{"document": 1}, {"document": 2}]
documents = text_splitter.create_documents([state_of_the_union, state_of_the_union], metadatas=metadatas)
print(documents[0])
page_content='.. Ignoring text ..' lookup_str='' metadata={'document': 1} lookup_index=0
text_splitter.split_text(state_of_the_union)[0]