🐱

gemini-2.0-flashのチュートリアルやってみた

2025/02/16に公開

はじめに

初投稿になります。
gemini-2.0-flashが相当使用しなければ無料(料金体系)で使用できるそうなので、公式チュートリアルをやってみました。
今回はpythonを用いて、画像と文言の入力に対してレスポンスが返ってくる所までの記録を残したいと思います。
まだ触り始めたばかりでチュートリアルをなぞっているだけですが、生成AI関連の実装に興味はあるが、まだ手が出せてない人への参考になれたらうれしいです。

大まかな流れ

基本的にはこちらを参考にして進めます。
Gemini API ライブラリをインストール

Google AI Studio で Gemini API キーを取得

疎通確認としてリクエスト送信

画像と文言の入力に対してレスポンスが返ってくる実装
という流れで書いていきます。

Gemini API ライブラリをインストール

pip install -q -U google-genai

python3.9以降でないと対応していないそうです。
自分はpipのバージョンアップを要求されました。

Google AI Studio で Gemini API キーを取得

こちらでAPIキーを作成してメモっておいてください。
github等に上げる場合はAPIキーを公開しないよう気を付けてください。
今回は.envファイルに環境変数を設定・そこからAPIキーを取得・.gitigonoreに「env」を記載しgithubに上げないようにしました。

疎通確認としてリクエスト送信

  • .envファイルを作成して環境変数を設定
  • 環境変数を取得するファイル(settings.py)を作成
# coding: UTF-8
import os
from os.path import join, dirname
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)

AP = os.environ.get("GEMINI_API_KEY")

dotenvがインストールされていなければインストールしてください。

$ pip install python-dotenv
  • 疎通確認用にリクエストを送るファイルを作成
# coding: UTF-8
import settings
from google import genai

GEMINI_API_KEY = settings.AP
client = genai.Client(api_key=GEMINI_API_KEY)

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works"
)
print(response.text)
  • 作成したファイルを実行
    • 以下のようなログが出力されたら疎通できています!
Okay, let's break down how AI works.  It's a broad field, so I'll focus on the core concepts and then touch on different types of AI.  Think of AI as trying to make computers do things that normally require human intelligence.~

画像と文言の入力に対してレスポンスが返ってくる実装

  • 適当な画像を格納
    • 今回はフリー素材の猫ちゃん画像を用いました。
  • 画像と文言を入力してレスポンスをログ出力する実装
    • 猫ちゃん画像と「猫について教えて」という文言を英語で入れてみます。
# coding: UTF-8
import settings
from google import genai
from PIL import Image

GEMINI_API_KEY = settings.AP

client = genai.Client(api_key=GEMINI_API_KEY)

image = Image.open("./img/cat.jpg")
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=[image, "Tell me about this cat"])
print(response.text)
  • 作成したファイルを実行
Here's a description of the cat in the image:

**Appearance:**

*   **Coat:** It has a beautiful tabby coat with a mix of brown and tan colors, and distinct dark stripes.
*   **Eyes:** It has striking green eyes.
*   **Collar:** It's wearing a patterned collar with a small charm, possibly shaped like a fish.

**Posture and Context:**

*   **Relaxed:** The cat is lying down and appears relaxed, suggesting it feels safe and comfortable.
*   **Being Pet:** A person's hand is gently petting the cat, further indicating a positive and affectionate interaction.

**Overall Impression:** The cat seems well-cared-for and is likely enjoying the attention and affection it's receiving. It is a beautiful and contented animal.

上記のログから首に魚のチャームが付いている等、細かいところも観察していることがわかりました!

今回は本当に簡単な導入部分ですが、今後色々試していけたら楽しいと感じました!
ぜひ遊んでみてください!!

追記

入力テキストを「この猫について教えてください」に変更したら、日本語で返ってきました!

この猫は茶色の虎縞模様の猫で、緑色の目と青い首輪に魚のチャームが付いています。誰かが猫の頭と背中をなでています。

日本語で入力すれば日本語で返ってきてくれそうでした!

補足

今後色々いじっていくと思いますが、今回のリポジトリも載せておきます。
https://github.com/yymasaki/try-gemini

Discussion