RAG From Scratch: Part 1 (Overview)

LangChain
6 Feb 202405:13

Summary

TLDRランス氏は、新しいシリーズ「RAG from Scratch」を開始し、RAG(Retrieval-Augmented Generation)の基本原則から高度なトピックまで解説します。RAGの主な動機は、LLM(Large Language Models)が、プライベートデータや最新のデータなど、あなたが関心を持つかもしれないデータを見ているわけではないということです。また、LLMは、どんどん大きくなるコンテキストウィンドウを持っており、外部の情報を取り込むことができます。RAGは、インデックス作成、検索、生成という3つのステージを通じて行われます。まずは、外部文書をインデックス化し、入力クエリに基づいて簡単に検索できるようにします。次に、検索された文書をLLMにフィードし、検索された文書に基づいて回答を生成します。今後の動画では、これらの基本コンポーネントから派生する多くの興味深い方法やコツを詳細に説明します。また、コードウォークスルーも行い、実際にRAGパイプラインを構築する際に役立つlsmithキーを紹介します。

Takeaways

  • 🚀 ランチェーンのLanceが、RAG(Retrieval-Augmented Generation)の基本原則を解説する新シリーズを開始します。
  • 🔍 RAGの主な動機は、LLM(Large Language Models)が、プライベートデータや最新のデータを含むすべてのデータを見ているわけではないということです。
  • 📈 LLMは、どんどん大きくなるコンテキストウィンドウを持っており、外部ソースからの情報を取り込める能力があります。
  • 💡 RAGは、インデックス化、検索、生成の3つのステージを通じて、LLMに外部データを接続する一般的なパラダイムです。
  • 📚 インデックス化の第1ステージでは、外部文書を入力クエリに基づいて簡単に検索できるようにします。
  • 🔑 第2ステージの検索では、入力された質問に関連する文書を検索し、それらをLLMにフィードします。
  • 📝 最後の生成ステージでは、検索された文書に基づいて答えが生成されます。
  • 🌐 RAGは、新しいタイプのオペレーティングシステムのカーネルであり、外部データへの接続はその開発の中心的な能力です。
  • 🛠️ ランチェーンは、RAGパイプラインの構築時にトレースと可視性のためにlsmithを使用することをお勧めしています。
  • 📈 RAGのコードクイックスタートでは、文書を読み込み、分割、埋め込み、そしてベクトルストアにインデックス化します。
  • 🔬 検索では、入力質問に基づいて関連文書を取得し、それらと質問をLLMに渡してフォーマットされた出力を生成します。
  • 📊 lsmithを使用して、RAGの実行過程を可視化し、各ステップを確認できます。

Q & A

  • ランスが所属する組織の名前は何ですか?

    -ランスは「Lang chain」という組織に所属しています。

  • 「rag from scratch」というシリーズの目的は何ですか?

    -「rag from scratch」は、RAG(Retrieval-Augmented Generation)の基本原則から高度なトピックまでを解説するシリーズです。

  • LLM(Large Language Models)が抱える主な課題は何ですか?

    -LLMは、プライベートデータや最新のデータなど、あなたが関心を持つかもしれないデータを見ることができないという課題を抱えています。

  • LLMのコンテキストウィンドウが拡大する意味は何ですか?

    -LLMのコンテキストウィンドウが拡大することで、外部の情報源から数十ページから数百ページまでの情報を取り込めるようになります。

  • RAGの3つの主要なステージは何ですか?

    -RAGの3つの主要なステージは、インデックス作成、検索、そして生成です。

  • インデックス作成とは何を意味しますか?

    -インデックス作成とは、外部ドキュメントを入力クエリに基づいて簡単に検索できるように整理するプロセスです。

  • ランスが使用しているベクトルストアの名前は何ですか?

    -ランスが使用しているベクトルストアは「Chroma」です。

  • ドキュメントを分割することがなぜ重要ですか?

    -ドキュメントを分割することは、各分割を埋め込み、ベクトルストアにインデックス登録するプロセスに重要です。

  • RAGのプロンプトとは何ですか?

    -RAGのプロンプトとは、入力質問に応じて関連ドキュメントを取得し、それらをLLMにフィードして答えを生成するための指示です。

  • ランスが使用しているLLMの名前は何ですか?

    -スクリプトにはLLMの名前が明示されていませんが、一般的に使用されるLLMの一例として、「Language Model」という名前にしておくことができます。

  • lsmithとは何ですか?

    -lsmithは、RAGパイプラインのトレースや観測性向上に役立つツールです。

  • ランスが紹介するRAGクイックスタートのコードの目的は何ですか?

    -RAGクイックスタートのコードは、RAGの基本的な動作を示すためで、ドキュメントの読み込み、分割、埋め込み、インデックス登録、検索、そして生成を実行するプロセスを示します。

