Exercise#2 Google API Key

Reference

Gemini documentation

Gemma family of models

Objective

Create a Google API Key for accessing to the Google generative AI models.

Google provides two API based access methods for Gemini:

  1. Google AI API A simple API interface that uses an API key for accessing the Google models such as Gemini. This requires you to use your Google Account Id. If you don’t have one please create it before proceeding.

  2. Google Cloud Vertex AI API Requires a Google Cloud Platform (GCP) account. This API is more complex and supports integration with other Cloud services, IAM, service accounts, etc.

In this exercise, you will use Google AI API i.e., method #1, which means that you don’t need to have a GCP account.

You may create API Key in your GCP account i.e., use method #2. If you do so, make sure to enable Gemini model access in your GCP account otherwise you wont be able to access it.

Steps

1. Go through the Google AI pricing plans

You will be using the free plan that have rate restriction. These rate restrictions may change over time. Please go through it before proceeding.

Pricing

2. Generate the API Key

Open the link to create the API Key.

Google API

3. Add it to the key file

Copy/Paste the key to the key file you created in the earlier exercise.

If you just want to test the setup and not code, you may skip steps #4 #5 and go straight to the solution below.

4. Create a notebook

Use the path in your template folder for creating the notebook.

Your-template-folder/Gen-AI-Fundamentals/setup-test-api-keys.ipynb

5. Copy/paste the code in notebook cells & run it

Gemini model variants

Don’t worry about the code itself. You will learn HOW it is working later. At this time our objective is to ensure that our API key setup is working.

Setup the environment
from dotenv import load_dotenv
import sys
import json

# CHANGE THIS TO POINT TO YOUR OWN FILE
# Load the file that contains the API keys 
load_dotenv('C:\\Users\\raj\\.jupyter\\.env')

# Setting path so we can access the utils folder
sys.path.append('../')
Setup the client to the model
from utils.create_llm import create_google_llm

# Try out the Gemini Flash model
# model="gemini-1.5-flash" - created by default
llm = create_google_llm()

# Uncomment to create the pro model
# llm=create_google_llm(model="gemini-1.5-pro")

llm
Invoke the model
# Query to be sent to model
query = "explain LLM in 5 sentences"

# Invoke the model with the query
response = llm.invoke( query)

# Print the response
print(response)

Solution

To try out the solution, follow the steps below:

  1. Open the notebook in Jupyter

setup-test-api-keys

  1. Change the path to the API key file in the cell the cell titled Setup the environment

  2. Run the cell titled Setup the environment

  3. Run the cell titled Google API key

The solution code should work if everything was set up as per the instructions in section on Development environment setup and in this section. You will need to address the issue based on the error. MAKE SURE TO ADJUST THE PATH TO THE KEY FILE!!!