Розбиття зовнішніх даних для пошуку

Розроблення застосунків LLM за допомогою LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Кроки розробки RAG

Загальний процес RAG: завантажувач документів, розбивач документів і процес зберігання та пошуку.

  • Document splitting: розбийте документ на chunks
  • Діліть документи, щоб вмістити їх в context window LLM
Розроблення застосунків LLM за допомогою LangChain

Обдумуємо розбиття…

Перший абзац із вступу до статті Attention is All You Need.

Line 1:

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

Line 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, розбитий на два шматки з перекриттям.

Розроблення застосунків 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]
  • Розбиває за роздільником, щоб було < 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...