見出し画像

【LangChain】英語解説を日本語で読む【2023年4月26日|@Sam Witteveen】

このビデオでは、LangChainとFAISSベクターストアを使用して、PDFファイルのチャットとクエリを行う方法について説明します。
公開日:2023年4月26日
※動画を再生してから、インタビューを読むのがオススメです。


Okay.

はい。

In this video, we're going to be looking at using a LangChain to chat with and query PDF files.

このビデオでは、LangChainを使って、PDFファイルとチャットしたり、クエリーを行うことについて見ていきます。

And more importantly than that, I'm going to introduce a whole bunch of different elements that we're going to use in a lot of the things coming up on videos as well.

そして、それ以上に重要なのは、これからビデオで紹介する多くのことに使用する、さまざまな要素を紹介することです。

This is the game plan, and this is the key thing to understand here.

これがゲームプランであり、ここで理解すべき重要なことです。

While we can do a simple question A over something and feed that into the context of the prompt, if our prompt is limited to 4,000 tokens or perhaps maybe even 8,000 tokens with a GPT-4, it's still a very finite thing.

単純な質問Aを行い、それをプロンプトの文脈に反映させることはできますが、プロンプトが4,000トークン、あるいはGPT-4では8,000トークンに制限されているとしたら、それはまだ非常に有限なものです。

And if we want it to go through a book, it's not going to work that well.

そして、それを本として成立させようとすると、そううまくはいきません。

So this is where we're going to introduce a whole bunch of new things that relate to vector stores, embeddings, using queries and retrieval to get information that we're going to basically use for doing our chat with our PDF file.

そこで今回は、ベクトルストア、エンベッディング、クエリや検索を使った情報取得など、PDFファイルを使ったチャットに関連する新しい事柄を紹介します。

So the way this works is we're going to have a document and the document that I've chosen is this.

この仕組みは、ドキュメントを用意し、私が選んだドキュメントはこれです。

So this is a book by Reid Hoffman that he's released all about GPT-4 and all about AI and he actually wrote it with GPT-4 and I don't think he's put out the book to make money.

これはリード・ホフマンがGPT-4とAIについてまとめた本を出したもので、彼は実際にGPT-4を使ってこの本を書いているのですが、彼がお金を稼ぐためにこの本を出したとは思えません。

He's basically put it that you can order it on Amazon or you can download the free PDF.

彼は基本的に、アマゾンで注文するか、無料のPDFをダウンロードできるようにしています。

And that's exactly what we've done here.

私たちがここでやったこともまさにそうです。

I've basically just downloaded the free PDF, and you can see that it's got quite a number of pages.

無料のPDFをダウンロードしたところ、かなりのページ数があることがお分かりいただけると思います。

We're going to go through and we're going to convert this PDF into something that we can query and that we can talk to as we go through this.

これから、このPDFをクエリや会話に使えるように変換していきます。

So the first thing that we're going to be doing is we're going to be just loading in the PDF file and we're going to split the document into chunks.

まず最初に、PDFファイルを読み込み、ドキュメントを塊に分割します。

And the idea here is that we're going to build a vector store and the vector store, we're going to query that vector store.

そして、ベクターストアを構築し、そのベクターストアにクエリを発行するというものです。

So just like you would with a search engine, we're going to be looking for things in that vector store.

つまり、検索エンジンと同じように、ベクターストアにあるものを探すのです。

But rather than do it through keywords, we're going to be doing it through semantic meaning.

しかし、キーワードで検索するのではなく、意味的な意味を通して検索します。

And this is done, I'll look at doing a whole video about the concept of embeddings, but what we're going to do is we're going to make an embedding for each chunk in here and that embedding is going to be a vector.

埋め込みの概念については、ビデオで紹介したいと思いますが、これから行うのは、この中の各チャンクに対して埋め込みを作り、その埋め込みをベクトルにすることです。

It's going to be quite a large vector because we're actually going to get, in this case, we're going to get OpenAI to basically make that vector for us.

この埋め込みはベクトルになるのですが、このベクトルは非常に大きなものになります。

And that vector will be representative of all the information that's in that chunk.

そのベクトルは、そのチャンクに含まれるすべての情報を代表するものになります。

So if we ask a question about some information, when you basically embed the question, which we're going to do later on, we would have our standalone question.

ですから、ある情報に関して質問をする場合、質問を埋め込むと、後ほど紹介しますが、独立した質問ができることになります。

