Use Google Gemini from Python — Brand New SDK (Code example included!)
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:
- Get an API key from Google AI Studio.
- In Colab, add the key to the secrets manager under the “🔑” in the left panel, naming it GOOGLE_API_KEY.
- Pass the key to the SDK either through the environment variable
GOOGLE_API_KEY
or usinggenai.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
For text-only prompts, initialize the gemini-pro
model and generate content:
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Write me a medium blog article about google generative ai gemini-pro")
to_markdown(response.text)
Chat Conversations
Gemini supports freeform conversations across multiple turns. Utilize the ChatSession
class to simplify the process:
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
response = chat.send_message("Lets go back and forth to create a story about Gandalf the Grey, you start with one sentence.")
chat.history
# Continue the conversation
response = chat.send_message("He came across a very peculiar sort of creature, ones that live in burrows and hillside mounds.")
to_markdown(response.text)
response = chat.send_message("In one sentence, explain how a computer works to a young child.")
to_markdown(response.text)
# Concat full story
for txt in chat.history[1:]:
print(txt.parts[0].text)
In the midst of a peaceful morning, the wise wizard Gandalf the Grey embarked on a journey beyond the borders of Rivendell.
He came across a very peculiar sort of creature, ones that live in burrows and hillside mounds.
As Gandalf the Grey wandered through the rolling green hills, he stumbled upon a peculiar sight—a colony of industrious hobbits, scurrying about their daily tasks amidst their cozy burrows and lush gardens.
Generate Text from Image and Text Inputs
Explore the capabilities of the multimodal model gemini-pro-vision
by including an image:
img = PIL.Image.open('donuts.png')
model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content([
'''Is that a donut?
If so, give me a quote from a popular cartoon character that likes donuts.''',
img], stream=True)
response.resolve() # Concat streamed input if needed
to_markdown(response.text)
- Parts of this article were written using Generative AI
- Subscribe/leave a comment if you want to stay up-to-date with the latest AI trends.
- Earn $25 and 4.60% APY for FREE through my referral at SoFi Bank Here
Plug: Checkout all my digital products on Gumroad here. Please purchase ONLY if you have the means to do so. Use code: MEDSUB to get a 10% discount!