Using Gemini (“Google’s Largest & Most Capable AI Model”) With Python
An Extremely “Bare Bones” Example

If you don’t know what Gemini is, it’s essentially a collection of Google’s latest large language models (LLMs). In this very brief article I will show you how to get started using Gemini-Pro (one of the Gemini LLMs).
This article is geared towards those of you who might have an interest in LLMs and are newer to Python or Google API’s but want to start playing around with Gemini in your Python projects. Keep in mind that Google provides excellent documentation for what I’m about to show you but I would consider their explanation just a notch above “bare bones”.
The goal is to get you up and running so let’s make this short and sweet.
Setup
1. Use Python 3.9+
Make sure to install Python 3.9 or later. On Mac you can use something like pyenv to make sure you have the desired version installed.
2. Install PyCharm
In general this isn’t necessary but it’s great for developing with Python and you can follow the steps in this article more closely.
3. Create a new Python project & virtual environment
Open PyCharm and create a new project making sure to select whatever Python version you’re using (remember to use 3.9+) as the base interpreter. It’s also good practice to create a new environment (if you’re using PyCharm, the environment will automatically be activated when you open the project in PyCharm).

4. Install google-generativeai
Open a terminal (if using PyCharm you can type Option + F12 on Mac, and Alt + F12 on Windows) and install google-generativeai by typing the following into the terminal:
pip install -q -U google-generativeai

5. Get an API key
You need an API key from Google. To get a key you need a Google account but that is it! Simply follow the instructions here and copy the API key:

6. Add the API key as an environment variable
There are many ways to do this but if you’re following along then you can simply follow these instructions on stack overflow. Let’s assume you name your environment variable GOOGLE_API_KEY.

7. Create a new script and import necessary packages
Create a new Python script with the following content:
import google.generativeai as genai
import os
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)Run the script. You should see the names of the available models:
models/gemini-pro models/gemini-pro-vision
Start Using The Models!
Since this is meant as a super quick start guide I won’t go into detail here. You can read more about what you can do here, but here’s an example of how you can use the gemini-pro’s text-to-text functionality:
Prompt:
What are the main kinds of mountaineering accidents (at a high level)?
Code:
import google.generativeai as genai
import os
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("What are the main kinds of mountaineering accidents (at a high level)?")
print(response.text)Response:
1. **Falls:** This is the leading cause of mountaineering accidents, accounting for over 50% of all fatalities. Falls can occur from a variety of factors, including:
* Loss of balance
* Slips on snow or ice
* Rockfalls
* Avalanches
* Equipment failure
2. **Avalanches:** Avalanches are a major hazard for mountaineers, especially in the spring and early summer when the snowpack is unstable. Avalanches can be triggered by a variety of factors, including:
* Overloading the snowpack with too much weight
* Rapid warming of the snowpack
* Heavy snowfall
* Earthquakes
* Volcanic eruptions
3. **Rockfalls:** Rockfalls are another common hazard for mountaineers, especially in areas with loose or unstable rock. Rockfalls can be triggered by a variety of factors, including:
* Earthquakes
* Heavy rain or snow
* Freezing and thawing
* Human activity
4. **Exposure:** Exposure to cold, wind, and sun can also be a major hazard for mountaineers, especially at high altitudes. Exposure can lead to a variety of health problems, including:
* Hypothermia
* Frostbite
* Sunburn
* Dehydration
5. **Altitude sickness:** Altitude sickness is a common problem for mountaineers who ascend to high altitudes too quickly. Symptoms of altitude sickness can include:
* Headache
* Nausea
* Vomiting
* Diarrhea
* Fatigue
* Shortness of breath
* Confusion
* ComaWhat’s Next?
Now that you know the basics, it’s time to learn more about model capabilities. For example, Gemini-Pro can text from text inputs (as in the example above), but it can also generate text from text AND image inputs, and even has chat-like capabilities.