We would embed that, and then that will go to the vector store, and the vector store will then return the chunks of information that are most relevant to what we want.

それを埋め込むと、ベクトルストアに送られます。ベクトルストアは、私たちが欲しい情報に最も関連性のある情報の塊を返します。

And then they will be passed into the language model, along with our question.

そして、質問と一緒に言語モデルに渡されます。

And then the language model will basically decide, okay, reading through these, what is the answer?

そして、言語モデルは基本的に、これらを読んで、答えは何だろうかと判断します。

The idea of the idea of this sort of semantic search thing is not a new one.

このようなセマンティック検索という考え方は、新しいものではありません。

It's been around for a long time.

ずっと以前からあるものです。

And even the idea of using a large language model to query a section of text is also something that's been around for a long time.

また、大規模な言語モデルを使ってテキストの一部を照会するというアイデアも、ずっと前からあるものです。

So there've been some famous data sets like squad, and people would train up models like BERT on doing this kind of thing.

そのため、Squadのような有名なデータセットがあり、BERTのようなモデルをトレーニングしてこのようなことをやっていました。

And it turns out that this is something a model can do pretty well.

そして、これはモデルがかなりうまくできることがわかりました。

If you give it some text in its context, later on, we'll look at the prompts to see how this is done.

コンテキストにテキストを与えると、後でプロンプトを見て、これがどのように行われるかを確認することができます。

It can then look through that text, work out what relates to the question asked, and then return an answer for this.

そして、そのテキストに目を通し、何が質問と関連しているのかを調べ、それに対する答えを返すことができます。

So there are a number of key things we've got to do here.

ここでやるべきことは、いくつかあります。

We've got to basically load up the document, we've got to split the document.

基本的には、文書を読み込み、文書を分割します。

We've got to create the embeddings.

埋め込みを作成する。

We've got to put it in a vector store.

それをベクターストアに格納します。

So a vector store is like a database.

ベクターストアとは、データベースのようなものです。

And then we're going to basically use LangChain to create our chain, which can then basically take a query, look up a vector store, get the information back, bring that into the chain, and then combine that to finally come up with an answer.

そして、基本的にはLangChainを使ってチェーンを作ります。チェーンは、クエリを受け取り、ベクターストアを調べ、情報を取り戻し、それをチェーンに取り込み、最後にそれを組み合わせて答えを出すことができます。

Okay, so here you can see, I've got the data for downloading this, and we've got our first things that we're going to be bringing in.

さて、ここにダウンロード用のデータがあり、最初に取り込むものがあります。

First off, we've just got a PDF reader.

まず最初に、PDFリーダーを用意しました。

Now I'm not saying that this is the best PDF reader, in other videos, we'll look at using some other ones and stuff like that, the idea with this one is everything's simple, and then we'll do other videos about each specific part for this.

このPDFリーダーがベストだとは言いません。他のビデオでは、他のPDFリーダーの使い方なども紹介しますが、このビデオはすべてがシンプルであることが特徴です。

The embeddings I'm using, they're OpenAI embeddings.

私が使っているエンベッディングは、OpenAIのエンベッディングです。

You'll see in future videos that I will show you free ways that you don't need to pay for these embeddings and use OpenAI.

今後のビデオでは、これらの埋め込みにお金を払う必要がなく、OpenAIを使う無料の方法を紹介する予定です。

We need a text splitter, and then we need a vector store.

テキストスプリッターと、ベクターストアが必要です。

And in this vector store, I'm just going to store it in memory with a library called FICE or face here.

そしてこのベクターストアでは、FICEやface hereというライブラリを使って、メモリに保存しておくだけです。

Okay.

なるほど。

First up, reading the PDF in.

まず最初に、PDFを読み込む。

So we've got our PDF reader.

そこで、PDFリーダーを用意しました。

It brings in the PDF and you can see that from this, we've basically got a text and we've got our text as one long string in here.

PDFを読み込むと、基本的にテキストが表示され、テキストは1つの長い文字列としてここに表示されていることがわかります。

And if we, if we look at how many characters that is, we can see that it's almost 360,000 characters.

このテキストが何文字あるのか見てみると、ほぼ36万文字あることがわかります。

If we look at just the first hundred characters, we can see that this is what we're getting out.

最初の100文字だけを見てみると、このように表示されることがわかります。

Now, you can see that the formatting is not great, right?

さて、書式が良くないことがお分かりでしょうか?

