目的
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の一種であり、ウェブブラウザ上で直接コードを書き、実行することができる。
事前準備
ノートブックの作成
Jupyter LABの起動
クライアントライブラリの導入
下記を入力し、実行する
!pip install google-cloud-aiplatform --upgrade --user
Workbench(JupyterLAB)からのAPIコールの実行
GUIで下記の様に実施することをAPI経由で実施する
単純に「hello」とチャットする
projectとlocationは実行環境の値にする
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}")
実行してみる
Google ColabからのAPIコールの実行
ライブラリの導入
!pip install "shapely<2.0.0"
!pip install google-cloud-aiplatform --upgrade
実行してみる
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}")