Placeholder Image

Subtitles section Play video

  • Hey everyone, in this video I will be showing you how to use Google's Gemini AI API along with Python.

    大家好,在本視頻中,我將向大家展示如何使用 Python 來使用 Google 的雙子座人工智能應用程序接口(Gemini AI API)。

  • So let's get started.

    那麼,讓我們開始吧。

  • So first thing just Google 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 的第一個鏈接 選擇此鏈接,並確保您正在使用谷歌賬戶登錄,然後在頂部您會看到這個獲取代碼的鏈接。

  • 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.

    代碼基本就是這樣,現在我們需要安裝兩個 Python 套裝軟體。

  • One is the Google generative AI Python package because as you can see here we are importing this one over here.

    一個是谷歌生成式人工智能 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.

    這是第一個套裝軟體,另一個是 .env 套裝軟體,因為我們將在這裡使用雙子座人工智能應用程序接口密鑰。

  • 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.

    現在,讓我們去獲取雙子座人工智能 API 密鑰。

  • So for getting that here you will be having get API key option.

    是以,為了獲取 API 密鑰,你可以在這裡使用獲取 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 密鑰",因為我已經有很多項目了,所以不需要創建新項目,如果你沒有任何現有項目,你需要點擊這個,然後創建一個 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 密鑰已被洩露,你可以點擊刪除圖標,基本上 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 密鑰,並將其放在雙引號內,然後在此處輸入雙子座 API 密鑰。

  • We're going to copy this and put it over here with the 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 導入 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.

    應用程序接口密鑰和作業系統的密鑰都沒問題,現在一切都設置好了,這基本上就是代碼。

  • 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.

    首先是溫度,它基本上決定了創造力的高低。

  • 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.

    溫度越低,創意就越少,就會更專注於產出,這就是上下劃線 P 上下劃線 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.

    所以這是一個重要參數,最後一個是響應類型,這只是文本,對,沒什麼大不了的,這裡是 genai.generativemodel,我們在這裡有兩個參數,我們傳遞的是模型名稱,在我們的例子中是雙子座 2.0 閃存。

  • 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.

    你可以在這裡選擇任何模型,然後轉到 studio,在這裡選擇你想要的任何模型,這是我們傳遞的兩個參數,這就是我們開始哈拉的方式,它將是這個特定的變量 .chat,你還可以在空格中添加歷史記錄,基本上你的問題就是哈拉歷史記錄,然後我們開始哈拉會話 .sendmessage,在這裡我們將詢問輸入的問題,最後我們將得到回覆並打印文本。

  • 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

    這就是基本代碼,現在讓我們來問一個特定的問題,我要做的就是獲取原始回覆,然後讓我輸入一個 hello 就可以了,這將是我的輸入文本或問題,是以運行時將使用 pythonspace Gemini underscore file dot py,在運行之前,請確保你的系統已經安裝了 python,這一點非常重要。如果我現在運行它,我就會收到一條錯誤的協議消息:生成配置沒有響應的 mime 下劃線類型,所以這是一個生成配置,我猜這個特定的參數是不需要的,所以我把它去掉就可以了,好了,現在讓我們再試一次,我們會得到

Hey everyone, in this video I will be showing you how to use Google's Gemini AI API along with Python.

大家好,在本視頻中,我將向大家展示如何使用 Python 來使用 Google 的雙子座人工智能應用程序接口(Gemini AI API)。

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it

A2 US

如何用 Python 使用雙子座人工智能應用程序接口 | 最新 2025 教程 (How to Use Gemini AI API with Python | Latest 2025 Tutorial)

  • 0 0
    松松 posted on 2025/03/03
Video vocabulary