使用模型即服务 (MaaS) 提供的开放模型

本文档介绍了如何在 Vertex AI 上通过模型即服务 (MaaS) 使用开放模型。MaaS 提供对所选合作伙伴模型和开源模型的无服务器访问,因此无需预配或管理基础设施。

Model Garden 是一个集中式 AI 和 ML 模型库,其中包含 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}")

后续步骤