Advanced splitting methods

LangChain के साथ Retrieval Augmented Generation (RAG)

Meri Nova

Machine Learning Engineer

हमारी मौजूदा splitting रणनीतियों की सीमाएँ

 

  1. 🤦 Splits साधारण हैं (context-aware नहीं)

    • आस-पास के टेक्स्ट का संदर्भ नज़रअंदाज़ होता है
  2. 🖇 Splits characters से होते हैं, tokens से नहीं

    • Tokens को मॉडल प्रोसेस करते हैं
    • context window से आगे निकलने का जोखिम

 

SemanticChunker

 

TokenTextSplitter

LangChain के साथ Retrieval Augmented Generation (RAG)

Tokens पर splitting

एक character text splitter जो characters की संख्या के आधार पर टेक्स्ट को chunks में बाँट रहा है.

LangChain के साथ Retrieval Augmented Generation (RAG)

Tokens पर splitting

एक token text splitter जो tokens की संख्या के आधार पर टेक्स्ट को chunks में बाँट रहा है.

LangChain के साथ Retrieval Augmented Generation (RAG)

Tokens पर splitting

Tokens को हाइलाइट किया गया है ताकि वे `chunk_size` और `chunk_overlap` से कैसे मेल खाते हैं, दिखे.

LangChain के साथ Retrieval Augmented Generation (RAG)

Tokens पर splitting

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 पर splitting

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

Chunk 2:
 fleece was white as snow.
LangChain के साथ Retrieval Augmented Generation (RAG)

Tokens पर splitting

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)

Semantic splitting

एक पैराग्राफ जिसमें RAG एप्लिकेशंस पर एक वाक्य और कुत्तों पर एक वाक्य है.

LangChain के साथ Retrieval Augmented Generation (RAG)

Semantic splitting

पैराग्राफ को characters या tokens से बाँटा गया है, जिससे संदर्भ खो गया है.

LangChain के साथ Retrieval Augmented Generation (RAG)

Semantic splitting

एक semantic splitter ने पैराग्राफ को उस बिंदु पर बाँटा जहाँ विषय RAG से कुत्तों पर बदलता है.

LangChain के साथ Retrieval Augmented Generation (RAG)

Semantic splitting

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)

Semantic splitting

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...