We've got some spaces in weird places and stuff like that.

変なところに空白があったり、そんな感じです。

This is where over time you would try out different kinds of ways to bring your data in.

このように、時間をかけてデータを取り込む方法をいろいろと試していくことになります。

And it will be different for each project.

そして、それはプロジェクトごとに異なるでしょう。

For some projects, something really basic will work well.

あるプロジェクトでは、本当に基本的な方法でうまくいくでしょう。

For other projects, you want to use a library called Instructure.

また、Instructureというライブラリを使いたいプロジェクトもあります。

For some projects, maybe you want to use an API from AWS or Google cloud, et cetera.

AWSやGoogleクラウドのAPIを使いたいプロジェクトもあるでしょう。

Right.

そうですね。

But we've brought that text in now.

でも、そのテキストは今持ってきました。

The next thing up is to basically split it up.

次は、それを基本的に分割します。

So here we've got a text splitter and this is the most basic kind of splitter here.

ここではテキストスプリッターを用意しましたが、これは最も基本的なスプリッターです。

We're literally just splitting on the number of characters.

文字通り、文字数で分割しています。

And so we're basically going to say that, okay, we're going to have a chunk size of 1000 characters and then we're going to stride over it.

つまり、チャンクサイズを1000文字とし、その上をまたぐということです。

So what do we mean by that?

では、どういうことかというと

We mean that there's going to be an overlap.

つまり、オーバーラップが発生するということです。

So imagine if I had, imagine if I've got a few sentences and half the thing I'm talking about is in the first two sentences.

もし、私がいくつかの文章を書いたとしたら、最初の2つの文章に私が話していることの半分が書かれているとします。

And then I split and then half of it was in the second two sentences.

そして、分割して、その半分が2つ目の文章にあったとします。

So maybe I've got like your 50 sentences, but the last two sentences have got a key piece of information and they go on to the first two sentences of the next chunk.

つまり、50個の文章があったとして、最後の2つの文章に重要な情報があり、それが次の塊の最初の2つの文章に続いている。

So in that kind of case, to prevent that we have this moving window, which is this chunk overlap and we slide this across.

このような場合、それを防ぐために、このムービングウィンドウがあります。

So this is basically going to mean that we're going to have an overlap of 200 characters for each chunk size.

つまり、各チャンクサイズで200文字のオーバーラップが発生することになります。

So a lot of the text or a portion of the text will appear in more than one chunk.

つまり、テキストの多くは、あるいはテキストの一部は、複数のチャンクに表示されることになります。

But because we're taking the whole semantic meaning of that, that we're going to make an embedding, it's going to be different from chunk to chunk.

しかし、その意味全体を汲み取って埋め込みを行うわけですから、チャンクごとに異なることになります。

So each chunk will have its own semantic meaning in there, but just in case something was going to overlap, we want to make sure we can catch that.

だから、それぞれのチャンクにはそれぞれの意味合いがあるのですが、万が一、何かが重なるようなことがあっても、それをキャッチできるようにしたいのです。

So there are a whole bunch of strategies for this, depending on what your data is.

そのため、データの内容に応じて、さまざまな戦略があります。

I'm using a very small overlap here; you do want to use some overlap.

ここではごくわずかな重なりを使っていますが、重なりはあったほうがいいでしょう。

I see some people tend to use none, but you really would like to use some overlap.

オーバーラップを使わない人もいますが、オーバーラップはあったほうがいいでしょう。

Then, for certain things, if the meaning is changing quite quickly, you might have an overlap of 500, so that you're taking the first thousand, moving 500 along, taking from 500 to 500, and then you might have 100 to 1500 moving along from 1000.

また、ある種のものについては、意味の変化が非常に速い場合、500のオーバーラップが必要かもしれません。つまり、最初の1000を取り、500から500に移動し、1000から100から1500に移動するのです。

All right.

そうですね。

Obviously that's going to create a lot more chunks.

そうなると当然、チャンクの数が多くなります。

So you have to balance the number of chunks you're getting with the amount of overlap that is going to be good for you.

そのため、得られるチャンクの数と、自分にとってよい重なり具合とのバランスをとる必要があるのです。

All right.

なるほど。

So we've got that.

では、これで完成です。

You can see after that, we've ended up with 448 different sort of texts or mini documents here.

この後、448種類のテキストやミニドキュメントができあがりました。

