1
0

Gemini API サンプルコード

Posted at
import os

import google.generativeai as gemini
from dotenv import load_dotenv
from pathlib import Path

# .env ファイルを読み込む
load_dotenv()
# Gemini API キーを設定
gemini.configure(api_key=os.getenv("API_KEY"))

# モデルを準備
model = gemini.GenerativeModel("gemini-pro")
response = model.generate_content("鯖の塩焼きの健康効果を教えてください。")
print(response.text)
# プロンプトに画像を使用するためのモデルを準備
model = gemini.GenerativeModel("gemini-pro-vision")

img = [
    {
        "mime_type": "image/png",
        "data": Path(
            "/Users/username/Desktop/sample_gemini/hogehoge.png"
        ).read_bytes(),
    }
]

prompt = "画像には何が写っていますか?"
response = model.generate_content(contents=[prompt, img[0]])
print(response.text)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0