Outlines

00:00

📚 ラグスクラッチシリーズ紹介

ランスが主催する「ラグスクラッチ」というシリーズが開始されます。このシリーズでは、RAG(Retrieval-Augmented Generation)の基本原則から高度なトピックまで解説していく予定です。RAGの主な動機は、LLM(Large Language Models)が扱えないプライベートデータや最新のデータを考慮しないという点にあります。また、LLMはどんどん広範囲のコンテキストウィンドウを持ち、外部データとの接続が重要な機能となっています。RAGには、インデックス作成、検索、生成という3つのステージが存在し、これからの動画ではこれらについて詳しく解説していく予定です。また、コードウォークスルーも行われ、実際にRAGパイプラインを構築する際に役立つlsmithというツールの使い方や、環境変数の設定方法も紹介されています。

05:00

🔍 RAGの仕組みと今後の予定

RAGの仕組みについて概要的に説明されており、今後の動画では各コンポーネントを詳しく解説する予定です。RAGは外部文書をインデックス化し、入力クエリに基づいて検索し、LLMにフィードして答えを生成するプロセスを経ます。また、今後の動画では、インデックス作成、検索、生成の各段階を詳細に解説し、さらに高度なトピックにも触れていく予定です。

Mindmap

Keywords

💡RAG(Retrieval-Augmented Generation)

RAGは、検索と生成を組み合わせた手法で、自然言語処理のタスクに応用されています。この手法は、事前に学習された言語モデル(LLM)に、問い合わせに関連する外部ドキュメントを組み合わせることで、より正確な応答を生成することを目指しています。ビデオでは、RAGがどのようにしてプライベートデータや最新のデータにアクセスし、LLMの限界を克服することができるかについて説明されています。

💡LLM(Large Language Model)

LLMとは、大量のデータを用いて事前学習された言語モデルを指します。これらのモデルは、自然言語の理解や生成タスクで広く使われていますが、プライベートデータや最近のデータにはアクセスできません。ビデオでは、RAGがLLMに外部情報の活用を促進し、その限界を超える方法を提供する理由として触れられています。

💡検索(Retrieval)

検索とは、問い合わせに基づいて関連性の高い外部ドキュメントを検索するプロセスです。RAGの第一段階として、検索は問い合わせに応じて適切な情報を提供する鍵です。ビデオでは、検索がどのようにしてRAGのプロセスに組み込まれ、最終的な応答に寄与するかが説明されています。

💡生成(Generation)

生成とは、検索によって取得された情報に基づいて、言語モデルが答えを生成するプロセスです。RAGの最後の段階で、生成は問い合わせに対する最終的な応答を提供します。ビデオでは、生成がどのようにしてRAGの全体的なプロセスに貢献し、より包括的な情報を提供できるかが解説されています。

💡インデックス(Indexing)

インデックスとは、外部ドキュメントを整理し、検索エンジンが迅速に検索できるようにするプロセスです。RAGでは、インデックスは検索の前段階として、問い合わせに応じた情報の迅速な検索を可能にします。ビデオでは、インデックスがRAGの検索プロセスにどのように貢献するのかが説明されています。

💡ベクトルストア(Vector Store)

ベクトルストアは、テキストデータを数学的なベクトル表現に変換し、検索エンジンが高速に検索できるようにするデータベースです。RAGでは、テキストの埋め込みを格納し、検索の際に使用されます。ビデオでは、ベクトルストアがどのようにしてRAGの検索と生成プロセスに貢献するのかが説明されています。

💡プロンプト(Prompt)