And if we look at some of these, we can see that, okay, if we look at the 20th one, we can see that it's got, it's obviously starting with half of the sentence from the previous one, it's going along as we do this.

20番目の文章を見ると、明らかに前の文章の半分から始まっているのがわかります。

And we can see that, okay, it's got a whole bunch of information in there.

このように、たくさんの情報が入っていることがわかります。

We look at another one, we can see different information in there.

別のものを見てみると、そこには別の情報が含まれていることがわかります。

So it's just splitting this up into chunks there.

つまり、これは塊に分割されているのです。

Next up, we want to make embeddings for these things.

次に、これらの情報を埋め込むための埋め込みを行います。

So here we're going to basically use OpenAI embeddings.

ここでは、基本的にOpenAIの埋め込みを使用します。

So if we come in and we look at this, we're just setting up that this is going to be the embeddings.

つまり、ここに来てこれを見ると、これがエンベッディングになるという設定になっているわけです。

Then we're going to basically use this FICE library and we're going to just tell it from the texts.

そして、基本的にはこのFICEライブラリを使い、テキストから指示することになります。

We're going to pass in the texts, we're going to pass in the embedding maker - so this is actually our embedding maker - and it will then create the vector store itself.

テキストを渡して、エンベッディングメーカーを渡すと、これがエンベッディングメーカーです。

And if we look at this, we can see that once we've got this created, we can actually look at the embedding function.

そして、これを見ると、これを作成した後、実際に埋め込み関数を見ることができることがわかります。

And we can see that, okay, sure enough, it's using OpenAI.

すると、なるほど、確かにOpenAIを使っていることがわかります。

We can see the model it's using is text embedding, Ada 002.

使っているモデルは、テキスト埋め込み、Ada 002であることがわかります。

We can get some other key information on there.

その他、いくつかの重要な情報を得ることができます。

Now, if we, if we want to test this out, so if I come along and say, okay, how does GPT-4 change social media?

さて、もしこれをテストしたいのであれば、私が「GPT-4はソーシャルメディアをどう変えるか?

This is a query and I pass this query into the doc search and I'm using similarity matching search here.

これはクエリで、このクエリをdoc searchに渡すと、ここでは類似性マッチング検索を使っています。

So we're looking for something that's just purely the closest to this.

つまり、これに最も近いものを探しているのです。

Now there are a bunch of different ones that you can use.

このように、さまざまな類似検索を利用することができます。

This is probably the most common one that you'll see used.

おそらく、これが最も一般的な使用方法でしょう。

Okay.

なるほど。

We'll look at some more of these things as we focus in, maybe just on vector stores in the future.

今後、ベクトルストアに焦点を当てながら、このようなものをいくつか見ていくことにしましょう。

But here we pass in our query.

しかし、ここではクエリを渡しています。

By default, this is going to return four documents, the four closest documents to this, which is also being embedded with the, with our embedding function from OpenAI out there.

デフォルトでは、これに最も近い4つのドキュメントを返します。

And then if you can see, if we look at the first result that we get back, we can see that, sure enough, social media is mentioned in there.

そして、最初に返ってきた結果を見ると、確かにソーシャルメディアが言及されていることがわかります。

If we look here, we can see social media being mentioned in the first one that we've got back, in fact, it's mentioned multiple times in here.

ここで、最初に返ってきた結果を見ると、ソーシャルメディアが言及されていることがわかります。

So we can see that semantically it's getting good information back.

つまり、意味的に良い情報が返ってきていることがわかります。

And then this is what's going to be passed into our, our language model chain to be used for what we've got going forward with this.

そして、この情報は言語モデルチェーンに渡され、今後の活動に活用される予定です。

All right.

なるほど。

So next up, we want to actually just build a chain.

では次に、実際にチェーンを構築してみましょう。

So we've done now the loading of the document and one of the things I should point out is that even though here I've basically just loaded one document, I could load up multiple documents and feed them in, in the same way.

ここでは基本的に1つの文書を読み込んだだけですが、複数の文書を読み込んで、同じように送り込むこともできます。

And then I would just chunk them all together, which is fine.

そして、それらをまとめてチャンクするのですが、これは結構なことです。

That's something that I could do in here.

そのようなことも可能です。

We can also load up documents and store different metadata with some of the vector stores as well, which is a key technique that we'll look at in the future.

また、ドキュメントを読み込んで、ベクターストアの一部で異なるメタデータを保存することも可能です。

Okay.

