# Gemini API with Python - Getting Started Tutorial

## Метаданные

- **Канал:** Patrick Loeber
- **YouTube:** https://www.youtube.com/watch?v=qfWpPEgea2A

## Содержание

### [0:00](https://www.youtube.com/watch?v=qfWpPEgea2A) Segment 1 (00:00 - 05:00)

hi I'm Patrick i'm a developer at Google DeepMind and in this tutorial I want to show you how you can start building with Gemini and the Python SDK so I want to show you how you can get started and how you can explore all the models in Google AI Studio and then move over to the Python SDK then I want to show you how you can start setting up a chat and ask questions to the model then I want to show you some of the cool multimodal capabilities because Gemini is also really good at for example image video or audio understanding and lastly I want to show you some of the thinking capabilities with the new Gemini 2. 5 models and how to work with them so let's dive right into it the best place to start building is in Google AI Studio and for this you can simply type AI. dev and now you land here this is a playground where you can play around with all the Gemini models and then try different prompts try different settings and explore all the functionalities that Gemini offers and then this is also the place where you generate your API key and you can even generate starter code and then start building so let me give you a very quick rundown so on the right side you can select the model you want to try for example our 2. 5 models you can also select our 2. 0 models or even Chemma models so let me select for example Gemini 2. 5 flash then here further on the right side you can tweak some settings like the temperature or the thinking mode and then even toggle different tools and now here you can type your prompt for example create a blog post about generative AI and then you hit command enter and now it's going off and by the way this is how fast uh the flash models are and now you get your uh blog post and now for example you can click here and generate starter code for the different SDKs we have um in this case I want to do this manually so let me show you how to set up the Python SDK and by the way you find this on GitHub the Google Gen AI SDK and this is the installation command you need so let's go to a terminal here i'm already in a project folder and I activated a virtual environment and now the command you need is pip install Google genai i also used the minus u flag to upgrade to the latest one and now we have this installed the next thing we need is a API key and you can generate a free API key in Google AI studio so for this simply click get API key i'm also starting from scratch here so again we click on create API key then you can uh generate a new project or select an existing one so for you it should autogenerate one and then we create an API key and this should take only a few seconds and now we have it so now we can copy this away and by the way we have a very generous free tier so all the code I'm going to show you in this tutorial can be used on the free tier so let's copy this and close this and later you find this again down here so let's go back to the terminal and I recommend storing this as an environment variable so for this I'm on a Mac i say export Gemini and then here we give this a name Gemini API key equals and now we paste in the key that we just copied and then hit enter and now we can use this in the code so let's open your editor of choice and here you can now say from uh Google import chenai then we need to set up a client so we say client equals chenai. client and here we give it an API key so you could hardcode this i want to do this in a more secure way so I import os and then here we can say os get enth and get the environment variable with the name that we just used so gemini api key and now we can send our first request so for this we say response equals client. generate content and now we give it a model ID so for this we can go back to the AI studio and then here you can select the model you want the first one I want to show you is Gemini 2. 0 flash so you also find the model ID here so let me copy this and then go back to the code and paste this in here and now we also need to give it a prompt so for this you need the contents argument and here we can say for example why is the sky blue question mark and now this is

### [5:00](https://www.youtube.com/watch?v=qfWpPEgea2A&t=300s) Segment 2 (05:00 - 10:00)

all we need so now we can say print response. ext and now save this and then go back to our terminal and then run the script and now this should take a few seconds and now here we have it why is the sky blue due to a phenomenon called relay scattering so now this is working so now you've sent your first request the second method I want to show you is instead of generate content you could also use the generate content stream method and now you can iterate over this so for stream in response and then print stream. ext so what this is doing is this is generating it on the fly and now you will see it starts generating and you immediately see the results so this is the difference between generate content and generate content stream so yeah this is how to send your first request next I want to show you how to set up a chat to chat with a model you can create a chat by calling clients. create and then again you need to specify the model you want to use and here I created a simple while true loop that waits for a user input and then we can call chat send message and print result. ext and the chat class is a utility class in the SDK that keeps track of the conversation history so now we can start chatting with the model by running this script and I can say hi and then the model should respond and I can say can you tell me about machine learning and now we should get a response and now we can keep going for example we can say tell me more about kegle and then again it continues in the conversation so this is how to use jets gemini is designed to be natively multimodal so now I want to show you how good it is at understanding different file types and also how easy it is to work with them so here for example I have a image with some cupcakes i have a short audio file and I have a PDF file with an invoice and to use this in your prompt all you need to do is you can use client. files. upload and then upload your file and then when calling generate content in your contents argument this can also be a list so here you use your prompt describe this image for me and then append the uploaded file and then again print response. ext so let's run this and now if we execute this it should describe the image and hopefully detect the cupcakes and the image showcases a wooden table with a variety of whimsically decorated cupcakes surrounded by baking stuff so yeah this is working so let me show you how to do the audio file for example so here we upload audio. mpp3 and then we can say describe this audio for us we can also say for example transcribe uh the audio file it's also good at transcribing so let me clear this and let me run this and while this is running let's see if we can quickly play this if the Porsche Macon has proven anything it's that the days of sacrificing performance for practicality are gone long gone so this works and last but not least this is an exercise for you so you can try this for example with uh PDFs and then for example say extract the date and amount uh from the invoice so yeah try this yourself if you have an invoice but this is how to work with different file types one more thing to mention is that you don't necessarily have to use the upload function you can also directly append the data in your request so for this you can use from google. chai Chen Aai import types and then here we have the image data as raw bytes by reading this and then we create types. part from bytes and here we specify the image bytes and then the correct mime type in this case it's image/jpeg and then we can also use this image in our request directly so yeah these are the two ways how you can append files in your request and the last thing I want to show you is how you can work with the thinking models so all the 2. 5 models have thinking capabilities so let's select the latest 2. 5 flash here by the time you're watching this maybe this is already a new version so yeah keep an eye out what's the latest one here so what's cool

### [10:00](https://www.youtube.com/watch?v=qfWpPEgea2A&t=600s) Segment 3 (10:00 - 12:00)

is now developers are in control so you can even with this one disable the thinking mode and you can set the thinking budget so how many tokens the model can get um to work through the steps so zero means no thinking and then this is the maximum so if you just disable this then it uses as many as it needs and now if you run a prompt generate a learning road map to learn Python you get this box here where you can follow the thinking steps so it's developing a Python plan structuring the learning plan mapping the journey and so on and then it's outputting the learning road map to the user so this is how to try it in AI studio and now to use this in the code you select the model name so again check the model name in AI studio i'm using 2. 5 flash preview and then this is from May the 20th and this one is optional so the code is the same you can simply give it the prompt here but now uh what I just showed you is you can define a config and then specify the thinking config and here you can specify the thinking budget so again zero means no thinking and if you specify include thoughts then you also get a summary and can print it like this so let's save this and now let's run this and then again we get the response and then somewhere down here you can also find the thinking summary so this is how to use thinking models in your code with the Python SDK all right and this is all I wanted to show you now you know how to get started with Gemini and the Python SDK let me know in the comments if you have any questions or feedback or any feature issues you want to see from Gemini or Google AI Studio and then I see you in the next one bye

---
*Источник: https://ekstraktznaniy.ru/video/29557*