In this demo, you’ll explore Google Colab Notebook. You’ll first need your API Key. Navigate to AI Studio and then click Get API Key on the left. In the list of API keys, click the API Key you created using your Project ID. Select Show projects in my organization and then under your project MyFirstGemini, click the API Key. Click Copy, this copies your API key to the clipboard and you’ll paste it as a secret to execute the Python Colab Notebook samples.
Navigate to the Gemini API Python Examples. Colab Notebooks have a simple cloud-based interface and are intuitive. To notice some of the neatest features, expand File at the top. There are options to Save a notebook in Drive and Save a copy in GitHub. Notebooks seamlessly integrate with Drive and Github. Under View, notice the Executed code history, and under Tools, there’s a Diff notebooks option that can compare two notebooks with diff.
In the center of the webpage, there are different options View on Google AI, Run in Google Colab and View source on GitHub to view or run this notebook. Along the left, there are some more options. Click the key icon to open Secrets. Click Add new secret. Type GOOGLE_API_KEY and paste your API key in the Value cell. Toggle the Notebook Access switch to use this key. You can also store multiple secrets in this section.
Next, you’ll install the Google Generative AI package. Close Secrets, scroll down the Colab until you find the Python code to install. Find this install python SDK:
!pip install -q -U google-generativeai
and click Run cell on the left.
Wait for the execution to complete. This will install the package in your current virtual environment.
When the installation is complete scroll down to Import packages. Click Run cell on each of these two code cells in order. These lines of code import libraries and your API key. It also declares a function to convert the response to Markdown.
Now, come down to Set up your API key section. Under the code, make sure that you change the key name to Google_API_Key. Run the code cell. This retrieves your API key and configures the GenAI object to use this key when making requests. To test if it works, click Run cell on the next code segment under List Models. The output will list all the available GenAI models.
Next, scroll down to Generate text from text inputs section. The first code cell selects a GenAI model. Click Run cell, and this will select gemini-1.5-flash as the model you’ll use.
Run the next code block as below:
%%time
response = model.generate_content("What is the meaning of life?")
This code calls the model’s API with the prompt, “What is the meaning of life?” and displays the model’s time to execute this code. Scroll down to the next code block and run the markdown function. It prints the text
of the response
object after it’s been converted to the markdown format by to_markdown
method.
Then, scroll down and execute the next code block to check prompt_feedback
. This displays the safety settings for the prompt. If you didn’t get a response here, come down to the next code line:
response.prompt_feedback
This will check if the response was blocked due to safety concerns regarding the prompt.
You can modify the examples, too. Scroll up to the previous code block with the prompt What is the meaning of life?.
%%time
response = model.generate_content("Show me a good chocolate chip cookie
recipe")
Run this code and and run the next line of code also. You’ll see the response is now changed with the changed prompt.
You can use these playgrounds and samples and change the code to experiment! You can click any of the samples in the Table of contents on the left and give it a try.
Click File and New notebook in Drive to open a new notebook. In this new notebook, there are Code and Text buttons at the top. Clicking these buttons can insert a new cell as code or text. The text cell is either plain text or nicely formatted text using Markdown. You can write Python code in the code cells or generate code with an AI model. Clicking on any of these cells also gives more options, like delete. If you have more code cells, you can use these options to move up or down. You can format the notebook however you like using these options or using Markdown to insert helpful comments explaining your code.
I’ll delete these unwanted cells. Next, you can click on Secrets in the left and enable your API key for this notebook. Close the secrets section and in the first line of code enter:
!pip install -q -U google-generativeai
Now, import the useful packages.
import pathlib
import textwrap
import google.generativeai as genai
from IPython.display import display
from IPython.display import Markdown
Insert another code cell and write a function to format the text using Markdown.
def to_markdown(text):
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
Now, in the next code cell, import your API Key using userdata
.
from google.colab import userdata
GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
Next, insert a code cell and then type:
model = genai.GenerativeModel('gemini-1.5-pro')
response = model.generate_content("Hello Gemini! It's lovely to meet you!
How are you today?")
to_markdown(response.text)
print(response.text)
That’s it. Run this code cell by cell. I made a small mistake here. I’ll just correct it and run the second cell.
You’re now familiar with Colab and learned to configure API Keys, modify existing samples and create new notebooks and save them to your Google Drive.
Colab can also download notebooks to your desktop, save it to GitHub or GitHub Gist. You can then share it and collaborate with others.
Colab is a quick, convenient and powerful tool. Great job exploring its features!