プロンプトとは、言語モデルに入力を与えるためのテキストです。RAGでは、プロンプトは問い合わせと検索によって取得されたドキュメントを組み合わせ、言語モデルに入力します。ビデオでは、プロンプトがどのようにしてRAGの生成プロセスに貢献し、より正確な応答を生むかが解説されています。

💡ドキュメントプロセシング(Document Processing)

ドキュメントプロセシングとは、テキストドキュメントを解析、整理し、検索や生成に使用できる形式に変換するプロセスです。RAGでは、ドキュメントプロセシングは検索と生成の前段階で、情報の整理と構造化を促進します。ビデオでは、ドキュメントプロセシングがRAGの全体的なプロセスにどのように貢献するのかが説明されています。

💡トークン(Token)

トークンとは、言語モデルが扱うテキストデータの最小単位です。トークンは、単語やフレーズ、記号などが含まれます。ビデオでは、トークンが言語モデルの学習とRAGの検索プロセスにどのように関与しているかが触れられています。

💡オープンAI埋め込み(OpenAI Embeddings)

オープンAI埋め込みとは、オープンAIが提供するテキストの数学的な表現です。これらの埋め込みは、テキストの意味を数学的に表現し、検索エンジンが高速に検索できるようにします。ビデオでは、オープンAI埋め込みがRAGの検索プロセスにどのように貢献するのかが説明されています。

💡lsmith

lsmithは、言語モデルの実行を追跡し、観察するためのツールです。ビデオでは、lsmithがRAGパイプラインの構築時にどのように役立つのか、特に問題の特定や改善点の洗い出しに役立つ方法が説明されています。

Highlights

Starting a new series called 'RAG from Scratch' to explore basic principles of Retrieval-Augmented Generation (RAG) and build up to advanced topics.

Motivation for RAG is to address the limitations of LLMs (Large Language Models) in handling private or very recent data not included in their pre-training.

LLMs have context windows that are increasing in size, allowing them to process more information from external sources.

RAG involves three stages: indexing, retrieval, and generation.

Indexing involves organizing external documents for easy retrieval based on input queries.

Retrieval stage fetches relevant documents to be used in the final generation stage of the LLM.

RAG is seen as a kernel of a new kind of operating system, with connecting to external data being a central capability.

The video series will cover various methods and tricks related to the three basic components of RAG.

Each video will be kept short, around five minutes, focusing on advanced topics.

Introduction to the basic ideas behind indexing, retrieval, and generation in the first three videos.

Interactive code walkthrough demonstrating a RAG quick start using a notebook and installed packages.

Explanation of using environment variables for LLM keys and the importance of tracing and observability in building RAG pipelines.

Loading and splitting documents into chunks for embedding and indexing into a vector store.

Use of OpenAI embeddings and Chroma for vector storage in a local setup.

Defining a prompt for RAG and setting up the LLM for document processing.

Chain setup to take input question, retrieve documents, and generate an answer grounded in retrieved documents.

Demonstration of using Llama (LLM) to trace and observe the RAG process.

Showcasing the retrieved documents and how they are used to answer the input question.

Providing a general overview of how RAG works with plans to break down each component in more detail in future videos.

Transcripts

play00:03

hi this is Lance from Lang chain we're

play00:05

starting a new series called rag from

play00:07

scratch that's going to walk through

play00:09

some of the basic principles for Rag and

play00:12

kind of build up to Advanced

play00:15

topics um so one of the main motivations

play00:18

for rag is simply that llms haven't seen

play00:21

all of the data that you may care about

play00:24

so like private data or very recent data

play00:26

would not be included in the

play00:29

pre-training Run for these LMS and you

play00:31

can see here on the graph on the xaxis

play00:33

that the number of tokens that they're

play00:35

pre-trained on which is of course very

play00:37

large um but of course it's still always

play00:40

going to be limited relative to private

play00:42

data that you care about or for example

play00:44

recent

play00:45

data but there's another interesting

play00:47

consideration is that llms have context

play00:49

windows that are actually getting

play00:51

increasingly large so you know coming

play00:54

going from like thousands of tokens to

play00:56

many thousands of tokens which

play00:58

represents you know dozens of Pages up

play01:00

to hundreds of pages we can fit

play01:02

information into them from external

play01:05

sources and a way to think about this is

play01:08

llms are kind of a a kernel of a new

play01:10