なるほど。

Back to the basics for now.

基本に戻りましょう。

So we've got our plain question answering chain.

平易な質問応答チェーンを用意しました。

So this is basically just the load QA chain.

これは基本的にロードQAチェーンだけです。

We're just using OpenAI.

OpenAIを使用しているだけです。

You could try a variety of different models here and the chain type we're using here is stuff.

ここでは様々なモデルを試すことができますが、ここで使用しているチェーンのタイプはstuffです。

What stuff means is that we're literally just going to stuff everything in to one call.

stuffとは、文字通り、すべてを1つの呼び出しに詰め込むということです。

So ideally what we're going to stuff in there is going to be less than 4,000 tokens that we've got in there.

理想的には、この中に詰め込むのは、4,000トークン未満にすることです。

Next up, we've got our prompt.

次に、プロンプトを作成します。

So let me just copy this and we can just paste this in here to be able to see it.

これをコピーして、ここに貼り付けると表示されるようになります。

And we can see that, okay, this is our prompt: use the following pieces of the context, so that's what got returned from the vector store to answer the question.

これがプロンプトです。「次の文脈の一部を使って、質問に答えてください。

At the end, if you don't know the answer, just say you don't know, but just say you don't know and don't try to make up an answer.

最後に、答えがわからなければ、わからないと言えばいいのですが、わからないと言うだけで、答えを作ろうとしないでください。

So this is where all the stuff from our database or a vector store is going to go.

これがデータベースやベクターストアからのすべてのものの行き先となります。

This is the query that we're going to put in there as well.

クエリもここに入れることになります。

And then it's going to answer this.

そして、これに答えようとします。

So you can see here, if I come down and I ask it a query.

このように、下に降りてきてクエリを出すと、このように表示されます。

So let's look at doing this sort of bit by bit.

では、このように少しずつやってみましょう。

So I've got my query and I say, okay, who are the authors of the book?

この本の著者は誰ですか?

And then I pass that query into the vector store in here for a similarity search.

このクエリをベクトルストアに渡すと、類似性検索が行われます。

So that would return back for documents.

そうすると、文書が返ってきます。

Then I pass those documents into our language model chain here.

そして、その文書を言語モデルチェーンに渡します。

And I pass in the query as well.

そして、クエリも同じように渡します。

It comes back with an answer.

すると、答えが返ってきます。

Now the answer is not perfectly correct in here.

この答えが完璧に正しいわけではありません。

Really the authors were Reid Hoffman, which it did get right.

著者はReid Hoffmanで、これは正解でした。

That's pretty good.

これはなかなかいい。

And the second author was GPT-4 looking at the book.

そして、2番目の著者は、本を見るとGPT-4でした。

It's by Reid Hoffman with GPT-4.

GPT-4とリード・ホフマンの本です。

So it's gotten part of it, right?

ということは、その一部は理解できたということですね。

But it's obviously it's taken the word authors that it felt like, oh, it has to be two people.

でも、明らかに著者という言葉から、「ああ、2人じゃなきゃダメなんだ」と感じたんです。

And so that's probably why it hasn't put GPT-4 there.

だから、GPT-4をそこに入れなかったのでしょう。

It's looked for the closest other person that it thought would fit in there.

一番近い人物を探したのです。

Just to show you that what this is actually doing is using that those inputs though, from the vector store.

ただ、これが実際にやっていることは、ベクトルストアの入力を使っているということを示すためです。

Let's say I say the same query, but for the vectors, I pass in a different query.

同じクエリでも、ベクターには別のクエリを渡すとします。

I pass in, has it rained this week?

今週は雨が降りましたか?

Into the, so that's getting four docs back up answering, has it rained this week?

というクエリに、「今週は雨が降りましたか」というクエリに答える4つのドキュメントを入力します。

Then I pass those docs in and the, who is the author of the book query?

そして、その書類を渡して、「この本の著者は誰ですか」という問い合わせをします。

And you can see it comes back with the author of the book is not specified in the given context.

すると、本の著者は指定された文脈では特定されないという答えが返ってきます。

So it means that you want your vector store to return useful information.

つまり、ベクトルストアは有用な情報を返すようにしたい、ということです。

You don't want to make the amount of documents too small.

ドキュメントの量を少なくしすぎないようにすることです。

You don't just want to return the top one because quite often the answer will be in the third, the fourth, sometimes maybe even the fifth one in there.

