Google Gemini AI API Tutorial ✦ How to Use Gemini AI API for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'll show you how to use the Google gini API for beginners we'll build a simple command line node.js application in which we'll generate text from text only input we'll use streaming for faster interactions we'll also take a look at the Gemini provision API which will use to generate text from text and image input Google recently released their brand new Gemini API this means that you can call their API and get responses directly from Google Gemini AI which means you can essentially build AI into your own applications which is super powerful this opens up exciting new business opportunities I've already made a video where I shared eight best AI business ideas that you can start using the Google gimini API if you want to check out the video the link should pop up on the screen I'll also leave a link in the description the good news is that the API is free within limits and is very easy to use so let's get started [Music] you can access Gemini API from two places Google AI studio and the vertex AI you'll need a Google account in both cases if you'd like to access Gemini AI from vertex AI you'll need a Google account Google Cloud account with billing enabled if you're just getting started I recommend trying out the API in the Google AI Studio it's easier and when ready for fully managed Enterprise AI platform you'll be able to easily transition your AI Studio code to vertex AI for additional customization and Google Cloud features as a prerequisite you'll need a nodejs version 18 or above you can download it from nodejs.org and a code editor like Visual Studio code you can download it from code. visual studio.com they are free now that we have covered the prerequisites let's jump into my computer and show you exactly step by step how to use the Gemini API the most important thing to get started is to get an API key you can get an API Key by going to ai. google.de click get API key in Google AI Studio click get API key and create API key in new project Google AI Studio creates a new Google Cloud project for each new API key you can also create a API key in an existing Google Cloud project so your API key is generated and this is the only time you're going to copy this so H copy and the API key is copied into the clipboard so now what we're going to do is create a new folder I'm going to call it gim API tutorial open this folder with Visual Studio code and create a newv file paste the API key I'm going to call it AP iore key equals to the API key let's also create a get ignore file I don't want to accidentally comment this API key to GitHub because I'm going to be creating a GitHub repository for this code and I'll be sharing the link in the description so I don't want to commit. EnV nodecore modules please don't try to use this API key because I'm going to be removing it once I record this tutorial video like I mentioned this API key is free but you can get your own you get a generous six requests per minute which is more than enough if you're trying to build a small application let's install dependencies npmi EnV at Google generative dependencies have installed now we are ready for coding the things that I'm teaching you in this video I've learned it from the documentation so let's head over to the documentation you can get to the documentation for njs by going to this URL Let's uh generate text from text only input I'm going to copy the sample code let's head back to visual studio code let's create a new file index.js I'll paste the code that I copied from the documentation this is the code I'm going to use the EnV Library called const pnv require V I'm going to call the config method this allows us to use the the process. EnV environment variables like API key that we set in the EnV file so let's save this and run this code node index.js it's taking a while and as you can see we are getting a response from Gemini AI this is in response to the default prompt that is here write a story about a magic backpack if you want a different prompt we can modify this prompt I'm going to say write a poem about uh story night let's save this and run it again note index.js as you can see it's processing the request and uh we get back response from Gemini AI this is a poem about the star Knight and the expanse of midnight blue a Celestial candles comes into view it Rhymes it sounds like a great poem but the problem with this is that each time we want uh to write a prompt we need to come over here change this variable run the code and then we're getting a response we want to be able to create an interface in nodejs so that we can interact with gimini AI without having to come back to this code and change the variables and the best way that we can do that is by using a library called readline so I'm going to import that read line require read light this is a builtin njs library and we need to create an interface so to to do that we create a variable called the user interface line. create interface and we need to provide two properties input process. is standard n stdi n and output process. STD out going to save it so we created an interface let's PR the user let me comment out this function no wonder to run it let's do user interface. prompt and so what it does is if I if I run this you see you get a prompt you can type something and the application will do something once you enter this line now we need to set up an event lener we can do it online user interface do on line what that means is that whenever we H enter we want to call call this function with the input and we want to do something with that input so let's copy whatever we have over here I'm going to cut it I'll paste it over here we can get rid of this prompt because we don't need it anymore and replace this prompt with input and we can leave the rest the same and we can get rid of this function let's run this and see if it works note now I can say hi who is this I get a response from gimini AI it says I'm a conversational AI or chatbot trained by Google I'm designed to be informative and comprehensive I can say what can you do yep so I get a response back from the AI and it's telling me the things that it can do that's good but I want the response to be interactive and com in assets generated so to do that we can use the streamed response and to get a faster interactive responses we can change this result model instead of generate content we can say generate content stream like this and provide the input in an array and then I want to write a loop for each chunk of the result. stream I want to get the chunk text from chunk. text and console.log chunk text and so we don't need this anymore I can get rid of this and now when I save this I can run I can say Hi how are you and you can see now I get faster response I get the response as it's generated I don't have to wait for the whole response to be completed before I get it I get the response in real time this is so cool so this is how we can use the streamed response now let's take a look at the multimodel input let's create a new file to work with the Gemini provision API I will call this multimodal doj let's go back to the documentation and copy the sample code I will copy this code and I'll paste it in the new file I'm not explaining the code because it's pretty easy and straight forward comments have been already included as you can see we're using the same library and we need to use EnV we need to use EnV to access our API key as an environment variable I can call count fig over here this will allow us to use our environment variables from the EnV file here this function converts local file information to a Google generative AI part object we have an asnc function over over here for text and image input we use the gini Pro Vision here we provide a prompt and we can provide a single image or multiple images in an array we can provide up to 16 individual images which should not exceed the file size of 4 megabytes along with the prompt over here we send prompt along with the images to the Gemini AI we wait for a response and once we receive the response we display it in console log the text let me copy a couple of images to work with the G Prov Vision API I have downloaded a couple of images and image one I have my logo and the second picture a bunch of vegetables so I'm going to copy these two files I'm going to move it to my folder and once I go back to visual studio code I should be able to see these two images we need to make sure that we use the correct file name as you can see I have Mage 1. jpic so we need to make sure that it matches the file and I I need to change this as well JP and the second file is image 2 is also JP for now let me just use one image I'll change this prompt to say what's this image question mark I will save this and run it this is the image I provided it's sending the image along with the prompt and as you can see it's saying the image is a logo for a company called coding money the logo is blue and green rectangle with the diamond in the center is describing this image perfectly let me provide this image and give it a more specific prompt I'm going to say trct the the objects and the provided image and output them in a list in Bal order and now I'm going to use mh2 I will save this run I'm getting an error yes I don't have the correct file name it is jpeg so I'm going to save this run it again and this is the image that I have provided it's got all the objects correctly I can provide multiple images in the same prompt I can say image 1. jig along with image2 and the prompt I'm going to say describe the provided images save run it I provided these two images it's going to send the prompt these images along with the prompt to Gemini Ai and it's going to get back with the response now as you can see the first images of a logo that says coding money the logo is blue and green with a diamond in the middle that has a dollar sign on it the second images of a group of vegetables I hope you enjoyed this video in case you missed it check out my last video where I share eight best AI business ideas in just 8 minutes I'd love to hear your thoughts in the comments down below would you be interested in videos about building an AI business from scratch feel free to share your ideas for upcoming videos or if you have any questions if you found this video useful a thumbs up would be highly appreciated stay tuned for exciting new videos to ensure you don't miss out please subscribe and hit the notification Bell until next time thank you for [Music] watching [Music] around the rugged rocks the ragot Rascal rat
Info
Channel: Coding Money
Views: 13,916
Rating: undefined out of 5
Keywords: google gemini, gemini, gemini api, gemini api tutorial, how to use gemini api, google gemini ai api, how to get started with google gemini ai api, google gemini ai tutorial, google gemini pricing, how to install gemini ai, node js tutorial, gemini pro free api, chatgpt api, gemini pro api, coding money, google gemini ai api tutorial, google ai studio tutorial, google gemini ai, google ai, google ai api, gemini api integration, gemini vision api, prompt engineering
Id: heXuVxXG5Vo
Channel Id: undefined
Length: 13min 39sec (819 seconds)
Published: Fri Dec 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.