Create Projects with Generative AI for Free: Using Google Gemini API in Colab

Create Projects with Generative AI for Free: Using Google Gemini API in Colab

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:

  1. Click on the Secrets tab (the key icon).
  2. Select Gemini API keys and then Manage Keys in AI Studio. Alternatively, you can directly visit this URL .
  3. Click on Create 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:

  1. Go back to the Secrets tab.
  2. Click on Add new secret.
  3. Name your secret GOOGLE_API_KEY.
  4. Paste your API key into the value field.
  5. Ensure that Notebook access is enabled so your notebook can access the key.

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:

Using Google Gemini API for Free

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:

  • Experiment with different prompts and see how the model responds.
  • Explore more advanced features of the Gemini API.
  • Start building your own AI-powered projects or applications.

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!

Sola lin

CameoUI Designer

2 周

Send me connection please

回复

要查看或添加评论,请登录

Luciano Ayres的更多文章