見出し画像

【OpenAI Assistance APIを活用:ボット作成の完全ガイド】英語解説を日本語で読む【2023年11月24日|@Dave Ebbelaar】

この動画では、OpenAIのAssistance APIを使用してPythonでボットを作成する方法について詳細に説明しています。具体的には、Assistance APIを利用してAirbnbのWhatsAppボットを作るプロジェクトを例に、APIの基本的な使用方法、データ取得、ボットとの対話設定などが紹介されています。また、カスタムロジックを使用してアプリケーションの各部分を組み合わせる方法や、GitHub上で公開されているプロジェクトコードを利用する方法も説明されています。ただし、Assistance APIがまだベータ版であり、完全な制御が難しい可能性や不具合のリスクがあるため、開発者は注意が必要です。
公開日:2023年11月24日
※動画を再生してから読むのがオススメです。


By the end of this tutorial, you will have a complete boilerplate with Python code to work with the assistant API and start building your own bots.

このチュートリアルが終わるころには、アシスタントAPIと連携し、あなた自身のボットを作り始めるためのPythonコードを含む完全なボイラープレートができていることでしょう。

So, if you're a developer and you're interested in working with OpenAI's models, then make sure to stick around.

もしあなたが開発者で、OpenAIのモデルを扱うことに興味があるのであれば、ぜひこのチュートリアルにお付き合いください。

I'm not only going to show you everything that you need to know about the assistant API right now, but I'm also really going to provide you with a framework, a boilerplate in Python that you can use to build applications and bots using the assistant API.

アシスタントAPIについて知っておく必要があることをすべてお見せするだけでなく、アシスタントAPIを使ったアプリケーションやボットを作るために使えるフレームワーク、Pythonのボイラープレートも提供します。

So, let's first start high level with what we're actually going to create.

ではまず、実際に何を作るのか、ハイレベルなところから始めましょう。

I'm going to walk you through the assistant API docs, but really what I found is if you follow all of the instructions over here, which are pretty clear to follow, but then when you want to actually put this into an application and make all of the various pieces communicate with each other, that's really where you need some custom logic.

アシスタントAPIのドキュメントをご案内しますが、実際にはこちらの指示に従ってすべてを進めると、かなりわかりやすいですが、それをアプリケーションに実装し、各部分を連携させる際には、カスタムロジックが必要になるのです。

So, I've created some functions for us that we can go through and that can really help you to put this all together, like I said, to create a framework to reuse this.

ですので、私たちのためにいくつかの関数を作成しました。これらを使って、フレームワークを作成し、再利用するのに本当に役立つことができます。