なぜなら、答えは3番目や4番目、時には5番目のドキュメントにあることもあるからです。

So you want to allow for that when you're doing this.

そのため、このような場合にも対応できるようにしたいものです。

So just to show you with this, you can actually set this, this number.

このように、実際にこの数字を設定することができます。

So I put in all these ones we've been getting four back, but that was just because it was the default.

これまで4個だったものを全部入れてみましたが、これはデフォルトだからです。

If we wanted to actually set it, we could set it over here.

実際に設定したい場合は、ここで設定することができます。

We could change it to six or something like that.

6とかに変更することも可能です。

Obviously with the stuff method, and this is what you're going to see in a second with the stuff method, we can only put in a very finite amount of stuff in there.

stuffメソッドでは、これから説明するように、非常に限られた量のものしか入れることができません。

So if we suddenly say, okay, K equals 20, and I return back the top 20 searches, there's going to be too many tokens.

ですから、いきなり「Kは20に等しい」といって、検索上位20件を返すと、トークンの数が多すぎることになります。

And you get this kind of error.

そして、このようなエラーが発生するのです。

The model's maximum context length is 4,097, however, you requested 5,300.

このモデルの最大文脈長は4,097ですが、あなたは5,300を要求しました。

So you can see that's the kind of error that we would get for this.

このようなエラーが発生することがおわかりいただけると思います。

So one of the ways to get around this is to change the chain type.

そこで、これを回避する方法のひとつが、チェーンの種類を変更することです。

We've looked at these kinds of chain types before in one of the videos.

このようなチェーンの種類については、以前にもビデオで紹介しました。

There are a number of different ones.

いろいろなものがありますね。

Stuff is where you're putting everything in.

Stuffは、すべてを入れるところです。

You've got MapReduce, where you can basically do each one in parallel.

MapReduceは、基本的に1つ1つを並列で行うことができます。

And it will basically run this on each one.

そして、基本的にそれぞれでこれを実行します。

That can turn out to be quite a lot of, you know, that can be quite a lot of calls though, to the API.

しかし、その分APIを大量に呼び出すことになりかねません。

So anytime you can use stuff, it's good to use that because it's going to cost you less.

だから、使えるものはいつでも使った方がいいんです。その方がコストがかからないから。

The MapRerank one, let's look at this.

MapRerankの方ですが、こちらを見てみましょう。

So here I've basically asked it, who are OpenAI?

ここでは基本的に、OpenAIとは何者か?

And I'm asked for 10 queries back.

と質問し、10クエリを返してもらいました。

And so you'll see that what we get back is not just a simple answer we get, because we've told it, okay, return your intermediate steps.

返ってくるのは単純な答えだけではないことがお分かりいただけると思います。

So for each one of these things, it's gone and called the language model in there.

このように、それぞれの質問に対して、言語モデルを呼び出しています。

And you can see that it returns an answer for each of them, or it returns what it got.

そして、それぞれの答えが返され、あるいは得られた答えが返されているのがわかると思います。

And then it scores it as well.

そして、それを採点しています。

So if we look at the final output text, we can see OpenAI is a research organization that develops and shares artificial intelligence tools for the benefit of humanity.

最終的な出力テキストを見ると、OpenAIは人類の利益のために人工知能ツールを開発し、共有する研究組織であることがわかります。

And then if we look at the intermediate steps, we can see each thing that we got back and then the score that it gave.

そして、中間ステップを見ると、返ってきたそれぞれの事柄と、それに付けられたスコアが表示されています。

So it's created an answer based on each one that it's got back.

つまり、返ってきたもの一つひとつをもとに、答えを作り出しているのです。

And it's obviously got, obviously, OpenAI is mentioned a lot in this book, and it's given some of them 80, it's given some of them 90, and it's given some of them a hundred.

この本ではOpenAIのことがたくさん出てきますが、80点、90点、100点のものもあります。

So we can see this one, OpenAI is technology company focused on.

このように、OpenAIはテクノロジーに特化した企業であることがわかります。

So it's gone for probably the two, one or the three 100 ones and merged those together to give us our output there.

そのため、おそらく2つ、1つ、または3つの100を選び、それらを統合して出力しているのでしょう。

If we come and look at the prompt for doing it this way, we'll see that the prompt is actually different.

この方法でプロンプトを見ると、実はプロンプトが異なっていることがわかります。

So we can see here that the prompt is basically showing it that this is the question, this is a help for answer, this is a score, and this is for merging it all together.

