Demo
You explored some of the online tools available to work with the Gemini API. Now you’ll learn how to use it in your own local environment. Gemini supports many languages, you’ll be using Python.
First, open Visual Studio Code and navigate to your project folder. In the root project, create a new file and name it .env.
Get the API Key from AI Studio. Add this line in Visual Studio Code.
GOOGLE_API_KEY = "YOUR API KEY"
This puts the API Key where it’s easy to access from your script. Save the file. If your project is connected to GitHub, don’t forget to include this file in .gitignore to keep your API Key private. I’ll be deleting this key after this demo.
Now, create a new Jupyter Notebook file. Save this file with the name 03-text-generation-with-google-gemini.ipynb. Save this file and in the first code cell, add the following code:
%pip install -U -q google-generativeai
%pip install python-dotenv
So, these statements will install the Google Generative AI libraries and the Python’s dot environment. Click Execute Cell on the left of this cell here. Select or create a Python environment. I’ll select my virtual environment. Now, wait for the commands to execute and install the libraries. You may get a note to restart the Kernel. Click Restart at the top.
In the next cell, add the following code:
import google.generativeai as genai
import os
from dotenv import load_dotenv
These statements import the libraries, so you’ll need to load the API key and connect to the API. Execute this cell. You’ll see a text message right below the execution that looks like this:
0.6s
This confirms your code runs successfully in point 6 seconds.
In the next cell, add the following:
load_dotenv()
os.environ.get('GOOGLE_API_KEY')
genai.configure(api_key=os.environ.get('GOOGLE_API_KEY'))
The API key is read from the file and configured with your generative AI environment. Execute this line. When your code executes successfully, add another code cell:
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)
This code prints all the models available with Google’s generative AI if the model also supports generateContent.
Execute, and you’ll see a list that is similar to this:
models/gemini-1.0-pro
models/gemini-pro
models/gemini-1.0-pro-001
models/gemini-1.0-pro-vision-latest
models/gemini-pro-vision
models/gemini-1.5-pro-latest
models/gemini-1.5-pro-001
models/gemini-1.5-pro
models/gemini-1.5-pro-exp-0801
models/gemini-1.5-pro-exp-0827
models/gemini-1.5-flash-latest
models/gemini-1.5-flash-001
models/gemini-1.5-flash-001-tuning
models/gemini-1.5-flash
models/gemini-1.5-flash-exp-0827
models/gemini-1.5-flash-8b-exp-0827
Now in a new code cell, add:
genai.get_model('models/gemini-pro-vision')
This line gets information on a specific pro-vision model. Execute and you can see specific information about the gemini-pro-vision model. The output displays many of the different parameters that you’ll learn about later.
In the next code cell, add:
model = genai.GenerativeModel('gemini-pro')
model is an instance of the Gemini pro model that you can now use to make API calls. Execute the code.
Now, add a cell and insert:
response = model.generate_content(
'What kind of safety features does Google Gemini API provide for
prompts'
)
This queries the API to generate content and stores it in the response object. Execute and then add another cell with the following:
print(response)
This prints the response object so you can see all the fields. Execute this cell. You’ll observe the response object is in a JSON format by default. The text attribute holds the answer to your query, and you can also see the different safety categories and probabilities. Also note the long output is truncated, and you have the option to open this output in a text editor. This was a safe prompt, and thus, you could see the response.
Now, in another code cell, add:
print(response.text)
This will print just the response text. Execute this cell. You’ll see a nicely formatted text with the safety filters. Some of these features are set to filter automatically, but you can also create your own custom filter. This helps protect the end users from harmful or offensive responses.
Now insert a new cell and add the following code:
response = model.generate_content('List some prompts that are flagged
as hate speech')
print(response)
This is an unsafe prompt. Execute it and in the JSON response, you’ll see that the hate speech category has a probability of medium, and the harm harassment category has a probability of high. Don’t worry if you get slightly different results here. Sometimes the model displays this value as medium and sometimes as high. What’s important is the model is blocking these values as either medium or high.
In the next code cell, add:
print(response.text)
This line of code is expected to print the response text. Execute and you’ll notice the text doesn’t print. Instead, there’s an error message like so:
ValueError: Invalid operation: The `response.text` quick accessor requires
the response to contain a
valid `Part`, but none were returned. Please check the
`candidate.safety_ratings` to determine if
the response was blocked.
This is because the response was blocked due to safety settings.
Now, let’s try another unsafe prompt:
response = model.generate_content('All purple people eaters are homicidal
maniacs!!!')
print(response)
Execute this line and see the hate speech is now medium and the harassment is also medium. But, these results are not very consistent from the model. Next you’ll see how to create a customized safety setting, if you ever encounter a situation where you need to block only the most harmful content.
In the next code cell, add the following code:
from google.generativeai.types import HarmCategory, HarmBlockThreshold
safety = {
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold
.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold
.BLOCK_ONLY_HIGH
}
response = model.generate_content(
'All purple people eaters are homicidal maniacs!!!',
safety_settings= safety
)
print(response)
This code imports the libraries that you need to change the safety settings. It then adjusts the hate speech and the harassment categories to block only the high probabilities. It then passes the safety settings to generate_content along with an unsafe prompt. Execute these code line by line and wait for the response to come up. You’ll notice that this is not blocked because it is now filtering the speeches as negligible.
Great job, you’ve created your own Python program and made requests to the Gemini API. You also experimented with safety settings. When a response is not generating as you expected, seeing if it was filtered by the safety settings is a very good first step for troubleshooting.
In the next section, you’ll learn about more parameters that you can use to customize your API call requests.