Getting Started With Google Gemini API In Python (Vertex AI) | Full Tutorial

Jie Jenn
14 Dec 202311:29

TLDRThis tutorial demonstrates how to utilize Google's AI Gemini API in Python, highlighting its capabilities over GPT by OpenAI. It covers the free usage period, pricing structure, and the necessary steps to set up the API, including creating a Google Cloud Platform account, generating a service account with appropriate permissions, and setting up an environment variable for authentication. The script showcases an example of using the API to identify a landmark by processing image inputs and constructing a prompt for the Gemini Pro model to generate content, successfully guessing the location of the Golden Gate Bridge in San Francisco, United States.

Takeaways

  • 🌟 Google's AI Gemini API is a powerful model, more capable than GPT by OpenAI.
  • 🆓 The API is free to use until January 15, 2024, after which pricing will apply based on input and output.
  • 🔧 To begin, sign up for a Google Cloud Platform account and create a new project.
  • 👤 Create a service account within the Google Cloud Platform for API access, and assign appropriate roles.
  • 🔐 Download the service account JSON file and set it as an environment variable named 'Google_APPLICATION_CREDENTIALS'.
  • 📦 Install the required Python package 'google-cloud-aiplatform' with a version equal to or greater than 1.38.
  • 🔄 Use the 'requesters' library for making API requests and 'vertexai' library for accessing the generative model.
  • 🏙️ For image input, ensure all files have the same extension to avoid compatibility issues with the API.
  • 📝 Construct the prompt by referencing the model object and using the 'generate_content' method with the 'content' parameter.
  • 🤖 The AI model can infer missing information, such as the name and location of a landmark, from provided image data.
  • 📈 Review the API documentation for detailed information on available models and parameters.
  • 🎥 Stay tuned for more tutorials on using AI models for various projects with both OpenAI and Google Gemini API.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is how to use Google's AI Gemini API in Python.

  • How does the Gemini AI model compare to GPT by OpenAI?

    -Gemini AI is considered to be more capable than GPT by OpenAI, as mentioned in the video.

  • Is there a cost associated with using the Gemini API?

    -The API is free to use until January 15, 2024, after which there will be charges based on usage.

  • What are the three different input types supported by the Gemini model?

    -The Gemini model supports image input, video input, and text input.

  • How much does the output cost for the Gemini API?

    -The output is charged at a rate of 0.0005 cents per thousand characters.

  • What is the first step in getting started with the Gemini AI model in Python?

    -The first step is to sign up for a Google Cloud Platform account.

  • What is a service account in the context of Google Cloud Platform?

    -A service account in Google Cloud Platform is equivalent to a user in AWS or a resource group in Azure, used to grant access to the API.

  • How is the account credential saved and used in the Python script?

    -The account credential is saved as a JSON file and used by setting an environment variable named 'Google application credentials'.

  • What is the name of the Python package needed to access the Gemini Pro model?

    -The package name is 'Google-cloud-aiplatform'.

  • How does the prompt for the Gemini AI model work?

    -The prompt references the model object and uses the generate content method, providing information about the images to help the AI infer the missing details.

  • What was the result of the example given in the video?

    -The Gemini AI model correctly identified the landmark as the Golden Gate Bridge in San Francisco, United States from the provided image data.

Outlines

00:00

🚀 Introduction to Google's AI Gemini API

This paragraph introduces the viewer to Google's AI Gemini API, highlighting its capabilities and comparing it to OpenAI's GPT model. The script explains that Gemini AI is a more advanced AI model developed by Google and is available for free until January 15, 2024. The video will demonstrate how to use this API in Python, and the first step is to look at the pricing model, which includes three different input types: image, video, and text. The costs are based on the output, charged at a rate of 0.0005 cents per thousand characters. The script then guides the viewer through the process of getting started with the API, including signing up for a Google Cloud Platform account, creating a project, and setting up a service account with appropriate permissions. It emphasizes the importance of saving the account credentials as a JSON file and creating an environment variable for the API to authenticate the user's account.

05:02

🛠️ Setting Up and Using the Gemini Pro Model

This paragraph delves into the technical setup required to access and use the Gemini Pro model. It instructs the viewer to install the necessary Python packages, such as 'Google-cloud-aiplatform' and 'requesters', and provides the correct command for installation. The video then demonstrates how to import the required libraries and create a helper function to handle image URLs and create image objects for Google's AI. The script also discusses the importance of consistent file extensions for the input files and defines the model to be used, which is the Gemini Pro version. The main focus is on creating a prompt for the AI model, explaining the parameters and how to structure the content list to reference image data and leave blank spaces for the AI to fill in. The paragraph concludes with a live demonstration of running the code and explaining the output, showcasing the AI's ability to identify the location of an image based on the provided data.