プロンプトは基本的に、これが質問、これが答えのヘルプ、これがスコア、そしてこれがすべてをマージするためのものであることを示すものであることがわかります。

It gives some examples that, okay, if something had a higher score, you'd want to pay attention to this when you're getting it back.

また、「より高いスコアを獲得した場合は、そのスコアに注意する必要がある」という例も示されています。

So there's some basic changes that we can see in OpenAI.

このように、OpenAIにはいくつかの基本的な変化が見られます。

There's some basic changes for doing it.

OpenAIの基本的な操作方法には、いくつかの変更があります。

Another way is we can start to put, rather than have separate bits where we've got one thing to query our vector store and one thing to query the LLM, we can put it all in one chain.

ベクトルストアへの問い合わせとLLMへの問い合わせを別々に行うのではなく、すべてを1つのチェーンにまとめることができます。

So here we're basically starting to use the retrieval QA chain.

つまり、ここでは基本的に検索QAチェーンを使い始めているのです。

And what this is going to do is take a, is take a retriever.

これは、リトリーバー(retriever)を使用するものです。

So I'm just taking the retriever that we already had, this doc search that we've set up already, and I'm just saying as retriever, passing in similarity, passing in four for being the number of docs that we want to get back.

すでに持っているリトリーバー、すでに設定したドキュメント検索を、リトリーバーとして、類似性でパス、4でパスして、戻ってきたいドキュメントの数でパスします。

And then now I can basically pass this into this retriever QA from chain type.

そして、これをチェーンタイプからレトリーバーQAに渡すことができます。

So the type I'm going to use is stuff.

ここで使う型はstuffです。

I'm going to pass in the retriever and I'm going to get our source documents back here.

retrieverに渡すと、ソース・ドキュメントがここに戻ってきます。

Now you can see if I ask it, okay, what is OpenAI?

ここで、「OpenAIとは何ですか」と聞いてみるとわかります。

What it will return back is the result, which is going to be the answer.

と尋ねると、結果が返ってきます。

So this is the answer similar to what we got before.

これが、先ほどと同じような答えです。

And then it's going to return the source documents of where it sourced that answer from.

そして、その答えがどこから来たのか、ソース・ドキュメントを返します。

So that we can see here, if I ask it other questions, I'll get things like that.

このように、他の質問をすると、このような答えが返ってくることがわかります。

Now, if I don't want all of that, no problem.

もし、そのすべてを必要としないのであれば、問題はありません。

I can just put in the query and just pass back the result.

クエリーを入れて、その結果を返すだけです。

So here I'm saying, okay, what does GPT-4 mean for creativity?

ここで、「GPT-4はクリエイティビティにとってどんな意味があるのだろう」と考えてみました。

And I can see that, okay, the result GPT-4 can amplify the creativity of humans by providing contextualized search, gives us a whole nice answer for this.

そして、「GPT-4は文脈に沿った検索を提供することで、人間の創造性を増幅させることができる」という結果が、これに対する素晴らしい答えを与えてくれていることがわかります。

I, if we look in the book, if we look at the book, so I've chosen sort of questions based on what was in the table of contents, do it, would it answer some of these questions?

この本を見ると、目次にあったものをもとに質問を選んでいるのですが、それで、これらの質問のいくつかに答えられるのでしょうか?

So I encourage you to play with this so you get a sense of what it can do, what it can't do.

ですから、ぜひこれで遊んでみて、何ができて、何ができないかを感じ取ってください。

One of the tricks for this is that certain things that would be really obvious, I, I'm not actually in the book that much.

このトリックの1つは、本当に明白なことでも、実はこの本にはあまり出てこないということです。

So if you think about who was the book written by, we can see that it's jumping into this, but it doesn't refer a lot to who it was written by.

この本は誰によって書かれたのだろうかと考えると、この本の中に飛び込んでくるのはわかるのですが、誰によって書かれたのかについてはあまり言及されていません。

We've just got this one tiny bit by Reid Hoffman with GPT-4 there.

リード・ホフマンがGPT-4についてほんの少し書いているだけなんです。

So it's certainly got the Reid Hoffman part.

つまり、リード・ホフマンの部分があるのは確かなんです。

But my guess is it didn't think of GPT-4 as being a person that would write a book or as an author of themselves.

しかし、私の推測では、GPT-4が本を書くような人、あるいは自分自身の著者であるとは考えていなかったようです。

