進階分割方法

使用 LangChain 的 Retrieval Augmented Generation(RAG)

Meri Nova

Machine Learning Engineer

目前分割策略的侷限

 

  1. 🤦 分割很直覺(無語境意識)

    • 忽略周遭文字的語境
  2. 🖇 以字元而非 tokens 分割

    • 模型以 tokens 處理文字
    • 可能超出 context window

 

SemanticChunker

 

TokenTextSplitter

使用 LangChain 的 Retrieval Augmented Generation(RAG)

以 tokens 分割

以字元數將文字切成區塊的字元分割器。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

以 tokens 分割

以 token 數將文字切成區塊的 token 分割器。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

以 tokens 分割

反白顯示 tokens,對齊 chunk_size 與 chunk_overlap 的示意。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

以 tokens 分割

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")
使用 LangChain 的 Retrieval Augmented Generation(RAG)

以 tokens 分割

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

Chunk 2:
 fleece was white as snow.
使用 LangChain 的 Retrieval Augmented Generation(RAG)

以 tokens 分割

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.
使用 LangChain 的 Retrieval Augmented Generation(RAG)

語意分割

一段文字,包含關於 RAG 應用與關於狗的句子。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

語意分割

使用字元或 tokens 分割後,段落失去語境關聯。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

語意分割

語意分割器在主題從 RAG 轉為狗的地方切分段落。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

語意分割

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
使用 LangChain 的 Retrieval Augmented Generation(RAG)

語意分割

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}
使用 LangChain 的 Retrieval Augmented Generation(RAG)

一起來練習吧!

使用 LangChain 的 Retrieval Augmented Generation(RAG)

Preparing Video For Download...