So, I'm going to walk you through this project, which I will make available via GitHub (link will be in the description), and this is part of a larger project that I'm working on, mainly building an Airbnb WhatsApp bot that can assist hosts by answering frequently asked questions, which we have over here (and we'll get into). But for this part, we are only going to focus on the AI and the retrieval part of the data, so really a common use case for the assistant API.

ですので、私はこのプロジェクトを詳しく説明します。GitHubを通じて利用可能にします(リンクは説明に記載されます)。これは私が取り組んでいる大規模なプロジェクトの一部であり、主にAirbnbのWhatsAppボットを構築して、ホストがよく寄せられる質問に答えることができるようにすることです。こちらにあります(後ほど詳しく説明します)。しかし、この部分では、AIとデータの検索に焦点を当てるだけですので、アシスタントAPIの一般的な使用例です。

Now, the rest of this project, building out the whole bot, will be covered in future YouTube videos.

さて、このプロジェクトの残りの部分、ボット全体の構築については、今後のYouTubeビデオで取り上げる予定です。

So, if you're interested in that, make sure to subscribe so you can stick around for that.

ですので、興味がある場合は、チャンネル登録しておいてください。

So, if you want to follow along, make sure you have an active Python environment running and you have your OpenAI API key ready.

このプロジェクトについて行きたい方は、Pythonのアクティブな環境とOpenAIのAPIキーがあることを確認してください。

You can clone the repository or start from scratch, create your own.

リポジトリをクローンしてもいいし、ゼロから自分で作ってもいい。

There are requirements in here.

ここに必要なものがあります。

For this tutorial, you only need the OpenAI Library.

このチュートリアルでは、OpenAI Library だけが必要です。

Flask won't be needed for this one.

Flaskはこのチュートリアルには必要ありません。

And then we can get into it.

そして、本題に入ります。

So, I start with a simple load over here.

まずは簡単なロードから始めます。

Let's quickly get this up and running.

さっそく実行してみましょう。

Simple load to load the open aai client for us to interact with.

単純なロードで、open aaiクライアントをロードして対話します。

So, this is really the basis and it's the same as you would normally use the OpenAI client and the OpenAI API to work with these models.

これは本当に基本で、通常OpenAIクライアントとOpenAI APIを使ってこれらのモデルを操作するのと同じです。

But now let's look into the assistance that we can now create.

それでは、アシスタンスを作成してみましょう。

You can either do this through the UI, so I can follow this process, click create, all of that.

UIから行うことができるので、このプロセスに従って、作成をクリックします。

But we are going to do this from code obviously.

しかし、私たちは明らかにコードからこれを行うつもりです。

But first, real quick, to get some context on how these assistants are different from the regular openingi, let's quickly go to the playground and show you what's the difference between just the regular chat, which is for example what you would use like one month ago before assistance, and how that compares to if you use the assistance.

しかし、まず、これらのアシスタントが通常のopeningiとはどのように異なるのかについて、プレイグラウンドに行ってみて、通常のチャットとの違いをお見せしましょう。

So, this is all from the UI, from the playground, but this it works the same behind the scenes when you're using the API.

これはすべてUIから、プレイグラウンドからですが、APIを使用しているときは裏側で同じように動作します。

Let's start from the example of the WhatsApp B that we're trying to create, and I'm going to fast forward a little here, but if I ask regular C GPT turbo what is the checking time, it answers over here.

まず、作成しようとしているWhatsApp Bの例から始めましょう。ここでは少し早送りしますが、通常のC GPT turboに「チェックインの時間は何時ですか」と尋ねると、こちらに回答が表示されます。

A checking time is typically between 3: and 4:. So, this is just randomly based on the available knowledge within OpenAI models.

チェック時間は通常3:から4:の間です。つまり、これはOpenAIのモデルの中で利用可能な知識に基づいてランダムに計算されているのです。

Now let's quickly show if I switch to the Airbnb bot that we're going to create and let's ask what is the check-in time.

では、これから作成するAirbnbボットに切り替えて、チェックイン時間を聞いてみましょう。

Then we can see what's going on over here.

すると、こちらで何が起こっているかがわかります。

It's making various requests, it's thinking, and then ultimately it will provide us with the message saying the check-in time for the RB for the Airbnb is 300 p.m.

様々なリクエストをして、考えて、最終的にAirbnbのRBのチェックイン時間は午後300時というメッセージを表示します。

and then we can reference that in here check time is after 3:00 p.m.

そして、このチェックイン時間は午後3時以降です。

This is the fundamental difference that you have to understand over here when you use just the regular API GPT-3 and2 Turbo or the new GPT-4 Turbo model.

これが、通常のAPI GPT-3ターボとGPT-2ターボを使う場合と、新しいGPT-4ターボモデルを使う場合の根本的な違いです。

You're interacting directly with open ai's models without giving it any context.

あなたはオープンaiのモデルと、何の文脈も与えずに直接やりとりしているのです。

So essentially the answer that you will get from the API is the same as you would get using ChatGPT for example.

ですから、基本的にAPIから得られる答えは、例えばChatGPTを使って得られるものと同じです。

But now with the assistance, we can provide tools, namely functions, Code Interpreter, and retrieval.

しかし今、私たちは支援によって、関数、コード・インタープリター、検索といったツールを提供することができます。

And in this tutorial, we'll focus on the retrieval part and see how we can provide our bot, our assistant, with this data.

そして、このチュートリアルでは、検索の部分に焦点を当て、私たちのボット、私たちのアシスタントにこのデータをどのように提供できるかを見ていきます。

In this case, the PDF document.

この場合はPDF文書です。

So that is a really important concept to understand.

それは非常に重要な概念ですので、理解することが重要です。

And if we then look at this from the API side of things, what I was just doing in the playground, so for example doing a simple completion with jet GPT or GPT-3 and a half turbo, we can hear see the same response check-in time generally Etc.

API側から見てみると、先ほど私がプレイグラウンドでやっていたように、例えばジェットGPTやGPT-3.5ターボで単純な補完を行う場合、同じレスポンスのチェックイン時間を見ることができます。

So before the assistance API, this was the only way to interact with OpenAI models.

アシスタンスAPIが登場する前は、これがOpenAIのモデルとやりとりする唯一の方法でした。

And if you wanted to provide extra data, extra context to these models, then you would have to build your own solutions for that.

そして、もしこれらのモデルに追加データや追加コンテキストを提供したいのであれば、そのための独自のソリューションを構築する必要があります。

You would have to set up a vector database, use OpenAIS embeddings for example, and then use a retrieval augmented generation process, for example, which you could build using LangChain.

ベクターデータベースをセットアップし、例えばOpenAISのエンベッディングを使い、LangChainを使って構築できる検索拡張生成プロセスなどを使わなければならない。

That was just a lot of custom code and development that you had to put in place in order to work with these models using your own data.

それはただのカスタムコードと開発であり、独自のデータを使用してこれらのモデルと連携するために必要なものでした。

And I wanted to show you this because you first have to understand how it previously worked to fully understand the benefits but also the downsides of using the assistance API.

アシスタンスAPIを使うことのメリットとデメリットを完全に理解するためには、まず以前の仕組みを理解する必要があるからです。

All right, so now let's continue with the creation of our first assistant.

それでは、最初のアシスタントの作成を続けましょう。

And here you can see the code for that.

こちらにそのコードが表示されます。

So we can use client beta assistance create, and then we have various parameters that we can put in name, instruction, tools, models, Etc.

クライアント・ベータ・アシスタンス・クリエイトを使い、名前、インストラクション、ツール、モデルなど様々なパラメータを設定します。

One important thing to note is we can also put a file in here.

重要なこととして、ここにファイルを入れることもできます。

So here you can see we're giving it a name, Etc, and some instructions on how to behave.

ここでは、名前、その他、動作の指示を与えているのがわかるだろう。

So this is a simple prompt, but then we specify that we want to enable retrieval for this agent.

これは単純なプロンプトですが、このエージェントに対して検索を有効にしたいと指定しています。

And if we enable retrieval, we should also provide it with some data.

そして、検索を有効にするのであれば、いくつかのデータも提供する必要があります。

That is how retrieval works.

これが検索の仕組みです。

It decides with every question, okay, is this a question that I should search my own data for?

それぞれの質問に対して、自分のデータを検索すべきかどうかを判断します。

And the API also has functionality to upload your own data, and we can do that with the following.

また、APIには独自のデータをアップロードする機能もあります。以下の方法で行うことができます。

So we can have client files create.

クライアントファイルを作成します。

and then file, and then we open the following file.

そして、ファイルを開くためにファイルを選択します。

We also give it a purpose.

また、それに目的を与えます。

So, if you look at the project and go to the data folder, you can see the Airbnb fhq PDF document in here.

プロジェクトを見て、dataフォルダに行くと、この中にAirbnb fhq PDFドキュメントがあります。

That is exactly the one that I link to.

これがまさに私がリンクしているものです。

Let's store that.

それを保存しましょう。

Then, we can come over here and say upload file.

そして、ここでファイルをアップロードします。

We're going to put in the data path.

データパスを入力します。

But then, let's see, we are over here.

しかし、ここにいます。

Let's store the file.

ファイルを保存しましょう。

Now, we can upload that and also make sure to store that.

ファイルをアップロードして、それを保存します。

Okay, so now, behind the scenes, we're uploading data to OpenAI.

さて、舞台裏では OpenAI にデータをアップロードしています。

We now have a file which also has an ID.

IDを持つファイルができました。

So, we can use that right now with the creation of our assistant.

このファイルをアシスタントの作成に使うことができます。

For context, this is something that you would do, for example, using UI.

コンテキストとしては、これは例えばUIを使って行うことです。

Coming over here and then uploading a file.

ここに来て、ファイルをアップロードします。

Let's see, we can come over here to files and you can now see I just uploaded this one.

ここにファイルに移動して、これをアップロードしたのが見えるかと思います。

I also click on this and I can see my file here.

これをクリックすると、ここにファイルが表示されます。

That is data management and how you can do it through the UI and through the API.

それがデータ管理と、UIとAPIを介してそれを行う方法です。

So, now that we have that over here, we can come to our create assistant.

さて、ここまでできたら、次は作成アシスタントです。

Let's look at what's going on over here.

ここで何が起こっているのか見てみましょう。

For the sake of demonstration, create a number two because I already have this one.

デモンストレーションのために、既にこれを作成しているので、2番を作成します。

What we now have to do is we can run the create assistant and then put the file we've just created put that in here.

今度はアシスタントを作成し、先ほど作成したファイルをここに入れる必要があります。

This should still be in memory.

これはまだメモリ内にあるはずです。

Let's see, file, we still have it in memory and create the assistant.

それでは、ファイルをメモリに保存して、アシスタントを作成しましょう。

So, within the code, this is a one-time process and that's why I'm doing it kind of like flexible over here.

コード内では、この処理は1回限りなので、ここではフレキシブルにやっています。

So, later on, we don't need that as we start to interact with this.

そのため、ここでは柔軟性を持たせているのです。

This is something you can either do like in a notebook or in a separate file normally.

これはノートブックに書くか、別のファイルに書くかのどちらかだ。

But let's run all of this and see what's happening.

では、これをすべて実行して、何が起きているのか見てみましょう。

So, we are now creating our assistant and now we have WhatsApp Airbnb bu assistant.

これでアシスタントが作成され、WhatsApp Airbnb buアシスタントができました。

All right, so that is really the first step, creating your assistant and providing it with data.

これが最初のステップで、アシスタントを作成し、データを提供します。

Now, if you're new to AI or data projects or if you're a developer that wants to get a better understanding of these AI applications, then a good resource that you can check out is Project Pro.

もしあなたがAIやデータプロジェクトに慣れていないなら、あるいはAIアプリケーションをもっと理解したい開発者なら、Project Proをチェックするとよいでしょう。

Project Pro is a curated library of end-to-end projects and they have more than 250 completed projects in here, all related to data and artificial intelligence.

Project Proは、エンド・ツー・エンドのプロジェクトのキュレーション・ライブラリで、ここには250以上の完成したプロジェクトがあり、すべてデータと人工知能に関連している。

So, we have machine learning, data science, deep learning, NLP projects, and for example, here is one NLP project to build and fine-tune large language models.

機械学習、データサイエンス、ディープラーニング、NLPプロジェクトがあり、例えば、大規模な言語モデルを構築し、微調整するNLPプロジェクトがあります。

Here's one on language generation.

こちらは言語生成に関するプロジェクトです。

So, with these projects, you not only get the complete code written by industry experts, but you also get video walkthroughs, documentations, and even architecture diagrams.

これらのプロジェクトでは、業界のエキスパートによって書かれた完全なコードを入手できるだけでなく、ビデオによるウォークスルーやドキュメント、さらにはアーキテクチャ図も入手できます。

So, this is really one of the best resources to go through if you want to get a better, deeper understanding of data science and artificial intelligence.

ですので、データサイエンスと人工知能のより良い理解を得たい場合には、これが最高のリソースの一つです。

Tutorials on YouTube will only get you so far.

YouTubeのチュートリアルでは、ここまでしか理解できません。

If you really want to get in-depth, follow these complete end-to-end projects.

本当に深く知りたいなら、これらの完全なエンド・ツー・エンドのプロジェクトに従いましょう。

Then, make sure to check out Project Pro.

それから、Project Proを必ずチェックしてください。

The link will be in the description.

リンクは説明文にあります。

All right, so now we've created an assistant.

さて、アシスタントを作成しました。

But how do we interact with it?

しかし、どうやってアシスタントを操作するのでしょうか?

That brings us to the following concept, and that is threads and messages.

それによって次の概念に移りますが、それはスレッドとメッセージです。

This is important to understand.

スレッドとメッセージです。

If you want to dive deeper into this, make sure to check out the docs.

もっと深く知りたい場合は、ドキュメントをチェックしてください。

What OpenAI has done is they have allowed developers to very easily keep track of different conversations and the messages within all of that.

OpenAIが行ったことは、開発者がさまざまな会話とその中のメッセージをとても簡単に追跡できるようにしたことです。

A thread is basically a whole history of messages.

スレッドは基本的にメッセージの履歴全体です。

I think the easiest way to describe this is if you go to ChatGPT, you have your account, but on that, you also have the different conversations that you have.

これを説明する最も簡単な方法は、ChatGPTにアクセスすると、自分のアカウントがあり、そのアカウントでさまざまな会話をすることです。

Each conversation there is a different thread, which is different messages.

それぞれの会話は異なるスレッドで、異なるメッセージです。

By separating threads using a unique ID, developers can very easily keep track of that, not only between different conversations with the same user, but also, of course, between different users that are on our application.

一意のIDを使ってスレッドを分けることで、開発者は、同じユーザーとの異なる会話間だけでなく、もちろん、アプリケーションを利用している異なるユーザー間でも、非常に簡単に追跡することができます。

For example, in the context of the WhatsApp bot, when I send a message to the bot using my number, we, of course, want to keep that separate from the messages you are sending to the bot.

例えば、WhatsAppボットの場合、自分の番号を使ってボットにメッセージを送信する場合、もちろん、あなたがボットに送信しているメッセージとは別に管理したい。

Let's see how to do all of that.

では、その方法を見ていきましょう。

We're going to work on a function that is called generate response, and we're going to start pretty simple and build that out.

これからレスポンスの生成という関数に取り組みますが、かなりシンプルなものから作っていきます。

Initially, we cannot generate a response with this because we first have to create the thread for every new user.

最初は、新しいユーザーごとにスレッドを作成しなければならないので、これでレスポンスを生成することはできません。

And then, add a message to it so it's pretty straightforward.

そして、メッセージを追加するだけなので、非常に簡単です。

We start with a client beta threats create that will create a threat.

まずはクライアントのベータ版脅威の作成から始める。

So, this is now created and we can look at that.

これが作成されたので、それを見てみることができます。

It has an ID created at and it has some metadata.

作成されたIDがあり、いくつかのメタデータがあります。

We can also get the U uh get the thread ID by calling the ID.

IDを呼び出すことで、スレッドIDを取得することもできる。

This will allow us to now have a variable that just has this threat ID, alright?

これにより、この脅威IDだけを持つ変数が作成されます。

So now, we've created essentially a single conversation that we can use to send messages to, alright?

これにより、メッセージを送信するために使用できる単一の会話が作成されます。

And now, next, we are going to add a message to that threat.

そして次に、その脅威にメッセージを追加する。

That is using beta threats message create.

これはβ脅威メッセージの作成を使います。

So, we'll start with a user role.

まずはユーザー・ロールから。

Whenever someone sends a message to the bot, this is added as a user role.

誰かがボットにメッセージを送ると、これがユーザーロールとして追加されます。

And then, we put in the message.

そしてメッセージを入れます。

So, we can do that over here and it takes in the message body.

そうすれば、メッセージの本文を取り込むことができます。

So, let's actually uh store that and take again.

では、実際にそれを保存して、もう一度取り込んでみましょう。

Let's take the example of the check-in time.

チェックイン時間の例を見てみましょう。

Where is it?

それはどこですか?

Over here.

ここです。

So, we have a body and we are going to run this.

本文があるので、これを実行します。

So, let's do that and we now have a message object which we can look at.

これでメッセージ・オブジェクトができあがりました。

This is a threat message.

これは脅迫メッセージです。

It also has an idea um, but here you can see it does not have an assistant yet, nor does it have an answer.

また、アイデアもありますが、ここではまだアシスタントも回答もありません。

And that is because this is just a way to prepare the message before sending it to the AI model.

というのも、これはAIモデルに送る前にメッセージを準備するためのものだからです。

So, and now if we actually want to run this assistant, let's get to the next steps.

では、実際にこのアシスタントを実行したいのであれば、次のステップに進みましょう。

We get into runs and run steps.

実行と実行のステップに入ります。

I have a follow-up function.

フォローアップ機能があります。

Let's look at this and this is called run assistant.

これはrun assistantと呼ばれています。

It's a lot of code, so let's really go through it step by step so you understand what's going on because this is probably the most complex part.

おそらくここが一番複雑な部分なので、何が起こっているのか理解できるように、本当に一歩一歩見ていきましょう。

Because this is where everything comes together.

なぜなら、ここが一番複雑な部分だからです。

So, remember that we have created a threat also with a threat ID.

脅威IDを持つ脅威も作成したことを思い出してほしい。

So, that is to keep track of our conversations and our messages.

これは会話とメッセージを追跡するためのものです。

We also have a message, What's the check-in time?

また、メッセージもあります。「チェックインの時間は何時ですか?」

And now, we have to send this to the assistant that has context of our data.

というメッセージがあり、これをデータのコンテキストを持つアシスタントに送らなければなりません。

So, access to the PDF document.

つまり、PDFドキュメントにアクセスする。

And then, it should reply based on that.

そして、それに基づいて返信するのです。

So, let's see what we have to do.

では、何をすればいいのか見てみましょう。

First, we have to retrieve our assistant again.

まず、アシスタントを再度取得する必要があります。

And we use that using the retrieve function and calling the ID into this.

そしてretrieve関数を使い、IDをこの中に呼び出します。

So, we can load that.

それで、それを読み込むことができます。

And then, you can see we have the assistance.

そしてアシスタントのIDを取得します。

So, how do you get the ID?

では、IDはどうやって取得するのでしょうか?

Well, you either get it from the assistant variable that we created in the previous step, or you go to your assistants and you can see the ID over here.

それでは、前のステップで作成したアシスタント変数から取得するか、アシスタントに移動してIDを確認することができます。

So, two ways to capture that information.

つまり、情報を取得するには2つの方法があります。

That's the first step.

これが最初のステップです。

Then, we're going to retrieve our threat, and this also requires an ID that we put in here.

それから、脅威を取得します。これにはここに入力したIDが必要です。

How do you get that?

IDはどうやって取得するのですか?

Well, also from the previous step.

これも前のステップから。

So, we get an assistant and we get a threat.

アシスタントを得て、脅威を得る。

And again, to check that that is correctly stored, we can see assistance ID, threat ID, all right.

このIDが正しく保存されていることを確認するために、アシスタントのIDと脅威のIDを確認します。

And now, this is where we can bring everything together.

そして、ここですべてをまとめることができます。

So, we can run all of this and put in the threat ID and the assistance ID.

それでは、これを実行し、脅威IDとアシスタンスIDを入力しましょう。

And now, since we've added the message to this unique threat ID, now whenever we run this, that is what at when everything starts to come together and when the interaction with the AI model will be made.

そして、この一意の脅威IDにメッセージを追加して、これを実行するたびに、すべてが一つにまとまり、AIモデルとの相互作用が行われるようになります。

So, this is pretty complex.

これはかなり複雑ですね。

There are actually a lot of things going on behind the scenes to bring all of this together.

実は、これらすべてをまとめるために、舞台裏ではさまざまなことが行われている。

So, I really want to go step by step here and show it with a real-world example, all right.

ですから、ここでステップ・バイ・ステップで実例を挙げて説明したいと思います。

And we're not done yet because the thing is, if you just run this, we can see nothing happens in terms.

というのも、これを実行しただけでは、何も起こらないことがわかるからです。

There is no response.

何の反応もない。

I can look at this and it just has a ID.

IDが表示されているだけです。

It has the assistance ID canceled.

アシスタンスIDがキャンセルされています。

It has all the information completed at created at.

で作成されたすべての情報を持っています。

And that is because how they handle this run object.

そしてそれは、このラン・オブジェクトをどのように扱っているかということです。

And if we then come back to how this actually works, it explains what's going on over here.

そして、これが実際にどのように機能するのかに戻ると、ここで何が起こっているのかが説明されている。

And again, you should read through this to get a better understanding, but this piece is very important.

ここでも、理解を深めるために一読していただきたいのですが、この部分は非常に重要です。

In order to keep status of your run date, you have to periodically retrieve the Run object.

走行日のステータスを維持するためには、定期的にRunオブジェクトを取得する必要があります。

So, you have to check this object.

ですから、このオブジェクトをチェックしなければなりません。

If I come over here without retrieving this one more time and looking at the status, it still says cute because this is when you initiate the run.

もう一度これを取得せずに、ステータスを確認すると、まだ「cute」と表示されています。これは実行を開始するときの状態です。

But now, and this is where this piece of logic comes in, it's a little while loop to itly check the status of the Run object.

しかし、ここでこのロジックの一部が登場します。これはRunオブジェクトのステータスをチェックするための小さなwhileループです。

So, this has run for a couple seconds already.

これはすでに数秒間実行されています。

Right now, I'm now going to skip to this line and run just this part.

この行をスキップして、この部分だけを実行します。

So, not in the while loop.

つまり、whileループの中ではない。

We'll check that in a bit.

それは後で確認しよう。

But if I retrieve this one more time just visualize the run, the Run object, we can see going all the way over here, all right.

しかし、もう一度これを取得し、実行を視覚化すると、Runオブジェクトがここまで進んでいるのがわかります。

So, now we have status completed.

これでステータスが完了しました。

And that is something we can access by calling run.status.

run.statusを呼び出すと、このステータスにアクセスできる。

You can see completed.

completedと表示されます。

So, this is something weird right now in the API, and it's definitely because it's still in beta.

というわけで、これはAPIがまだベータ版だからなのですが、ちょっと変な感じです。

It also says we're plan to add support for streaming to make this simpler in the near future.

また、近い将来ストリーミングのサポートを追加して、よりシンプルにする予定だとも書いてある。

So, right now, you literally have to build in some mechanism.

だから、今は文字通り、何らかのメカニズムを組み込む必要がある。

And I right now put in a time.sleep to check every like half second whether it's already completed using this while loop.

僕は今、time.sleepを入れて、このwhileループを使って半秒ごとに実行が完了したかどうかをチェックしている。

So, this while run status is not completed, it keeps checking the Run status.

つまり、実行ステータスが完了していない間は、実行ステータスをチェックし続けるのだ。

So, this is a little workaround that you have to do right now that I've not seen in any other tutorials yet.

ですので、これは現在の回避策ですが、他のチュートリアルではまだ見たことがありません。

So, now let's see what we got over here.

では、何ができたか見てみましょう。

So, the run is completed, which would cause it to break out of the while loop.

実行が完了し、whileループから抜け出すことができました。

And now, we can get the messages.

これでメッセージを受け取ることができる。

So, how does that work?

さて、どのように動作するのでしょうか?

Well, we have beta threats messages list, and then we put in our same thread ID again.

さて、β脅威メッセージ・リストがあるので、同じスレッドIDをもう一度入れる。

So, remember, the thread stores all of the messages.

スレッドにはすべてのメッセージが保存されている。

So, let's get all of them and see what's in there.

スレッドにすべてのメッセージが保存されていることを思い出してください。

And then we have a message object, and here is all the information that is stored within.

メッセージ・オブジェクトがあり、ここにすべての情報が格納されています。

Uh, the messages.

メッセージです。

So, this could be a lot, and to get the latest value, you have to parse it like this.

最新の値を取得するには、このようにパースする必要があります。

So, this is all JSON data, and we get the data first element, then context, and then if we print a new message, we can see over here we get the same answer.

ですので、これはすべてJSONデータです。最初にデータの要素、次にコンテキストを取得し、新しいメッセージを表示すると、同じ回答が得られることがわかります。

The checking time for the Airbnb is after 300 p.m.

Airbnbのチェック時間は午後300時以降です。

based on the time over here, so that works out awesome.

こっちの時間を基準にすると、うまくいく。

All right, so that was the full loop from creating an assistant, giving it tools and data, and then getting to a user response.

アシスタントを作成し、ツールやデータを与え、そしてユーザーの反応を得るまでの完全なループでした。

But now, let's take that one step further and see how we can bring all of this together in an application framework.

では、もう一歩進めて、アプリケーションフレームワークでこのすべてをどのようにまとめられるか見てみましょう。

All right, and I'm going a little fast now because there's a lot to cover, and you can also check out the code yourself, follow the comments, and it should all be pretty straightforward.

では、これからは少し早く進めます。カバーする内容がたくさんありますので、コード自体を確認し、コメントに従って進めれば、すべてがわかりやすいはずです。

But what we got going on over here, we have separate pieces of functions.

ここでは、別々の関数のピースがあります。

First of all, for threat management, for generating responses, and then for running assistance.

まず、脅威の管理、レスポンスの生成、そしてアシスタンスの実行だ。

And we want to do this to enable the following.

そして、次のことを可能にするために、これを実行したい。

So let's think of this example again from the WhatsApp B, and let's say we have John and we have Sarah, and they, uh, they both have their unique WhatsApp phone number, which is a unique identifier for them.

では、WhatsApp Bの例から再度考えてみましょう。ジョンとサラがいるとします。彼らはそれぞれユニークなWhatsAppの電話番号を持っており、それが彼らのユニークな識別子です。

So in this case, 1 2 3 4 5 6 for Sarah, and they have different questions.

この場合、サラは1 2 3 4 5 6で、2人は異なる質問を持っている。

Now we want to ensure that the threats are handled correctly, and this is something that out of the box is not possible because when you create a threat, it uses a uniquely generated ID.

脅威が正しく処理されていることを確認したいが、脅威を作成する際に一意に生成されたIDを使用するため、そのままでは不可能である。

So if you later want to reference that and say, for example, this threat belongs to John, you have to create some kind of a database mechanism to keep track of that.

というのも、脅威を作成する際には、一意に生成されたIDが使用されるからである。そのIDを後で参照し、例えばこの脅威はジョンのものだと言いたい場合には、それを追跡するための何らかのデータベース機構を作らなければならない。

So when the next time when John with his WhatsApp number sends a message, you know which unique threat ID to retrieve from your database.

そうすれば、次にジョンがWhatsApp番号を使ってメッセージを送信したときに、データベースからどの一意の脅威IDを取り出せばいいかがわかる。

So that's really what we're going to focus on and what I'm going to show you right now.

これが、私たちが今注目していることであり、これからお見せすることです。

So that is basically the only thing that's added functionality for that, but that really is what bridges the gap between just a demo from the quick start and turning this into something useful.

それが追加された機能ですが、それがクイックスタートからデモだけでなく、実用的なものに変えるものです。

And for the database solution, we're going to use a very simple Python library called shelf, and that is a persistent dictionary-like object.

データベースのソリューションとしては、shelfという非常にシンプルなPythonライブラリを使います。

And it's a very easy way to store a dictionary that we can use for later.

これは永続的な辞書のようなオブジェクトで、後で使う辞書を保存するとても簡単な方法です。

So I will show you what that looks like, but you can just as well use any kind of data storage that you would like.

このライブラリがどのようなものかお見せしますが、どのようなデータ・ストレージでも同じように使うことができます。

So we have two functions that we're going to store.

これから格納する関数は2つあります。

That is, check if the threat exists and also to store it.

つまり、脅威が存在するかどうかをチェックする機能と、脅威を保存する機能です。

And we're going to base this on the WhatsApp ID in this case.

今回はWhatsApp IDをベースにします。

So we're going to reference whenever John sends a message, we're going to check, hey, does this WhatsApp ID, this WhatsApp number, is it already present in our database?

ジョンがメッセージを送信するたびに、このWhatsApp ID、つまりWhatsApp番号がデータベースに存在するかどうかを確認します。

If not, we'll create a unique threat for it.

もしそうでなければ、ユニークな脅威を作成します。

And that is what is handled over here.

それがこちらで処理されることです。

So you can see, if a threat does not exist, create it and store it.

つまり、脅威が存在しなければ、それを作成して保存します。

So we'll check that.

それをチェックします。

So if threat ID is none, we'll first call this function, check if threat ID exists.

脅威IDが存在しない場合、まずこの関数を呼び出し、脅威IDが存在するかどうかをチェックします。

If not, we'll create it and we'll store it.

存在しない場合は、脅威IDを作成して保存します。

And you can see here, store threat and how we're doing it.

ここで、脅威を保存している方法とその実行方法がわかります。

We open threats_DB, which will show up over here in this in our project once we start to run this.

threats_DBを開き、これを実行するとプロジェクトに表示されます。

All right, so let's now see what we got going on over here.

では、ここで何が起こっているのか見てみましょう。

So let's quickly store all of this.

早速、これをすべて保存しましょう。

And now let's say we are John, and John is new to, he just booked the Airbnb, and he wants to know what is the check-in time.

私たちがジョンで、ジョンはAirbnbを予約したばかりの新人で、チェックイン時間を知りたがっているとします。

So let's see, I've included some print statements to also show the name.

そこで、名前を表示するためにprintステートメントをいくつか用意しました。

And now let's run this to see what's going on.

では、これを実行して何が起こっているのか見てみましょう。

And here we can see, creating new threat for John with WhatsApp ID 123.

ここでは、WhatsApp ID 123でJohnの新しい脅威が作成されています。

So it correctly identified that there is no database, which there is right now.

データベースが存在しないことが正しく認識されています。

You can see threats_DB is created, and then it generated the message.

threats_DBが作成され、メッセージが生成されました。

So here's the message.

これがそのメッセージだ。

And then finally, it says to John, Checking time for rbb is after 3:00 p.m.

そして最後に、ジョンに対して「rbbのチェック時間は午後3時以降です。

And it also links to the source, so that's pretty awesome.

そして、ソースにもリンクされているので、それはかなり素晴らしいです。

But now let's see what happens if Sarah sends a message.

サラがメッセージを送ったらどうなるか見てみよう。

Let's run that, and again it correctly says, Creating new threat for Sarah.

それを実行してみましょう。そして、再び「Sarah」に対して新しい脅威を作成すると正しく表示されます。

And again, we get the response, and this is now sent to Sarah.

そしてまたレスポンスが返ってきて、これがサラに送信される。

But now the cool thing over here is if we take another message and let's say John asks, Hey, what was my previous question?

しかし、ここでクールなのは、別のメッセージを受け取って、ジョンが「おい、前の質問は何だったんだ?

So it says, Now retrieving the existing threat for John because it's already in a database.

という質問に対して、Johnは「今、Johnの既存の脅威を検索しています。

And to John, your previous question was asking about the check-in time.

そしてジョンに、あなたの前の質問はチェックインの時間について尋ねていました。

Okay, awesome.

オーケー、素晴らしい。

But now what if Sarah asks that same question?

でも、今度はサラが同じ質問をしたら?

And then to Sarah, your previous question was about the PIN for the lock box.

そしてサラに、あなたの前の質問は、ロックボックスの暗証番号についてでした。

So this is pretty sick, right?

ということは、これはかなり病んでいますよね?

And if you have some development experience, you know probably what's going on behind the scenes and how much work it would take to properly set that up, that user management to keep track of all of the messages in the cover conversations and integrating all of the AI models with the data.

開発経験のある方なら、舞台裏で何が起こっているのか、そしてそれを適切にセットアップするためにどれだけの労力が必要なのか、カバー会話のすべてのメッセージを追跡するためのユーザー管理、そしてすべてのAIモデルをデータと統合するためにどれだけの労力が必要なのか、おそらくご存知でしょう。

And we now have that in about 100 lines of code.

そして今、私たちは約100行のコードでそれを実現しました。

So this is a complete OpenAI surface that you can use to interact with the assistants.

これは、アシスタントと対話するために使用できる完全なOpenAIサーフェスです。

And this is something I haven't seen in other tutorials yet.

これは他のチュートリアルではまだ見たことがないものです。

So most people will just cover the quick API steps over here, but this is just the basis and you really need functionality like this to bring it all together.

ほとんどの人は、APIの簡単なステップをカバーするだけですが、これは基礎に過ぎず、すべてをまとめるにはこのような機能が本当に必要なのです。

But now an important question for you as a developer, should you go all in on this assistance API and use this to build your AI applications?

しかし今、開発者としてのあなたにとって重要な疑問があります。このアシスタンスAPIに全力を注ぎ、AIアプリケーションを構築するためにこれを使うべきなのでしょうか?

And to answer that, I'm not sure.

その答えは、私にはわからない。

I would say I'm a bit hesitant right now to convert my existing and ongoing applications that I'm building for clients to this new framework, to this new API.

私は今、クライアントのために作っている既存の、そして現在進行中のアプリケーションを、この新しいフレームワークやAPIに変換することに少し躊躇している。

And my current method is to build most of this functionality that I've just showed to you all custom.

私の現在の方法は、皆さんにお見せしたような機能のほとんどをカスタムで構築することです。

So mostly using length chain vector databases, performing the similarity search and the embeddings, all of that.

長さの連鎖したベクトル・データベースを使い、類似性検索と埋め込みを実行する。

So it's a lot more work, but I already have the code and the frameworks for that in place.

ですから、より多くの作業が必要ですが、そのためのコードとフレームワークはすでに用意してあります。

So once you got that up and running, it's pretty easy to replicate.

ですから、一度それを稼働させれば、複製するのはとても簡単です。

And the upside is that you have a lot more control over what's going on.

さらに、何が起こっているかをよりコントロールしやすくなるという利点もある。

So with this approach, you're uploading your data to OpenAI and using it from here for retrieval.

このアプローチでは、データをOpenAIにアップロードし、ここから検索に使用します。

So for most companies, most clients, this is a no-go.

ですから、ほとんどの企業、ほとんどのクライアントにとって、この方法はお勧めできません。

And also from a data management perspective, this is definitely not ideal.

また、データ管理の観点からも、これは間違いなく理想的ではありません。

That combined with the fact that this is still in beta version of it, and while testing it, I have found some scenarios where it did some weird things, where it said things like, Hey, I cannot answer that, or I don't have information on that, where it was literally in the PDF document that I provided it with.

それに加えて、これはまだベータ版ですし、テスト中にいくつかのシナリオで奇妙な動作をすることがありました。例えば、「それについては答えられません」とか、「それについての情報はありません」とか、PDFドキュメントに明記されているにもかかわらず、そう言ってしまうことがありました。

And this is something that has never happened to me with my Pinecone setup, for example.

これは、例えば私のPineconeのセットアップでは起きたことのないことです。

And it was found in some logs that they apparently used the quadrant factor database.

そして、どうやら四分位因子のデータベースを使用していることが、いくつかのログから判明しました。

So there are definitely some things going on behind the scenes that you don't have full control over.

つまり、舞台裏では、あなたが完全にコントロールできないことが起こっているのだ。

And of course, when you build custom software, when you build custom applications, that's one thing you do want.

もちろん、カスタム・ソフトウェアを作るとき、カスタム・アプリケーションを作るとき、それは望むことのひとつだ。

You do want that full control.

完全なコントロールが欲しいのです。

And as of right now, I feel like it's really a step in the right direction to make things easier for developers.

今のところ、これは開発者にとってより簡単なものにするための正しい方向への一歩だと感じています。

But right now, I feel like it's still not there yet.

でも、今はまだ、そこまでには至っていないと感じている。

That said, I think it's important to keep a close eye on this API and at least understand how it works, finding your assistance with functions and enCode Interpreter and retrieval and potentially other tools.

とはいえ、このAPIを注視し、少なくともどのように機能するのかを理解し、関数やenCode Interpreter、検索、そして潜在的に他のツールによる支援を見つけることが重要だと思います。

It's definitely the direction that we're heading and the concepts that are integrated within this API.

このAPIに統合されているコンセプトは、間違いなく私たちの目指す方向です。

So effectively managing threats and messages, those are all concepts that you have to understand.

脅威やメッセージを効果的に管理するためには、これらの概念を理解する必要があります。

So I would still recommend diving into this, play around with this.

ですから、このAPIに飛び込んで、実際に遊んでみることをお勧めします。

But for full-scale applications, like I said, I would still be hesitant and I would still revert to, for example, looking into LangChain and choosing your own factor database and build it out like that.

しかし、本格的なアプリケーションの場合は、先ほども言ったように、私はまだためらいがあり、例えばLangChainを調べたり、独自のファクターデータベースを選択したりして、そのように構築することに戻ると思います。

All right, so now you have a good understanding of how the assistance API works.

さて、これでアシスタンスAPIがどのように機能するかは理解できただろう。

And if you want to learn more about this and want to learn more about working with data, then make sure to check out my free group, Data Alchemy.

このAPIについてもっと知りたい、データを扱うことについてもっと学びたいという方は、私の無料グループ「Data Alchemy」をぜひチェックしてください。

Link will be in the description.

リンクは説明文にあります。

We have 4.5k members in here.

ここには4.5千人のメンバーがいます。

The community is very active.

コミュニティはとても活発です。

We discuss all things AI, but also there is a classroom in here where I teach you the Alchemy codex, which is my unique way of working with data and Python, especially good for beginners, but even for seasoned developers switching to data science and artificial intelligence.

私たちはAIに関するあらゆることを議論していますが、アルケミーのコーデックスを教える教室もあります。これは、データとPythonを扱う私のユニークな方法であり、特に初心者に適していますが、データサイエンスや人工知能に転向したベテラン開発者にも適しています。

So make sure to check that out.

ぜひチェックしてみてください。

It's completely free, like I've said.

完全に無料です、前にも言った通りです。

Link in the description.

説明文にリンクがあります。

and then, that will be, uh, it for this video.

それでは、このビデオはこれで終わります。

Thank you for watching.

ご視聴ありがとうございました。

As always, uh, I would really appreciate it if you like this video and also make sure to subscribe to stick around and make sure you don't miss future updates.

いつものように、この動画が気に入っていただけたら本当に嬉しいですし、チャンネル登録もお願いします。これからのアップデートを見逃さないようにしてください。

For example, the complete WhatsApp Bot video that I have in the pipeline.

例えば、WhatsApp Botの完全版ビデオを準備中です。

And for those of you that have been following me for a while, my apologies for being away for some time from YouTube.

長い間YouTubeから離れていたため、私をフォローしてくださっている方々にはお詫び申し上げます。

It's been some weeks since I've uploaded, um, I did some traveling, I did some moving, my actually my girlfriend moved in with me, and it was just a lot combined with all the client projects that I'm currently working on with my business, Data Lumina.

旅行したり、引っ越したり、実はガールフレンドが同居することになったり、そして今私が取り組んでいるData Luminaのクライアント・プロジェクトが重なったりと、いろいろなことが重なり、YouTubeにアップする時間がなくなってしまいました。

So, there was a lot of stuff going on and unfortunately, I didn't have enough time to put out YouTube videos.

だから、いろいろなことが起こっていて、残念ながらYouTubeのビデオをアップする十分な時間がなかった。

But I freed up some time again, I'm back at it, so hopefully we'll get back to some consistent uploads because there's just so much going on in the world of AI and it's really exciting.

でも、また時間ができたので、また再開します。AIの世界では本当にたくさんのことが起こっていて、とてもエキサイティングなので、またコンスタントにアップロードできるようになるといいですね。

Also, sometimes hard to keep up with, you know, one week you learn this specific library and this tool and then the next week it's all obsolete by some new update.

というのも、AIの世界では本当にたくさんのことが起こっていて、本当にエキサイティングなんだ。それに、ある週は特定のライブラリやツールを学んだのに、次の週には新しいアップデートで全部使えなくなっちゃったりして、ついていくのが大変なこともある。

But yeah, that's just the tradeoff, uh, of being on really at the forefront of this AI Revolution.

しかし、それはAI革命の最前線にいることのトレードオフです。

And yeah, like I've said, it's really exciting.

そして、私が言ったように、本当にエキサイティングだ。

Again, thank you all for watching and see you in the next one.

また、ご視聴いただきありがとうございます。次回もお楽しみに。


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