2
2

More than 1 year has passed since last update.

Vertex AIのPython APIによる利用(Workbench/Colab)

Posted at

目的

Google Cloudの生成AIであるVertexAIの機能をPythonからのAPIコールで試してみる
実行環境として、Vertex AI WorkbenchとGoogle ColabからのPythonで実行する

Vertex AI Workbenchとは?

データサイエンティストや機械学習エンジニアが機械学習(ML)モデルの開発、トレーニング、デプロイメントを効率的に行えるように設計された環境で、Jupyter Notebookをベースにしたインタラクティブな環境を提供し、コードの実行、データの可視化、モデルの評価などが一元的に行える。

Google Colabとは?

Googleが提供するクラウドベースのインタラクティブな環境で、Pythonプログラミングとデータサイエンス、機械学習のタスクに使用される。ColabはJupyter Notebookの一種であり、ウェブブラウザ上で直接コードを書き、実行することができる。

事前準備

APIを有効化する
貼り付けた画像_2023_08_29_10_37.png

貼り付けた画像_2023_08_29_10_37.png

ノートブックの作成

貼り付けた画像_2023_08_29_10_40.png

貼り付けた画像_2023_08_29_10_44.png

Jupyter LABの起動

貼り付けた画像_2023_08_29_10_50.png

貼り付けた画像_2023_08_29_10_50.png

クライアントライブラリの導入

下記を入力し、実行する

!pip install google-cloud-aiplatform --upgrade --user

貼り付けた画像_2023_08_29_10_55.png

カーネルをリロードする
貼り付けた画像_2023_08_29_12_10.png

Workbench(JupyterLAB)からのAPIコールの実行

GUIで下記の様に実施することをAPI経由で実施する
単純に「hello」とチャットする
貼り付けた画像_2023_08_29_13_21.png

projectlocationは実行環境の値にする
parametersについては、下記を参照
https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/text

GUIの右側にサンプルコードがあるが、そのまま実行しても動かないので少し手を加えたのが下記

from google.colab import auth as google_auth
google_auth.authenticate_user()

import vertexai
from vertexai.language_models import ChatModel, InputOutputTextPair

vertexai.init(project="generative-ai-396008", location="us-central1")
chat_model = ChatModel.from_pretrained("chat-bison@001")
parameters = {
    "candidate_count": 1,
    "max_output_tokens": 256,
    "temperature": 0.2,
    "top_p": 0.8,
    "top_k": 40
}
chat = chat_model.start_chat()
response = chat.send_message("""hello""")
print(f"Response from Model: {response.text}")

実行してみる

GUI操作と同様にレスポンスが確認できた
貼り付けた画像_2023_08_29_13_37.png

Google ColabからのAPIコールの実行

ノートブックの作成
貼り付けた画像_2023_08_29_13_41.png

ライブラリの導入

!pip install "shapely<2.0.0" 
!pip install google-cloud-aiplatform --upgrade

実行後、リスタートする
貼り付けた画像_2023_08_29_13_43.png

実行してみる

from google.colab import auth as google_auth
google_auth.authenticate_user()

import vertexai
from vertexai.language_models import ChatModel, InputOutputTextPair

vertexai.init(project="generative-ai-396008", location="us-central1")
chat_model = ChatModel.from_pretrained("chat-bison@001")
parameters = {
    "candidate_count": 1,
    "max_output_tokens": 256,
    "temperature": 0.2,
    "top_p": 0.8,
    "top_k": 40
}
chat = chat_model.start_chat()
response = chat.send_message("""hello""")
print(f"Response from Model: {response.text}")

実行するといくつかアクセス許可を手動で実施する
貼り付けた画像_2023_08_29_13_45.png

アカウントの選択
貼り付けた画像_2023_08_29_13_45.png

アクセス許可を選択
貼り付けた画像_2023_08_29_13_46.png

アクセス許可後、Gmailにセキュリティ通知が送られてくる
貼り付けた画像_2023_08_29_13_48.png

元の画面に戻ると実行されている
貼り付けた画像_2023_08_29_13_51.png

2
2
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
2
2