Use Google Gemini from Python — Brand New SDK (Code example included!)

Abish Pius
3 min readDec 15, 2023

Google’s Gemini API introduces a powerful and versatile tool for data scientists and developers to tap into the capabilities of large language models. In this blog post, we’ll explore the Python SDK for the Gemini API and demonstrate its usage through various scenarios, ranging from text-only prompts to multi-turn conversations and multimodal inputs.

Colab Notebook Here: Google Gemini Starter

Prerequisites

Before diving into the exciting world of Gemini, ensure your development environment meets the following requirements:

  • Python 3.9+
  • Jupyter installed for running notebooks

Setup

Install the Python SDK

Begin by installing the Python SDK for the Gemini API using pip:

pip install -q -U google-generativeai

Import Packages

Import the necessary packages for working with the Gemini API:

import pathlib
import textwrap
import google.generativeai as genai
# Used to securely store your API key
from google.colab import userdata
from IPython.display import display
from IPython.display import Markdown

Setup Your API Key

Before interacting with the Gemini API, obtain an API key and configure it:

  1. Get an API key from Google AI Studio.
  2. In Colab, add the key to the secrets manager under the “🔑” in the left panel, naming it GOOGLE_API_KEY.
  3. Pass the key to the SDK either through the environment variable GOOGLE_API_KEY or using genai.configure(api_key=...).

List Models

Explore the available Gemini models using the list_models method:

for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)

Gemini provides models like gemini-pro optimized for text-only prompts and gemini-pro-vision optimized for text-and-images prompts.

Generate Text from Text Inputs

--

--

Abish Pius

Data Science Professional, Python Enthusiast, turned LLM Engineer