字幕表 動画を再生する
Hey everyone, in this video I will be showing you how to use Google's Gemini AI API along with Python.
皆さん、こんにちは!このビデオでは、GoogleのGemini AI APIとPythonの使い方を紹介します。
So let's get started.
では、始めよう。
So first thing just Google Google AI studio.
そこでまず、Google AI studioでググってみてほしい。
You'll be getting the first link from aistudio.google.com Select this and here make sure that you are signing in with your Google account and here to the top you'll be having this get code.
aistudio.google.comから最初のリンクが表示されます。これを選択し、Googleアカウントでログインしていることを確認してください。
So if you click here you'll be having the code for different languages.
ですから、ここをクリックすれば、さまざまな言語のコードが表示されます。
In our case we are going to use it for Python and as you can see here we are getting a model name.
この場合、Pythonに使用することになるが、ご覧のようにモデル名を取得している。
So if you want to change the model name you don't have to like cut and paste you can just change the model by clicking this drop-down.
モデル名を変更したい場合は、カット・アンド・ペーストする必要はなく、このドロップダウンをクリックしてモデルを変更することができます。
For example let me go with Gemini 2.0 flash and if I click on get code we'll be having that particular model name added over here.
例えば、ジェミニ2.0フラッシュで、コードを取得をクリックすると、そのモデル名がここに追加されます。
So we can just copy this and I'm just having here an empty .py file.
これをコピーして、空の.pyファイルにしておきます。
So I'll just go and paste my code over here.
それでは、私のコードをここに貼り付けます。
So this is the code basically and now we need to install two Python packages.
これが基本的なコードで、あとは2つのPythonパッケージをインストールする必要がある。
One is the Google generative AI Python package because as you can see here we are importing this one over here.
ひとつはGoogleのジェネレーティブAI Pythonパッケージで、ここにあるように、これをインポートしている。
So that's the first package and another one is going to be .env because here we will be using the Gemini AI API key.
これが最初のパッケージで、もう1つは.envになる。ここではGemini AI APIキーを使うからだ。
So for that we will be storing that in a .env file.
そのため、.envファイルに保存することになる。
So this is also required.
だから、これも必要だ。
So you can just go and copy this.
だから、これをコピーしてくればいい。
Yeah you can just pause the video and type this out and this is the second package.
ビデオを一時停止して、これをタイプしてください。
Yeah now we are done with both.
ああ、これで両方とも終わった。
So now let's just go and get our Gemini AI API key.
それでは、Gemini AI APIキーを取得してみよう。
So for getting that here you will be having get API key option.
そのため、APIキーを取得するオプションが用意されている。
Select it and here select on create API key and since I'm having already so many projects I don't have to create a new project and if you don't have any existing projects you need to click on this and then create an API key.
もし既存のプロジェクトがなければ、これをクリックしてAPIキーを作成する必要がある。
So I'll just select any of my projects and click on this.
自分のプロジェクトを選んで、これをクリックする。
So this is the API key you need to just copy this and make sure that you don't you know share this with anyone and in case if you feel your API key has been compromised you can just click this delete icon and that will basically make the API key inaccessible.
APIキーが漏洩したと感じたら、この削除アイコンをクリックすれば、基本的にAPIキーにアクセスできなくなる。
Alright so now as I told you we will be creating the .env file.
さて、では.envファイルを作るとしよう。
Just click on new file in the exact same folder as your .py file .env and here you need to have it like this.
.pyファイル.envと全く同じフォルダにある新規ファイルをクリックし、以下のようにしてください。
Just paste the API key have it within double quotes and go here we will be having this Gemini API key.
APIキーを二重引用符で囲んで貼り付け、ここにGemini APIキーを入力する。
We're going to copy this and put it over here with the equal to.
これをコピーして、equal toと一緒にここに置く。
Just save this file and now again come to our Python file over here and we need to load that particular API key from our .env file.
このファイルを保存して、もう一度Pythonファイルに戻り、.envファイルから特定のAPIキーをロードする必要がある。
So for that you can just have from .env import load underscore .env.
そのためには、.env import load underscore .envとすればいい。
Next time it's going to be load just call this and now we are good.
次は負荷がかかりそうだ。
API key these are fine from OS yeah now everything is perfectly set over here and this is basically the code.
APIキーはOSから問題なく取得できている。
Here we are setting the API key to genai.configure and we are getting the genai from this particular Python package that we just installed now and here we are having some input parameters that we send to the model.
ここではAPIキーをgenai.configureに設定し、インストールしたばかりのPythonパッケージからgenaiを取得し、モデルに送る入力パラメータを設定しています。
First one is the temperature which basically determines the creativity level.
まず1つは、基本的に創造性のレベルを決定する温度である。
The more the temperature the more it will be creative.
温度が高ければ高いほど、クリエイティブになる。
The less the temperature it will be less creative and will be more focused on the output and this is the top underscore P top underscore K.
温度が低ければ低いほど創造性が低下し、アウトプットに集中するようになる。
I'll just put a screenshot you can just read it through what that means and next is the max output tokens which basically determines the number of words that are need to be generated in the output.
スクリーンショットを貼っておくので、どういう意味か読んでみてほしい。次は最大出力トークンで、基本的に出力に必要な単語の数を決定する。
This is really really important in case if you're having a paid plan because if you generate large number of tokens in the response you will be getting charged more.
有料プランの場合、これは本当に本当に重要です。なぜなら、レスポンスでトークンを大量に生成すると、より多くの課金が発生するからです。
So this is an important parameter and final one is the response type this is just text yeah nothing big over there and here genai.generativemodel and we are having two parameters here we are passing the model name in our case it's Gemini 2.0 flash.
これは重要なパラメータで、最後の1つはレスポンスのタイプで、これは単なるテキストです。
You can choose any model from here and just go to studio and you can choose whatever the model you want from here and these are two parameters that we are passing and this is how we are starting the chat it's going to be this particular variable .chat and you're also having a space to add history basically your question is chat history and then we're just starting the chat session .sendmessage and here we will be asking the input question and at the end we are getting the response and printing the text from it.
ここから好きなモデルを選ぶことができ、スタジオに行き、ここから好きなモデルを選ぶことができます。これらは渡す2つのパラメータで、これはチャットを開始する方法です。
So that's basically the code and now let's go and ask a particular question so what I will do is let's just get the raw response and see and let me just just put a hello that's it this is going to be my input text or question so for running this is going to be python space Gemini underscore file dot py and before going into all make sure that you have python installed on your system that's really important and if you don't have python installed you can just click on the card that is appearing on top I will run an entire video on that so if I run it now ok I'm getting an error protocol message generation config has no response mime underscore type so yeah this is a generation config so I guess this particular parameter is not needed or something so I'll just remove this okay so now let's go and try again so we are getting an object okay so now let me just go with dot text I thought we will be getting a JSON something like that so again rerun it yeah here as you can see here we have got hello there how can I help you today so now what we are going to do is I'm just going to ask another question this time I will ask a question that gives us a lot of response so let me just go and ask essay on global warming that's it saving and rerunning so this is the output that it has given that's a lot that's really like five to ten paragraphs and what if you wanted to control the response because as I told you earlier the more output token it gets generated the more you will be charged and there are some conditions where you might want the response to be like two to three paragraphs in that case what you can do is here we are having the max output tokens as 8192 you can just change this to like 200 maybe so I'm just going with the exact same prompt now but this time we'll be at most getting two paragraphs so let me just run it again so now we have just got two paragraphs over here so this is basically how you control the output response also and in case if you want to know more about how to access this API key you can just go here and they will be having API documentation select it and here you will be having the entire guide on how to use this like fine-tuning structured outputs and other use cases for vision long context code execution there are like so many things you can just go through it as a guide because this is like a starting point okay and that's the video guys I hope you have found it useful do check the playlist of my channel I've done a lot of videos in programming Gemini API open AI API Sora chat GPT and other AI tools and credit operations check the playlist subscribe me thanks for watching
これが基本的なコードで、次に特定の質問をしてみよう。pythonがインストールされていることを確認してください。pythonがインストールされていない場合は、上に表示されているカードをクリックしてください。もしPythonがインストールされていない場合は、一番上に表示されるカードをクリックしてください。このような場合、あなたはどのように応答を制御したい場合はどうすればよいのでしょうか。最大出力トークンを8192としていますが、これを200に変更することができます。