使用「模型即服務」(MaaS) 運用開放式模型

本文說明如何在 Vertex AI 上透過「模型即服務」(MaaS) 使用開放原始碼模型。MaaS 提供所選合作夥伴和開放原始碼模型的無伺服器存取權,因此不需要佈建或管理基礎架構。

模型園地 是集中式 AI 和機器學習模型庫,提供 Google、Google 合作夥伴和開放模型 (開放權重和開放原始碼),包括 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 服務

使用 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}")

後續步驟