10:06

🎉 Conclusion and Future Tutorials

In this final paragraph, the script wraps up the tutorial by demonstrating the output from the AI model, which correctly identifies the city, country, and landmark based on the provided image data. The video creator expresses intent to cover more examples and projects in the future, focusing on using open AI and Google's Gemini AI API. The script encourages viewers to subscribe to the channel for more content and to like and share the video if they found it helpful. The video ends on a positive note, with a friendly farewell to the viewers.

Mindmap

Keywords

💡Google's AI Gemini API

Google's AI Gemini API is a service provided by Google that allows developers to integrate advanced AI capabilities into their applications. In the context of the video, it is a powerful model that is said to be more capable than GPT by OpenAI. The API enables developers to process various input types such as image, video, and text, and utilize the AI's capabilities for tasks like content generation, analysis, and prediction.

💡Python

Python is a widely-used high-level programming language known for its readability and ease of use. In the video, Python is the chosen programming language to demonstrate how to interact with Google's AI Gemini API. It is used to write scripts that can send requests to the API, process the responses, and implement AI-driven functionalities in applications.

💡Google Cloud Platform

Google Cloud Platform (GCP) is a suite of cloud computing services offered by Google, which includes a variety of hosting and computing services. In the tutorial, signing up for a GCP account is the first step to access and use the Gemini AI model, as it requires creating a project and managing credentials within the platform.

💡Service Account

A service account in the context of GCP is a special type of account used to provide identity and authentication for applications and services. It allows these applications to access GCP resources securely. In the video, creating a service account is necessary to generate credentials that will be used to authenticate API requests to Google's AI Gemini.

💡JSON Key File

A JSON key file is a type of file used by GCP to store service account credentials in JSON format. This file contains sensitive information that allows your application to identify itself to GCP services when making API requests. In the tutorial, the JSON key file is downloaded and saved as an environment variable to authenticate the use of the Gemini API.

💡Environment Variable

An environment variable is a variable in the computer's operating system that stores data about the environment, such as the paths to certain directories or the settings for certain programs. In the video, setting an environment variable named 'Google Application Credentials' is crucial for the Python script to authenticate with the Gemini API using the path to the JSON key file.

💡Image Input

Image input refers to the capability of the Gemini AI model to process and analyze images. In the video, the user aims to use the image input feature to guess the location of a landmark based on images provided, showcasing the AI's ability to understand and interpret visual data.

💡Generative Model

A generative model is a type of AI model that can create new data instances, such as text or images, that are similar to the data it was trained on. In the context of the video, the Gemini Pro model is a generative model that can generate content based on the input it receives, like image data, and fill in missing information.

💡API Request

An API request is a message sent to an API's endpoint (a specific URL) to access a service or perform an action. In the video, the API request is made using Python to send image data to the Gemini AI model, which then processes the request and returns a response with the generated content.

💡Content Generation

Content generation refers to the process of creating new content, such as text, images, or videos, using AI models. In the video, the Gemini AI model's content generation capability is utilized to predict the name and location of a landmark based on the information provided in the prompt, demonstrating the model's understanding and ability to generate relevant content.

💡Response Output

Response output is the result or data returned by an API in response to a request. In the video, the response output from the Gemini AI model includes the predicted city, country, and landmark name, which are part of the content generated by the AI based on the input prompt.

Highlights

Introduction to Google's AI Gemini API in Python

Gemini AI is Google's latest AI model, more capable than OpenAI's GPT

Free access to the API until January 15, 2024

Gemini Pro model with three input types: image, video, and text

Pricing details for input and output usage

Sign up for Google Cloud Platform account

Creating a new project and service account

Assigning the 'owner' role for API access

Saving the service account credentials as a JSON file

Setting up an environment variable for credentials

Installing the required Python package for Gemini API access

Using the 'requesters' library for API requests

Importing the generative model library and image class

Creating a helper function for image processing

Ensuring uniform file extensions for consistency

Defining the Gemini Pro model for the task

Constructing the prompt for the AI model

Running the code to guess the location of a landmark

AI model successfully identifies San Francisco's Golden Gate Bridge

Future tutorials will cover more AI model applications