Create a Google API Key for accessing to the Google generative AI models.
Google provides two API based access methods for Gemini:
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.
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.
You will be using the free plan that have rate restriction. These rate restrictions may change over time. Please go through it before proceeding.
Open the link to create the API Key.
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.
Use the path in your template folder for creating the notebook.
Your-template-folder/Gen-AI-Fundamentals/setup-test-api-keys.ipynb
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.
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('../')
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
# 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)
To try out the solution, follow the steps below:
Change the path to the API key file in the cell the cell titled Setup the environment
Run the cell titled Setup the environment
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!!!