Phương pháp tách nâng cao

Retrieval Augmented Generation (RAG) với LangChain

Meri Nova

Machine Learning Engineer

Hạn chế của các chiến lược tách hiện tại

 

  1. 🤦 Cách tách ngây thơ (không theo ngữ cảnh)

    • Bỏ qua ngữ cảnh xung quanh
  2. 🖇 Tách theo ký tự thay vì token

    • Token được mô hình xử lý
    • Nguy cơ vượt quá cửa sổ ngữ cảnh

 

SemanticChunker

 

TokenTextSplitter

Retrieval Augmented Generation (RAG) với LangChain

Tách theo token

Bộ tách theo ký tự chia văn bản thành khối dựa trên số ký tự.

Retrieval Augmented Generation (RAG) với LangChain

Tách theo token

Bộ tách theo token chia văn bản thành khối dựa trên số token.

Retrieval Augmented Generation (RAG) với LangChain

Tách theo token

Các token được tô sáng để cho thấy cách chúng khớp với giá trị chunk_size và chunk_overlap.

Retrieval Augmented Generation (RAG) với LangChain

Tách theo token

import tiktoken
from langchain_text_splitters import TokenTextSplitter
example_string = "Mary had a little lamb, it's fleece was white as snow."

encoding = tiktoken.encoding_for_model('gpt-4o-mini')
splitter = TokenTextSplitter(encoding_name=encoding.name,
                             chunk_size=10,
                             chunk_overlap=2)

chunks = splitter.split_text(example_string) for i, chunk in enumerate(chunks): print(f"Chunk {i+1}:\n{chunk}\n")
Retrieval Augmented Generation (RAG) với LangChain

Tách theo token

Chunk 1:
Mary had a little lamb, it's fleece

Chunk 2:
 fleece was white as snow.
Retrieval Augmented Generation (RAG) với LangChain

Tách theo token

for i, chunk in enumerate(chunks):
    print(f"Chunk {i+1}:\nNo. tokens: {len(encoding.encode(chunk))}\n{chunk}\n")
Chunk 1:
No. tokens: 10
Mary had a little lamb, it's fleece was

Chunk 2:
No. tokens: 6
 fleece was white as snow.
Retrieval Augmented Generation (RAG) với LangChain

Tách theo ngữ nghĩa

Một đoạn văn gồm câu về ứng dụng RAG và một câu về chó.

Retrieval Augmented Generation (RAG) với LangChain

Tách theo ngữ nghĩa

Đoạn văn bị tách theo ký tự hoặc token nên đã mất ngữ cảnh.

Retrieval Augmented Generation (RAG) với LangChain

Tách theo ngữ nghĩa

Bộ tách ngữ nghĩa chia đoạn tại điểm chủ đề chuyển từ RAG sang chó.

Retrieval Augmented Generation (RAG) với LangChain

Tách theo ngữ nghĩa

from langchain_openai import OpenAIEmbeddings
from langchain_experimental.text_splitter import SemanticChunker

embeddings = OpenAIEmbeddings(api_key="...", model='text-embedding-3-small')
semantic_splitter = SemanticChunker( embeddings=embeddings,
breakpoint_threshold_type="gradient", breakpoint_threshold_amount=0.8
)
1 https://api.python.langchain.com/en/latest/text_splitter/langchain_experimental.text_splitter. SemanticChunker.html
Retrieval Augmented Generation (RAG) với LangChain

Tách theo ngữ nghĩa

chunks = semantic_splitter.split_documents(data)
print(chunks[0])
page_content='Retrieval-Augmented Generation for\nKnowledge-Intensive NLP Tasks\ Patrick Lewis,
Ethan Perez,\nAleksandra Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich
Küttler,\nMike Lewis, Wen-tau Yih, Tim Rocktäschel, Sebastian Riedel, Douwe Kiela\nFacebook AI
Research; University College London;New York University;\[email protected]\nAbstract\nLarge
pre-trained language models have been shown to store factual knowledge\nin their parameters,
and achieve state-of-the-art results when fine-tuned on down-\nstream NLP tasks. However, their
ability to access and precisely manipulate knowl-\nedge is still limited, and hence on
knowledge-intensive tasks, their performance\nlags behind task-specific architectures.'
metadata={'source': 'rag_paper.pdf', 'page': 0}
Retrieval Augmented Generation (RAG) với LangChain

Cùng luyện tập nào!

Retrieval Augmented Generation (RAG) với LangChain

Preparing Video For Download...