Gemini モデルのパラメータを構成する

Gemini モデルを使用して画像からテキストを生成し、生成されたテキストを返します。この例では、モデル構成パラメータの設定方法を示しています。

コードサンプル

Python

このサンプルを試す前に、Vertex AI クイックスタート: クライアント ライブラリの使用にある Python の設定手順を完了してください。 詳細については、Vertex AI Python API のリファレンス ドキュメントをご覧ください。

Vertex AI に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

import base64
import vertexai

from vertexai.generative_models import GenerationConfig, GenerativeModel, Part

# TODO(developer): Update project_id and location
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-002")

# Load example image from local storage
encoded_image = base64.b64encode(open("scones.jpg", "rb").read()).decode("utf-8")
image_content = Part.from_data(
    data=base64.b64decode(encoded_image), mime_type="image/jpeg"
)

# Generation Config
config = GenerationConfig(
    max_output_tokens=2048, temperature=0.4, top_p=1, top_k=32
)

# Generate text
response = model.generate_content(
    [image_content, "what is this image?"], generation_config=config
)
print(response.text)

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルを検索およびフィルタするには、Google Cloud のサンプル ブラウザをご覧ください。