Create Your Own AI Chatbot With Google Gemini AI API Using Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's going guys so in today's video we're going to learn how to build a Gemini a by using python now before diving into the tutorial let me just give you a quick demo of the app that I will be building from scratch all right so here me launch the app now when I launch the app it's going to uh gives me this uh welcome message and right here you can type quit to uh exit the app right let me put that back and to be able to work with a gini AI model we'll be using Google's generative AI python library now here I can uh type my questions so for example uh where is Chicago then going to reply Chicago is located in the states of inois in the mwest region of the United States then can ask a follow question where should I visit in now that's going to gives me a list of places that I can visit and no that the output is uh set to markdown which is the default but I can always change the output format to regular text or even a dictionary or Json upj it's up to you now because this is going to be an introduction uh tutorial I won't be going into too many too advanced features such as how to create a database to uh archive the conversations and CS my goal here is I'm hoping this tutorial will at least get you studed on how to use gmin a model to start building different applications right so let me close uh the terminal now step one is going to create python future environment that is going to be dedicated to our python project now open your terminal and we can create python future environment by using the command python M VMV follow by the envirment name in I'm name the environment Gemini check bar then I'm going to S into the folder now here on my window I'm going to open the folder here all right so once we create the python vure environment we now need to obtain the API key now open your browser and navigate to maker site. google.com slapp and let me see let me just try that for now okay that work yeah so you can uh use the URL maker site. google.com/ app and that takes you to uh this page page now before we start the tutorial make sure that you have a Google Cloud platform account and if you don't have an account simply go to console. cloud.google.com and follow the instruction to create account and it's pretty easy inside the Google AI Studio want to click on get API key on the top web two options we can either create an API key in a new project which means that Google studio is going to create a project for us or if I experience user and you already have a project created then you can click on create API key in existing project and select the project that I want to use now since I already have a project I'm going to click on create API key in existing project and here I'm going to uh choose the project I want to use then click on create new API key and new set API key will get generated now here want to create a credential file to store the API key so right click and here I'm going to create a text file uh I'm going to name the file to credentials. ini inside the ini file I'm going to create session code Gemini D API I'm going to create variable called API key then I'm going to copy the API key it should be this one here assign that to the API key variable and save the file and close the file then we can start with the script development right so let's go back now I need to activate the environment so simply run the activ v file from the scripts folder now to use the Gina a model in Python we need to install the Google generative AI python SDK so we need to type pip install Google generative Ai and enter and once the library is install let's create a python file callede app.py right so I'll launch my vs code and let me open the app. file now from the import statement here me that a little B bigger now from the import statement we need to UT the generative AI module so UT google. generative AI as gen AI now to make our code base a little more structured I'll be creating classes so the first class I'm going to create is the geni exception class so this going to be the base exception class that I'm going to use to print the a message then I'm going to create my chat Bo class and just realize something that I should be putting this in a different script because uh this is going to be a separate module that can be reused for other purposes now I'm going to name the file check barp now inside the check bar class you can Define the name of the check bar so here I'm creting a constant called check bar name and it's going to be the name that I'm going to assign to my chck bar next we're going to create the Constructor so when we initialize the chck bar instance we need to make sure that we pass the API key now inside the Constructor we're going to create an instance of the Gen AI module object which is this one here then we can attach the API key using the config method then API key is equals to API key and just remember that the self. Gen AI option is going to tie to the uh gen AI module then we're going to attach the model that I want to use using the generative model class by creating a generative model object and here we need to provide uh the model ID gini Pro and I name the object as model Now using the gen module to create a CH bar we can create a chest session instance to store the uh composition lot internally and here I'm going to create a blank composition object and I'll set the value To None because right now the composition log is empty so I'm going to set the value to now you can also set this to empty list now this one's optional so in case if want to preload some the conversation history to give the Air Model A little bit more context on what you're trying to do with the chba or the purpose of the chba in that case we can uh preload some the uh information of reference for the chba to use before we start the initial conversation with the user all right so that's everything we need to do in the Constructor let's create the start conversation method so this method is used to initialize a composition now inside the stock composition method we need to reference the model object then we can use the start check method to create a uh check session object or instance and inside the start check method like I said before we can uh Fe a pre-loaded message or conversation beforehand and because we are starting a fresh conversation we are supplying an empty list now if want to uh preload a composition log right so here have a method called preload composition and from this method I give a parameter called composition history just in case if you use case is a little bit different from mine now inside the method we need to check if the conversation history uh argument is list then we're going to update the conversation history now there are a couple other checks that we can do but just for demonstration purpose I'm not going into too much detail I'm pretty sure uh you can figure out once you finish the tutorial all right so if this condition fail then I just want to preload my own message or not my message my own chat history now to feed the message so with each message we need to specify the text and the rule so the rule can be either a user or thei which is a model all right so for example so I'm going to start from uh thumb on return the output Json object because with Google's API the default is set to markdown and I want to change that to Json object so here I can uh return the output of Json object that can be loaded in Pyon with the key and here the the key value text and I need to provide a reference then I can simulate what the output should be from thei so I can say so here I can return the response as a dictionary and I can simulate this as output from thei which is the model and to uh simplify the message construction I created another method called construct message and basically uh this method takes two parameters the text and the rule they we need to take the arguments and put that into aition object with these two keys and four parts we need to make sure that we put the text inside a list bracket now this another method I want to create meanwhile let me take out the canidate count because the default must be one output at a time now we can uh change the configuration setting such as the top n top K and the stop sequence uh settings for Simplicity I'm going to Simply uh use temperature as the only uh configuration setting in this uh chear app now if I new to the temperature setting so temperature controls the creativity of the response and the Val is between zero and one if want the check bar to have a relatively creative response then can set a value to 8859 or even 1.0 in one the response to be conservative then can set the value to between zero to 0 for maybe between that range and if want to preload the conversation so around the self. oops let me put that back so we'll run the self do preload conversation method before start the conversation now just in case if want to start new composition or if want to reset the current existing session we can create another meical clear conversation and to clear the conversation history we'll simply reference the model object the start chat and we'll reset the history to an empty list and we'll set the history to a blank list and to make the request code to send the prompt so here we're going to create me code send prompt and it's going to be the most important method to uh make the tri bar to work now the same promp method has two parameters promp and temperature so cover temperature it must be between 0 and 1 otherwise we're going to raise exception temperature must be between Z and one now the other thing I'm going to check is if the pump is return empty which shouldn't be the case but it does happen sometimes they want to raise another exception prom cannot be empty otherwise I can reference the composition object that send message inside the send message method I'm going to supply the pump to the content argument and to change the model setting I can reference the generation config object then I'll provide my configuration setting now just in case I feel output has uh multiple parts and this one I really like about the uh generative AI python SDK is that you can run the res s method to get all the parts first then we can print the response all together in a single response now I want to retrieve the conversation log and this another scene that I really enjoy using the generative AI API is that you can directly reference the the history and we can do that by referencing the composition object then we can reference the history attribute and that return the composition history and from each log it's going to contains two keys and it's going to be a addition option and from each log we can reference the row and the text by referencing parts and because part is going to send us a list and with the check session object you can only have one message at a time so we can reference here to uh refence the the message. text to the output and we're going to wrap this in a dictionary then I'm going to compile all the composition loog into a list then return the list now this is going to be everything that we need to write to create the check bar. py module and here's the check bar module so first we need to uh import a config pass class so we can load the API key then from check bar I'm going to UT a check bar class now here me insert the main function inside the main function we're going to load the API key then we're going to create the check bar object then we're going to start the conversation now here I'm going to uh preload a message when we start a conversation section then we need to insert W Loop to uh keep the conversation running until the input receive the the keyword quit then we're going to print message and terminate the session uh here me import the system module and I was doing some testing before so should be system. exit and let me put this right here otherwise we can reference the chat object then we can send the prompt from the user input then we'll print the response now let's test out the app first I need to select the target environment and save the file now I'm going to run the script oh I forgot to run the main function Gemini AI is not available this should be AI not API all right let me try again okay so here's the welcome message now let's say I want to know what are the top languages actually programming languages for web development now remember from The Constructor we preload the composition and from the preload composition method we specify that we want the output return as a Json object so we can load the output diary object so right here is the key text followed by the reply then can ask a followup question about data science and again it's going to return us a dictionary and here's the key text and here's the reply now I want to make sure that the reply is back to the regular mark down syntax in that case you will simply come out uh this line here now this time if I ask the same question again one not the top programming languages for web development and this time the reply is going to return as a regular modown format that we can read just a little bit easier this time so this going to be everything I'm going to cover in this tutorial and hopefully you guys find this video useful and you can find the source code from the link in the description below if you enjoy this video please don't forget to give this video like and click on the Subscribe button and I'll see you guys next time bye-bye
Info
Channel: Jie Jenn
Views: 5,167
Rating: undefined out of 5
Keywords: AI Chatbot Development, Google Gemini AI API, Python Programming, Machine Learning Techniques, Google Cloud Services, Chatbot Integration, Python for AI, Gemini AI Model, Generative AI, Python tutorial
Id: w73nrTquxm0
Channel Id: undefined
Length: 18min 32sec (1112 seconds)
Published: Thu Jan 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.