Create Projects with Generative AI for Free: Using Google Gemini API in Colab
Luciano Ayres
Engineering Manager @ AB InBev | Author of Digital Leadership: Empowering Teams In The New Era | AWS Certified | Azure Certified
I firmly believe that generative AI should be free for everyone. Making these powerful tools accessible empowers creativity, learning, and innovation across the globe. Luckily, we have options to tap into generative AI without spending a dime.
Today, I'll show you how to access Google's Gemini API for free and use it in a Google Colab notebook. While there are some rate limits on the free tier, it's perfect for your study projects and experiments. Let's dive in!
Setting Up Your Google Colab Notebook
First things first, open up Google Colab and create a new notebook. On the left sidebar, you'll see a key icon, this is the Secrets tab. We'll use this to securely store our API key.
Generating a Gemini API Key
To get your Gemini API key:
Testing Your API Key
After generating the key, you'll receive a cURL command to test it:
curl \
-H 'Content-Type: application/json' \
-d '{"contents":[{"parts":[{"text":"Explain how AI works"}]}]}' \
-X POST 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=YOUR_API_KEY'
Replace YOUR_API_KEY with your actual API key and run this command in your terminal. If everything works, you'll receive a response from the Gemini model.
Note: This code snippet is also available in the Secrets tab in Google Colab.
Integrating the API Key into Your Notebook
To use the API key in your Colab notebook:
Now, in your first code cell, retrieve the API key:
from google.colab import userdata
myKey = userdata.get('GOOGLE_API_KEY')
This stores your API key in the myKey variable. Be careful not to print or share this variable to keep your key secure.
Important: Deleting the secret from your notebook doesn't invalidate the API key. To completely remove it, go back to AI Studio and delete it there.
Making Your First API Call
In a new code cell, paste the following code:
import requests
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
headers = {
'Content-Type': 'application/json'
}
data = {
"contents": [
{
"parts": [
{
"text": "Explain how AI works"
}
]
}
]
}
response = requests.post(f"{url}?key={myKey}", headers=headers, json=data)
print(response.text)
Run the cell, and if everything went well, you should see a response from the Gemini model explaining how AI works.
Access the Google Colab Notebook
To make things even easier, I've prepared a Google Colab notebook with all the code and steps outlined above. You can access it here:
Simply click on the link to open the notebook in Colab. You can run the cells directly or make a copy to your own Google Drive to modify and experiment as you like.
Where to Go from Here
Fantastic job! You've successfully accessed free generative AI using the Google Gemini API in Colab. Now you can:
For more information and inspiration, check out the Gemini API Docs .
Join the Movement for Free Generative AI
By using and promoting free AI resources, you're supporting a community that believes in open access to technology. Let's continue advocating for generative AI tools to be available to everyone, fostering innovation and learning without barriers.
I hope you found this tutorial helpful. Happy coding, and enjoy exploring the exciting world of generative AI for free!
CameoUI Designer
2 周Send me connection please