Getting Started With Official Google Gemini AI Python Library | Step-By-Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's going guys all right so in this tutorial I want to cover how to use Google's official python SDK code Google generative AI to work with gini AI apis so the python library is called generative AI python so from one of my previous videos I cover how to use FedEx AI SDK to work Gemini AI models and the reason why I want to cover how to use Google's generative AI python SDK is because the fex AI SDK for python is recommended if an experienc machine learning or artificial intelligence engineer or there scientist who wants to programmatically automate your workflow in summary the fex a SDK is similar to this generative AI python client Library except that the fotex AI SDK is a higher level and L grania compan to the generative asdk and I'll link all the links in the description below all right all right so before you can uh use the SDK you need to have python 3.9 or later version and for this tutorial I'll cover three examples from basic to advanc examples all right so let's first of all create a python future environment so launcher terminal all right so let's name the environment let's name this as Google Pyon AO I'm going to the folder here then we need to activate the environment so C into the vir environment then activate the environment all right so going back to the documentation so to uh install the package we need to run the P install Google Dash gen ative AI so copy and paste enter and once we install the package we need to obtain the API key now open new tab and navigate to makeer site. google.com slapp API key now that takes you to the Google AI Studio on the left hand side I want to me make the little bigger on the left hand side you want to click on get API key and here I created the API key so here let's do this let me delete this one here now if you already have a Google project created you can either use a existing uh Google Cloud project or you can create new project now here let's create a new project so I'm going to click on create API key in new project I'm going to copy the API key ke and I'll save the API key to notepad something so here I'll create ini file and I'm name the file credentials all right so here name uh the group API key let's name this as Google API key and save the file now there are two ways you can authenticate uh your credential One is using the environment variable so if you want to create environment varable so basically you want to name the environment varable to Google API key then assign the API key so what happen is Google generative AI python SDK is going to look for the environment variable first which is this environment variable here and if the python SDK cannot locate the environment variable they will check mainly if an API key is provided and that's the appro that we're going to take for this exercise now once we have the API key we can now start putting together the script now I'm going to launch my vs code here right so I'll create a python script and I name the script demo. Pi it should demo onep now open your Comm p and make sure that you set the target environment to the environment that created so select interpreter and the environment should be Google python AI demo from the import statement let me to find bigger now from the import statement I'm going to uh import the config pass class from the config pass module then from google. generative AI I'm going to name the module as gen AI so everything we do to work with Google's AI model is based on this gen AI module all right so let's go ahead and load the credential first so I'm going to create my config P op then I'm going to load the in file then from the credential file I'm going to load the API key so from config API key follow by the Google API key all right so the first thing here we need to do is we need to attach the API key to the generative AI module so here from gen AI we need to use the configure method and found the API key we need to assign the API key here next we need to specify which model that I want to use now here let me go ahead and run this code block so me go ahead and uh attach the API key to the uh gen AI module now if you don't know what models are available from the uh Google's AI Library so from the Gen AI module we can use the this models method and here let's erate the models so for model in gen. L models and I'll run the uh the method and it's going to return all the meta data that currently the uh Google's python SDK supports now if we just look at one of the rackers so the model rer from the model record we have the name of the model model ID model version display name description token limit and the supporter generation methods and these three are the parameters that you can specify to control how the AI is going to respond to your P now for the generative AI which is what this video is about so for all the models that supports generative AI under the supporter generation methods property they should have the generate content value so this is an indication that this model is a generative AI model so if want to know all the generative AI models I can insert if condition I can say if model supporter generation methods and I want to check uh this key here generate Cent key is in the list and the condition is to down to print the the name of the model and the description all right so if I go and and run this Loop here let me insert seator So currently we only have two uh generative a models one is the Gemini Pro and the other one is the Gemini pro version so this model is used usly for image creation or anything Sim relate to dealing with image or videos first the Gemini pro model this one is used for any Sim related to uh text such as writing essays generating different business proposals and so on now I want to use the Gemini pro model to do something so from the Gen AI module we need to reference the generative model class then we attach the model name then name the output model Gemini Pro then I'll create a model instance now at this point this object here is the model itself so for example here let's say I have a promp I want to know the top 10 oh not top 10 the top destination to visit in California and here can take the model Gemini Pro option they refence the generate contain method now experience using the asynchronous approach then you can use the generate content a sync method and this the method that I use if I need to uh generate multiple contents quickly but usually generate content method should be sufficient for 80% to 90% of the use cases now from the generate content method then I'll provide the p and I'll save the outputs response and I'll go ahead and run this code block and once the output is generator let me delete this line here I can reference the X attribute and print the output so by default the output is going to return as a markdown uh syntax or markdown format so if you need to return the output as something else in JM format or just a regular text format they need to specify the format type in the prom let's look at another example now say your PR is going to uh return all large chunk of text so for example here in my prompt I want to write a project proposal why company should start using AI in there workflow now instead of waiting for the content to be generated I want to stream the content as they loaded so here I'm going to create list and name the list output so this output list is going to uh store the content as they are loaded then from the model Gemini Pro object. generate content I'm going to provide the pum then I'm going to set the stream parameter to true now to print the output as St generated we can insert Loop so basically if I run this code block and if I print the response to Opa now if we look at the object type so here knows that the object type is generate content response op and let me check something response one response one response okay so they are the same object type anyway now to stream the outputs they are generated so here we can simply erating the response to object so if I run the loop here let me let me do it again yeah around this code block here right so the first so here's the first part second part third part then the last part which is contains the remaining uh text all right so here me uh try something again I want to PR the opop type all right so now mind so when you set the stream value to two then um it's going to treat us kind of like a generator B basically and I was hoping that the object type is going to return as a generator but I guess that's not the case here now going back to the first example this one here now if we look at the parameters from the generate counting method I'm going to skip the safety settings property but I want to cover this generation config property parameter here now using the generation config parameter we can control how the AI is going to response based on the prompt that we provided right so here uh found the what's the parameters name generation config right so you want to provide the configuration setting to the pr from the Gen AI object types the generation config class now using this class we can uh specify different parameters of settings canidate counts means that how many responses that you want to generate so by default the generate counting methods going to uh return one output by want to generate a variety of outputs then can set how many outputs I want to generate stop sequence is that when you provide a promp you can set a list of keywords where the a is going to uh stop so for example I say I have two promps so the first request is what are the top destination to visit in California but if I change this to in Chicago and if I set the stop sequence keyword to three D ases then when we Supply the prom the model is only going to look at the prom below the keyword here the stop sequence keyword let me put that back and Max output tokens uh should be self-explanatory so this is the maximum token that the API can consume temperature controls the creativity of the response so the Val is between 0 to 10 and you can have decimal places if if want to have a conservative response then you can set the value to zero but you want the AI to be creative then you can set the value to 10 I'm not going to cover tab p and tab K value but I'll cover how to use those two parameters in some other videos right so let's go back to response two make sure that I still have the object active now let's assume that from your request uh use specify that you want to return more than one response in for to erate the outputs you need to reference the can attribute and it's going to return list oh and look like the response is not complete okay there you go all right so because uh for this one I set the stream to two and because the response to object is going to uh work like a generator so let's go back so let me recreate the First Response all right so from the response object if I turn the candidat attribute that should return this with a single element because in this case uh the default is one response and the response contains multiple outputs then you need to uh erray the list to get all the possible outputs and this another attribute iot what this one to do parts and I believe that Parts is used when we have different types of contents but in this case uh based on my prompt the a model is only going to uh generate the text output so found the part output we get a list and from the list object we can reference the first element and from the element we can reference the text attribute to get the output as well now this one I want to uh follow um the second sample so if we set the stream to two but let's say I don't want to stream the contents I want to return everything all once in that case then found the response you can use the resolve method and to wait until all the contents are generated then can print the entire output by referencing the text attribute now let's C the second example right so here let me name the file to demo 2 now for this example let's uh use the Gemini provision model all right so so here let me grab the image here right so here have a image of cyber truck for image that looks like this and for this example I want to uh use Gemini P Vision AI to do something with the image or to create um some outputs or contents based on the image all right so here let's do this I'm simply going to to grab this code block now to look at images uh using Google generative AI python SDK from the official website the recommend method is using the pillow Library which is a image manipulation python module all right so here I'm going to insert three more modules the io module base 64 module in font pillow I'm going to import the image class now here I'm going to create my model and I'm going to use the Gemini Pro Vision model now to load image so first from the image class that open want to provide the image file path and I name the object as IMG image then we need to convert the image to B array so first we need to create i. byes iio object to store the the byes string then we're going to pass the uh binary to the B array object and we need to uh specify the format so here if we reference the format attribute then it's going to return jpeg then we need to return the by array using the G Value method and the last step is we need to uh encode the Bas array using the B 64. b64 incode method then we Supply the B array and name the outputs encod the image data now to run the generate counting meod so here let me paste my Cod spp we need to first of all create dictionary inside the dictionary need to specify the mic type and just because you can provide either a video file or image file and even with the image file there's different types of image file so here because my image file is a JPEG file so I'm going to set the M type to IMG jpeg then from inco image data I need to decode that to utf-8 inside the generate counting method we need to provide another dictionary and we need to make sure that we name the key to inline data then you'll provide the image blob option now if I go in the Rong everything here me terminate this session oops let me go back now if I run the script so by default because I didn't provide any prompt or any request I simply just provide image to the generate content method so what happen is the AI is going to load the image then it's going to attempt to describe the image all right so here's the response the sa truck is oncoming electric pickup truck from Tesla it was unv in November 2019 and is expected to be released in 2023 and so now if we need to do something with image so if we have a sub request all right let me put this right here let me name this two p now I want to write a short engaging blog post based on the picture and the post should have at least 2,000 words now if want to supply a specific request then we need to insert list inside the list the first element is going to be the prompt and the second element is going to be the media file and here because the output can potentially be a pretty large variable outputs so I'm going to set the stream to two then I'm going to let me come out this two this one let me come on these two lines so I'm going to stream the the text and our runs so this is some of the use cases that you can combine both prompt and media file such as image or video file with Gman AI models now the last example I want to show you is how we can create a triip by using the building message meit right so here I'm actually going to name this uh script appp and now is the Gemini Pro AI model now I want to create a chck Bo so here me insert the co snippet so from the Gemini object there's a mythical St chat and and if we look at the app job type so the app job type is returning as a chat session so what that means is that you can use this method to store the conversation internally but first we need to uh provide a container to store the the log so using the history printer I'm going to start a new conversation so here I'm going to provide a blank list opt now you can also provide additional uh information or preloaded messages to the history to give the a model some background on what you're trying to do such as different examples or you can provide any references that can be useful to your pumps now for this check bar I simply just want to use the a model to help me to learn Google Cloud platform so let me go ahe around this Cod block mainly oops now to send the response from the conversation object we can reference the history attribute and if I print the history attribute it's going to return the conversation law and each record is going to contain the text and the rule so this is going to be the first record and this is going to be the second record so coming from the model itself if I print line 14 is going to model because the last message is generated by the G AI model and to print the text so from each element notice that you have this Parts follow by the text attribute so we can basically reference the last element from the composition. history uh list and again because you can potentially have more than one response so that's why here I'm erating the uh the part output if I run the loop here it's going to return the response generated by thei now if you need to ask a followup question so basically we will just take this method and run another send message method so you can say what are the what are the 10 gcp products and from the last message so we have several different groups compute engine app engine CL storage and couple others and to package this into a fully functional script so basically you will just convert this into a while loop to contining to wait for uh the input P from the users all right I want to show you one more example and this one's optional but this one's going to be a little more advanced example so here I'll name this to app2p all right so here I'm going to load the libraries then I'm going to create method to initialize the model now for this example I'm going to show a different way to create a chat box all right so whenever we send a message or we provide message we need to provide the pump which is the message and the r and here I'm setting the r default to user and this function is going to s a dictionary with two keys roll and Pass and Pass is going to be the message then I'm going to create another function and I'm name the function add models response so here uh if we look at the parameters conversation is going to be the message LW of the message history model is going to be the model object and to create a check bar so we can simply insert main function here here I me initializing the model then I'm going to create list and this is going to be the message log and here want to to set the first message to be your expert in writing business proposals can you write a 50 word summary to describe the usefulness of AI so this going to be my initial pump then here I'm going to insert while loop I'm going to feed the Mage slw to the A model here then I'm going to use resolve method to load all the contents first then I'll pin the last message to the conversation list and remember the response is going to be coming from the AI self so we need to set the road to model they want to PR the response and here we just basically uh running infinity loop until the user input receiv exit then we're going to terminate the the app otherwise we'll continue to add out user prompt or our user request all right so me to a demonstration I'll give a demo and here forgot to run the main method all right so because uh we're providing a initial prompt so it's going to return something in return then we can ask followup questions can you make it L than 20 was and here's the response then we can keep continuing until we type exit to excel the app so this is going to be everything I'm going to cover in this tutorial and hope you guys find this video useful and if you enjoy this video please don't forget to give this video a like and click on the Subscribe button and I'll see you guys next time
Info
Channel: Jie Jenn
Views: 1,903
Rating: undefined out of 5
Keywords: python tutorial, gemini ai, gemini ai tutorial, google gemini ai
Id: xBRzb5LqXTY
Channel Id: undefined
Length: 30min 22sec (1822 seconds)
Published: Tue Dec 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.