Tách dữ liệu ngoài để truy xuất

Phát triển ứng dụng LLM với LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Các bước phát triển RAG

Quy trình RAG tổng quát: bộ nạp tài liệu, bộ tách tài liệu, và quá trình lưu trữ - truy xuất.

  • Tách tài liệu: chia tài liệu thành khối (chunk)
  • Chia nhỏ để phù hợp cửa sổ ngữ cảnh của LLM
Phát triển ứng dụng LLM với LangChain

Suy nghĩ về cách tách...

Đoạn đầu tiên trong phần giới thiệu của bài báo Attention is All You Need.

Dòng 1:

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

Dòng 2:

in particular, have been firmly established as state of the art approaches in sequence modeling and
1 https://arxiv.org/abs/1706.03762
Phát triển ứng dụng LLM với LangChain

Chồng lấp khối

Đoạn đầu tiên của phần giới thiệu bài báo Attention is All You Need được chia thành hai khối có phần chồng lấp.

Phát triển ứng dụng LLM với LangChain

Chiến lược tách tài liệu tốt nhất là gì?

Từ "context" được chia thành từng chữ cái.

 

  1. CharacterTextSplitter
  2. RecursiveCharacterTextSplitter
  3. Nhiều cách khác
1 Wikipedia Commons
Phát triển ứng dụng LLM với 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
Phát triển ứng dụng LLM với 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]
  • Tách theo dấu phân cách để < chunk_size, nhưng không phải lúc nào cũng thành công!
Phát triển ứng dụng LLM với 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)
Phát triển ứng dụng LLM với 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. Thử tách theo đoạn: "\n\n"
  2. Thử tách theo câu: "\n"
  3. Thử tách theo từ: " "
Phát triển ứng dụng LLM với LangChain

RecursiveCharacterTextSplitter với 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 [...]
Phát triển ứng dụng LLM với LangChain

Ayo berlatih!

Phát triển ứng dụng LLM với LangChain

Preparing Video For Download...