kind of operating system and connecting

play01:13

them to external data is kind of a very

play01:15

Central capability in the development of

play01:18

this kind new emergent operating

play01:21

system so retrieval alment to generation

play01:24

or rag is a very popular kind of General

play01:27

Paradigm for doing this which typically

play01:30

involves three stages so the first stage

play01:33

is indexing some external documents such

play01:36

that they can be easily

play01:38

retrieved based on an input query so for

play01:41

example we ask a question we retrieve

play01:44

documents that are relevant to that

play01:45

question we feed those documents into an

play01:48

llm in the final generation stage to

play01:50

produce an answer that's grounded in

play01:52

those retrieve

play01:55

documents now we're starting from

play01:58

scratch but we're going to kind of build

play02:00

up to this broader view of rag you can

play02:03

see here there's a lot of interesting

play02:06

methods and tricks that kind of fan out

play02:10

from those three basic components of

play02:12

indexing retrieval and

play02:14

generation and future videos are

play02:18

actually going to walk through those in

play02:19

detail we're going to try to keep each

play02:21

video pretty short like five minutes but

play02:23

we're going to spend a lot of time on

play02:25

some of those more advanced

play02:27

topics first over the next three videos

play02:31

I'll just be laying out the very basic

play02:33

kind of ideas behind indexing retrieval

play02:34

and generation and then we'll kind of

play02:36

build beyond that into those more

play02:39

advanced

play02:40

themes and now I want to show just a

play02:42

quick code walkth through because we

play02:43

want to make these videos also a little

play02:45

bit

play02:46

interactive so right here and this repo

play02:48

will be shared it's public I have a a

play02:51

notebook open and I've just just

play02:54

basically installed a few

play02:57

packages and I've set a few environment

play02:59

variables for my lsmith keys which um I

play03:04

personally do recommend it's really

play03:05

useful for tracing

play03:07

observability um particularly when

play03:09

you're building rag

play03:11

pipelines so what I'm going to show here

play03:13

is the code for a rag quick start which

play03:15

is linked here and I'm going to run this

play03:18

but I'm then going to kind of walk

play03:20

through everything that's going on so

play03:23

actually if we think back to our diagram

play03:24

all we're doing here is we're loading

play03:26

documents in this case I'm loading a

play03:28

blog post we're then splitting them and

play03:32

we'll talk about that in future like uh

play03:34

short videos on like why splitting is

play03:36

important but just for now recognize

play03:38

we're splitting them or setting a chunk

play03:40

size of um you know a thousand

play03:42

characters so we're splitting up our

play03:44

documents every split is embedded and

play03:49

indexed into this Vector store so we say

play03:51

we picked open eye embeddings we're

play03:52

using chromas our Vector storage runs

play03:55

locally and now we' find this

play03:58

retriever we then have defined a prompt

play04:01

for

play04:02

rag um we've defined our llm we've done

play04:05

some minor document processing we set up

play04:08

this

play04:09

chain which will basically take our

play04:11

input

play04:13

question run our retriever to fetch

play04:15

relevant documents put the retrieve

play04:18

documents and our question into our

play04:19

prompt pass it to the LM format the

play04:23

output as a string and we can see here's

play04:24

our

play04:25

output now we can open up lsmith and we

play04:28

can actually see

play04:30

how this Ran So here was our question

play04:33

and here's our output and we can

play04:35

actually look here's our retriever here

play04:38

is our retrieve documents so that's

play04:40

pretty

play04:41

nice and ultimately here was the prompt

play04:44

that we actually passed into the LM

play04:47

you're an assistant for QA task use the

play04:49

following pieces of retrieve content to

play04:51

answer the question here's our

play04:53

question and then here's all the content

play04:57

this we retrieved and that DRS

play05:00

answer so this just gives a very general

play05:02

overview of how rag works and in future

play05:05

uh short videos we're going to like

play05:07

break down each of these pieces and I in

play05:09

in a lot more detail uh

play05:11

thanks

Rate This

5.0 / 5 (0 votes)

Related Tags
RAGランスランチェーン基本原則応用技術シリーズインデックス検索生成LLMプリトレインベクトルストアクロマオープンAIQAタスクプロンプトコードチュートリアル
Do you need a summary in English?