การแบ่งข้อมูลภายนอกเพื่อการค้นคืน

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

ขั้นตอนการพัฒนา RAG

ขั้นตอน RAG แบบทั่วไป: document loader, document splitter และกระบวนการจัดเก็บและค้นคืน

  • การแบ่งเอกสาร (Document splitting): แบ่งเอกสารออกเป็น chunks
  • แบ่งเอกสารให้พอดีกับ context window ของ LLM
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

ทำความเข้าใจเรื่องการแบ่ง...

ย่อหน้าแรกจากบทนำของบทความ Attention is All You Need

บรรทัดที่ 1:

Recurrent neural networks, long short-term memory [13] and gated recurrent [7] neural networks

บรรทัดที่ 2:

in particular, have been firmly established as state of the art approaches in sequence modeling and
1 https://arxiv.org/abs/1706.03762
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

Chunk overlap

ย่อหน้าแรกจากบทนำของบทความ Attention is All You Need ที่ถูกแบ่งออกเป็นสอง chunk พร้อม chunk overlap

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

กลยุทธ์การแบ่งเอกสารที่ดีที่สุดคืออะไร?

คำว่า "context" ที่ถูกแบ่งออกเป็นตัวอักษรแต่ละตัว

 

  1. CharacterTextSplitter
  2. RecursiveCharacterTextSplitter
  3. และอื่น ๆ อีกมาก
1 Wikipedia Commons
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain
quote = '''One machine can do the work of fifty ordinary humans.\nNo machine can do
the work of one extraordinary human.'''
len(quote)
103
chunk_size = 24
chunk_overlap = 3
1 Elbert Hubbard
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain
from langchain_text_splitters import CharacterTextSplitter


ct_splitter = CharacterTextSplitter( separator='.', chunk_size=chunk_size, chunk_overlap=chunk_overlap)
docs = ct_splitter.split_text(quote) print(docs)
print([len(doc) for doc in docs])
['One machine can do the work of fifty ordinary humans',
 'No machine can do the work of one extraordinary human']

[52, 53]
  • แบ่งด้วย separator เพื่อให้ < chunk_size แต่อาจไม่สำเร็จเสมอไป!
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain
from langchain_text_splitters import RecursiveCharacterTextSplitter


rc_splitter = RecursiveCharacterTextSplitter( separators=["\n\n", "\n", " ", ""], chunk_size=chunk_size, chunk_overlap=chunk_overlap)
docs = rc_splitter.split_text(quote) print(docs)
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

RecursiveCharacterTextSplitter

  • separators=["\n\n", "\n", " ", ""]
['One machine can do the',
 'work of fifty ordinary',
 'humans.',
 'No machine can do the',
 'work of one',
 'extraordinary human.']
  1. ลองแบ่งตามย่อหน้า: "\n\n"
  2. ลองแบ่งตามประโยค: "\n"
  3. ลองแบ่งตามคำ: " "
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

RecursiveCharacterTextSplitter กับ HTML

from langchain_community.document_loaders import UnstructuredHTMLLoader 
from langchain_text_splitters import RecursiveCharacterTextSplitter


loader = UnstructuredHTMLLoader("white_house_executive_order_nov_2023.html") data = loader.load()
rc_splitter = RecursiveCharacterTextSplitter( chunk_size=chunk_size, chunk_overlap=chunk_overlap, separators=['.'])
docs = rc_splitter.split_documents(data) print(docs[0])
Document(page_content="To search this site, enter a search term [...]
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

มาฝึกกันเถอะ!

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

Preparing Video For Download...