Podział tekstu, osadzanie i przechowywanie wektorów

Retrieval Augmented Generation (RAG) z LangChain

Meri Nova

Machine Learning Engineer

Przygotowanie danych do wyszukiwania

Ładowanie dokumentów.

Retrieval Augmented Generation (RAG) z LangChain

Przygotowanie danych do wyszukiwania

Podział dokumentów.

Retrieval Augmented Generation (RAG) z LangChain

Przygotowanie danych do wyszukiwania

Fragmenty dokumentów są osadzane.

Retrieval Augmented Generation (RAG) z LangChain

Przygotowanie danych do wyszukiwania

Fragmenty dokumentów są przechowywane.

Retrieval Augmented Generation (RAG) z LangChain

Przygotowanie danych do wyszukiwania

Krok podziału jest wyróżniony w przepływie pracy RAG.

Retrieval Augmented Generation (RAG) z LangChain

chunk_size

Strzałka pokazująca, że idealny rozmiar fragmentu jest gdzieś pośrodku; duże fragmenty mogą powodować wolne pobieranie i trudności z interpretacją, a małe mogą zawierać niewystarczający kontekst.

chunk_overlap

  • Uwzględnia informacje poza granicą

Dwa fragmenty z zaznaczonym obszarem nakładania się.

Retrieval Augmented Generation (RAG) z LangChain

CharacterTextSplitter

from langchain_text_splitters import CharacterTextSplitter

text = """Machine learning is a fascinating field.\n\nIt involves algorithms and models that can learn from data. These models can then make predictions or decisions without being explicitly programmed to perform the task.\nThis capability is increasingly valuable in various industries, from finance to healthcare.\n\nThere are many types of machine learning, including supervised, unsupervised, and reinforcement learning.\nEach type has its own strengths and applications."""
text_splitter = CharacterTextSplitter( separator="\n\n", chunk_size=100, chunk_overlap=10 )
Retrieval Augmented Generation (RAG) z LangChain

CharacterTextSplitter

chunks = text_splitter.split_text(text)

print(chunks) print([len(chunk) for chunk in chunks])
['Machine learning is a fascinating field.',
 'It involves algorithms and models that can learn from data. These models can...',
 'There are many types of machine learning, including supervised, unsupervised...']

[40, 260, 155]
  • Fragmenty mogą nie mieć kontekstu
  • Fragmenty mogą być większe niż chunk_size
Retrieval Augmented Generation (RAG) z LangChain

RecursiveCharacterTextSplitter

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(

separators=["\n\n", "\n", " ", ""],
chunk_size=100, chunk_overlap=10
)
Retrieval Augmented Generation (RAG) z LangChain

RecursiveCharacterTextSplitter

chunks = splitter.split_text(text)

print(chunks)
print([len(chunk) for chunk in chunks])
['Machine learning is a fascinating field.',
 'It involves algorithms and models that can learn from data. These models ...',
 'or decisions without being explicitly programmed to perform the task.',
 'This capability is increasingly valuable in various industries, from ...',
 'There are many types of machine learning, including supervised, ...',
 'learning.',
 'Each type has its own strengths and applications.']
[40, 98, 69, 91, 95, 9, 49]
Retrieval Augmented Generation (RAG) z LangChain

Podział dokumentów

from langchain_community.document_loaders import PyPDFLoader

loader = PyPDFLoader("research_paper.pdf")
documents = loader.load()

splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)

chunks = splitter.split_documents(documents)
Retrieval Augmented Generation (RAG) z LangChain

Podział dokumentów

print(chunks)

print([len(chunk.page_content) for chunk in chunks])
[Document(metadata={'source': 'Rag Paper.pdf', 'page': 0}, page_content='...'),
 Document(metadata={'source': 'Rag Paper.pdf', 'page': 0}, page_content='...'),
 Document(metadata={'source': 'Rag Paper.pdf', 'page': 0}, page_content='...')]

[928, 946, 921,...]
Retrieval Augmented Generation (RAG) z LangChain

Osadzanie i przechowywanie

Kroki osadzania i przechowywania są wyróżnione.

Retrieval Augmented Generation (RAG) z LangChain

Czym są osadzenia?

Zdanie przekazywane do modelu osadzania.

Retrieval Augmented Generation (RAG) z LangChain

Czym są osadzenia?

Model osadzania reprezentuje tekst jako wektor wartości liczbowych.

Retrieval Augmented Generation (RAG) z LangChain

Czym są osadzenia?

what_are_embeddings3.jpg

Retrieval Augmented Generation (RAG) z LangChain

Czym są osadzenia?

what_are_embeddings4.jpg

Retrieval Augmented Generation (RAG) z LangChain

Osadzanie i przechowywanie fragmentów

  • Osadzanie i przechowywanie za pomocą: OpenAI i ChromaDB
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma

embedding_model = OpenAIEmbeddings(
    api_key=openai_api_key,
    model="text-embedding-3-small"
)


vector_store = Chroma.from_documents( documents=chunks, embedding=embedding_model )
Retrieval Augmented Generation (RAG) z LangChain

Czas na ćwiczenia!

Retrieval Augmented Generation (RAG) z LangChain

Preparing Video For Download...