Model as a Service(MaaS)を使用してオープンモデルを使用する

このドキュメントでは、Vertex AI の Model as a Service(MaaS)を通じてオープンモデルを使用する方法について説明します。MaaS は、選択されたパートナー モデルやオープンソース モデルへのサーバーレス アクセスを提供します。そのため、ユーザーがインフラストラクチャのプロビジョニングや管理を行う必要はありません。

Model Garden は、Google、Google パートナー、およびオープンモデル(オープン ウェイト モデルとオープンソース モデル)の AI モデルと ML モデル(MaaS モデルを含む)を一元的に提供するライブラリです。Model Garden には、Vertex AI で使用可能なモデルHugging Face のモデルを含む)をデプロイする方法が複数用意されています。

MaaS の詳細については、パートナー モデルのドキュメントをご覧ください。

始める前に

MaaS モデルを使用するには、Google Cloud プロジェクトで Vertex AI API を有効にする必要があります。

gcloud services enable aiplatform.googleapis.com

モデルの API を有効にする

MaaS モデルを使用する前に、そのモデルの API を有効にする必要があります。そのためには、Model Garden のモデルページに移動します。MaaS を通じて使用可能なモデルの一部は、セルフデプロイすることもできます。これらの各モデルは、Model Garden の別々のモデルカードで提供されています。MaaS モデルのモデルカードは、カード名に API Service が含まれています。

Google Gen AI SDK for Python を使用してモデルを呼び出す

次に、Google Gen AI SDK for Python を使用して Llama 3.3 モデルを呼び出す例を示します。

from google import genai
from google.genai import types

PROJECT_ID="PROJECT_ID"
LOCATION="LOCATION"
MODEL="meta/llama-3.3-70b-instruct-maas"  # The model ID from Model Garden with "API Service"

# Define the prompt to send to the model.
prompt = "What is the distance between earth and moon?"

# Initialize the Google Gen AI SDK client.
client = genai.Client(
    vertexai=True,
    project=PROJECT_ID,
    location=LOCATION,
)

# Prepare the content for the chat.
contents: types.ContentListUnion = [
    types.Content(
        role="user",
        parts=[
            types.Part.from_text(text=prompt)
        ]
    )
]

# Configure generation parameters.
generate_content_config = types.GenerateContentConfig(
    temperature = 0,
    top_p = 0,
    max_output_tokens = 4096,
)

try:
    # Create a chat instance with the specified model.
    chat = client.chats.create(model=MODEL)
    # Send the message and print the response.
    response = chat.send_message(contents)
    print(response.text)
except Exception as e:
    print(f"{MODEL} call failed due to {e}")

次のステップ