Another one I'll show you.

もう1つ、お見せしましょう。

So here's an example where I asked it, okay, what has the last 20 years been like for American journalism?

アメリカのジャーナリズムにとって、この20年間はどのようなものでしたか?

Okay.

という質問です。

So the last 20 years have been difficult for American journalism industry.

アメリカのジャーナリズム業界にとって、この20年間は困難なものでしたね。

And then it goes on to give us a full answer for that.

そして、それに対する完全な答えを教えてくれるのです。

I was curious to see, so if we come in here and we click on journalism, sure enough for American journalism industry, it's been 20 years of mostly bad news and you can see that then it goes through this.

ここでジャーナリズムをクリックすると、確かにアメリカのジャーナリズム業界にとって、この20年間はほとんど悪いニュースばかりであったことがわかります。

So in, in effect, this is summarizing parts from that chapter and what it's gone and done there is it's used the query to go to the vector store to get the information out and then it's pulled it back and put it all together.

つまり、これはその章の一部を要約したもので、クエリを使ってベクトルストアに情報を取りに行き、それを引き戻してまとめています。

And then it's formulated an answer based on the top four results that it got back.

そして、戻ってきた上位4つの結果に基づいて、答えを導き出したのです。

Now, probably all of those results came from that chapter, maybe not, but we could, you could go in and have a look at the, at the outputs and see this.

おそらくこれらの結果はすべてその章から得られたものでしょうが、そうではないかもしれません。

So one of the big things, if you're building something for production, is you'll want to look at what your vector store is returning back.

ですから、生産用に何かを作る場合、ベクターストアが何を返しているのかを確認することが大きなポイントになります。

This way, you can be on top of working out if your problem with the LLM is causing errors or if it's with the vector store not giving you the right information back.

そうすれば、LLMの問題がエラーの原因なのか、それともベクターストアが正しい情報を返してくれないことが原因なのか、すぐに突き止めることができるのです。

How can journalists use GPT gives us nice answers for these, and then another one here.

ジャーナリストはGPTをどのように使えばいいのでしょうか。

Then finally, I asked it a sort of nonsense question.

そして最後に、ちょっとくだらない質問をしてみました。

What is BeagleBard?

BeagleBardとは何ですか?

Now this is not mentioned in the book anywhere.

これはこの本のどこにも書いてありません。

So this is just to show you that if your model's doing well, you should get something where, when it's asked a question that's out of context, it should just say, okay, I don't have a good answer for this or this is not my answer.

つまり、モデルがうまくいっていれば、文脈にそぐわない質問をされたときに、「これにはいい答えがありません」「これは私の答えではありません」と答えるはずだということです。

I don't have a good answer for this or this is not mentioned in the, in any of the contexts given or in any of the documents in there.

これについては良い答えがありません、あるいはこれは文脈や文書のどれにも記載されていません。

So this sums up just basically doing basic Q and A with, with a variety of different chains for doing this.

このように、基本的なQ&Aを、さまざまなチェーンを使って行うことができます。

So again, the key parts here is we have the document splitting, the chunking this up, creating embeddings, embedding it into the vector store.

ここでも重要なのは、文書の分割、チャンク化、埋め込み、ベクターストアへの埋め込みです。

Then for the querying part, we're using LangChain to basically look up the vector store, get the relevant documents, provide that back for an LLM lookup.

そして、クエリの部分では、LangChainを使って、基本的にベクトルストアを検索し、関連するドキュメントを取得し、それをLLMルックアップに提供するのです。

And that will give us the answer back anyway, in the next few videos, we're going to look at more fancier things that you can do with this basic setup.

次のビデオでは、この基本的なセットアップでできる、よりファンシーなことを見ていきます。

And all of these things are going to be generally using some kind of vector store, some kind of tool, some way of querying something and passing the context of that back into the large language model that we've got here.

これらはすべて、何らかのベクトルストアやツール、何かを照会する方法、そしてその文脈を大規模な言語モデルに戻す方法を使うことになります。

Anyway, as always, if you've got questions, please put them in the comments below.

いずれにせよ、質問がある場合は、以下のコメント欄に記入してください。

If you found this useful, please click and subscribe.

もしこの記事が役に立ったら、クリックして購読してください。

I will talk to you in the next video.

次のビデオでお話します。

Bye for now.

それでは、また。



この記事が気に入ったらサポートをしてみませんか?