avatarKarla Hernández

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2897

Abstract

ai.google.dev/tutorials/python_quickstart#setup_your_api_key">here</a> and copy the API key:</p><figure id="8eb3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Q32acLuzCGPXtE-q5vh-tg.png"><figcaption>Copy the key in the greyed out box here.</figcaption></figure><h2 id="70f2">6. Add the API key as an environment variable</h2><p id="84d6">There are many ways to do this but if you’re following along then you can simply follow <a href="https://stackoverflow.com/a/42708480/21829345">these instructions on stack overflow</a>. Let’s assume you name your environment variable <code>GOOGLE_API_KEY</code>.</p><figure id="3726"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*mke9tQIIjByRjM3cWPeVmQ.png"><figcaption></figcaption></figure><h2 id="bfee">7. Create a new script and import necessary packages</h2><p id="f971">Create a new Python script with the following content:</p><div id="d7b9"><pre><span class="hljs-keyword">import</span> google.generativeai <span class="hljs-keyword">as</span> genai <span class="hljs-keyword">import</span> os

GOOGLE_API_KEY = os.getenv(<span class="hljs-string">'GOOGLE_API_KEY'</span>) genai.configure(api_key=GOOGLE_API_KEY)

<span class="hljs-keyword">for</span> m <span class="hljs-keyword">in</span> genai.list_models(): <span class="hljs-keyword">if</span> <span class="hljs-string">'generateContent'</span> <span class="hljs-keyword">in</span> m.supported_generation_methods: <span class="hljs-built_in">print</span>(m.name)</pre></div><p id="77e4">Run the script. You should see the names of the available models:</p><div id="c44b"><pre>models/gemini-pro models/gemini-pro-vision</pre></div><h1 id="88e0">Start Using The Models!</h1><p id="5119">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 <a href="https://ai.google.dev/tutorials/python_quickstart">here</a>, but here’s an example of how you can use the gemini-pro’s text-to-text functionality:</p><p id="8f41">Prompt:</p><div id="606e"><pre>What are the main kinds of mountaineering accidents (at a high level)?</pre></div><p id="8426">Code:</p><div id="3e76"><pre><span class="hljs-keyword">import</span> google.generativeai <span class="hljs-keyword">as</span> genai <span class="hljs-keyword">import</span> os

GOOGLE_API_KEY = os.getenv(<span class="hljs-string">'GOOGLE_API_KEY'</span>) genai.configure(api_key=GOOGLE_API_KEY)

model = genai.GenerativeModel(<span class="hljs-string">'gemini-pro'</span>) response = model.generate_content(<span class="hljs-string">"What are the main kinds of mountaineering accidents (at a high level)?"</span>) <span class="hljs-built_in">print</span>(response.text)</pre></div><p id="f731">Response:</p><div id="8b2c"><pre><span class="hljs-bullet">1.</span> <span class="hljs-strong">Falls:</span> This is the leading cause of mount

Options

aineering accidents, accounting for over 50% of all fatalities. Falls can occur from a variety of factors, including: <span class="hljs-bullet"> *</span> Loss of balance <span class="hljs-bullet"> *</span> Slips on snow or ice <span class="hljs-bullet"> *</span> Rockfalls <span class="hljs-bullet"> *</span> Avalanches <span class="hljs-bullet"> *</span> Equipment failure

<span class="hljs-bullet">2.</span> <span class="hljs-strong">Avalanches:</span> 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: <span class="hljs-bullet"> *</span> Overloading the snowpack with too much weight <span class="hljs-bullet"> *</span> Rapid warming of the snowpack <span class="hljs-bullet"> *</span> Heavy snowfall <span class="hljs-bullet"> *</span> Earthquakes <span class="hljs-bullet"> *</span> Volcanic eruptions

<span class="hljs-bullet">3.</span> <span class="hljs-strong">Rockfalls:</span> 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: <span class="hljs-bullet"> *</span> Earthquakes <span class="hljs-bullet"> *</span> Heavy rain or snow <span class="hljs-bullet"> *</span> Freezing and thawing <span class="hljs-bullet"> *</span> Human activity

<span class="hljs-bullet">4.</span> <span class="hljs-strong">Exposure:</span> 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: <span class="hljs-bullet"> *</span> Hypothermia <span class="hljs-bullet"> *</span> Frostbite <span class="hljs-bullet"> *</span> Sunburn <span class="hljs-bullet"> *</span> Dehydration

<span class="hljs-bullet">5.</span> <span class="hljs-strong">Altitude sickness:</span> Altitude sickness is a common problem for mountaineers who ascend to high altitudes too quickly. Symptoms of altitude sickness can include: <span class="hljs-bullet"> *</span> Headache <span class="hljs-bullet"> *</span> Nausea <span class="hljs-bullet"> *</span> Vomiting <span class="hljs-bullet"> *</span> Diarrhea <span class="hljs-bullet"> *</span> Fatigue <span class="hljs-bullet"> *</span> Shortness of breath <span class="hljs-bullet"> *</span> Confusion <span class="hljs-bullet"> *</span> Coma</pre></div><h1 id="00b7">What’s Next?</h1><p id="d0d4">Now that you know the basics, it’s time to <a href="https://ai.google.dev/models">learn more about model capabilities</a>. 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.</p></article></body>

Using Gemini (“Google’s Largest & Most Capable AI Model”) With Python

An Extremely “Bare Bones” Example

Image Created By Author Using Midjourney AI

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:

Copy the key in the greyed out box here.

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
    * Coma

What’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.

Google Gemini
Python
AI
Llm
API
Recommended from ReadMedium