Intro to AI Engineering with Python (6/6) – Google Gemini

Hi and welcome to the last part of this tutorial series, where we will be taking a look at Google Gemini. Google Gemini is a powerful LLM (Large Language Model) just like ChatGPT.

It is designed to help you generate text, answer questions, and you can even have conversations with it. Just like ChatGPT, there are multi-modal capabilities available, allowing you to not just work with text but also with images, videos, and audio.

There are several models available but the two main current ones are Gemini 1.5 Pro, which is the flagship model, and Gemini 1.5 Flash, which is the smaller, faster, and roughly 10 times cheaper version of the flagship model.

Notice the similarities between gpt-4o and gpt-4o-mini, it’s basically the same concept, and prices are also roughly comparable.

So for this introduction, let’s look at an example of how we can use the Gemini API to ask a multi-modal question also involving an image. Before we can get started we’ll need an API key though.

Don’t worry, our costs will be negligible, and you’ll likely have a free trial option available as well. This is only really expensive if you scale up to a very large amount of automated requests or users or give video files as input.

Go to the Google AI Studio and you will be greeted with a screen something like this:

Login using your Google account, or create one if you don’t have one (it’s free!). Most people nowadays already have a Google account for Gmail so you can just log in straight away.

We’ll be taken to the next screen:

You can actually play around with Gemini in the web interface here, but as we don’t have much time in this single lesson, we’ll just focus on the API. See the full course later for more details.

You’ll find a πŸ”‘ Get API key button at the top left of the Google AI Studio interface:

Click on it and you’ll be taken to a page where you can manage your API keys:

On this new page, simply go ahead and click the πŸ”‘ Create API key button. It will ask you to select a project, which may seem a bit confusing:

Just go ahead and select something from the list by clicking inside the search box. Then click the button to create a key again and it will generate your API key:

Make sure you do not share your key and save it somewhere for later. You now likely have a Free of charge key, though these options could be dependent on locale or change over time.

As you can see, we have the option to Set up Billing. Your free key is very rate-limited with the number of requests you can make per minute and day, though you can easily follow along with the tutorial. You can always set up billing later to get a more powerful key.

The first thing we need to do is open back up our .env file and add our new key:

OPENAI_API_KEY=sk-YoUrApIkEy1234567890
GEMINI_API_KEY=AIYoUrApIkEy1234567890

Next, let’s install the Google Generative AI library:

pip install google-generativeai

You can liken this to the OpenAI library we used earlier, it will make it easier to interact with the Gemini API from our Python code.

Making a simple request

Now let’s create our first simple request to Gemini as a warm-up. Inside the root of your project directory, create a new file called simple_gemini_request.py:

πŸ“ Intro_to_AI_engineering
    πŸ“ output
    πŸ“„ .env
    πŸ“„ chat_gpt_request.py
    πŸ“„ generate_image.py
    πŸ“„ generate_speech.py
    πŸ“„ gradio_project.py
    πŸ“„ image_gen.ipynb
    πŸ“„ langchain_basics.py
    πŸ“„ simple_gemini_request.py     ✨ New file
    πŸ“„ test_audio.mp3
    πŸ“„ text_to_summarize.txt
    πŸ“„ transcribe_audio.py

Now let’s get started on our code. First, we’ll need to import the necessary libraries:

import os

import google.generativeai as genai
from dotenv import load_dotenv

We import os and load_dotenv from the dotenv library, this is for reading the API key just like with OpenAI. We also import the google.generativeai library as genai to make the name a bit easier to work with. Again, this genai object will be our main interface to interact with the Gemini API.

Now we’ll load the .env file and give the API key to the genai library. You already know how this works from our earlier exploits with OpenAI:

load_dotenv()
genai.configure(api_key=os.environ["GEMINI_API_KEY"])

The only difference is that genai uses a .configure() method to receive the api_key parameter.

Now I’ll create an object that contains our desired safety settings. I’ll explain this in more detail in the full course, but basically, Gemini has a pretty strict system to block potentially unwanted responses. As it can be a bit too strict at times, we want to set the settings a bit on the lower side:

SAFETY_SETTINGS = [
    {
        "category": "HARM_CATEGORY_HARASSMENT",
        "threshold": "BLOCK_ONLY_HIGH",
    },
    {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "threshold": "BLOCK_ONLY_HIGH",
    },
    {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_ONLY_HIGH",
    },
    {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_ONLY_HIGH",
    },
]

As you can see, there are four categories, and we set them all to block only the high-risk responses, which is a lot more lenient than the default settings.