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

本文档介绍如何在 Gemini Enterprise Agent Platform 上通过模型即服务 (MaaS) 使用开放模型。MaaS 提供对精选合作伙伴模型和开源模型的无服务器访问,因此您无需预配或管理基础架构。

Model Garden 是一个集中式库,其中包含 Google、Google 合作伙伴提供的 AI 和 ML 模型,以及 开放模型(开放权重和开源),包括 MaaS 模型。 Model Garden 提供了多种在 Gemini Enterprise Agent Platform 上部署 可用 模型 的方法,包括 Hugging Face 中的模型

如需详细了解 MaaS,请参阅合作伙伴模型 文档

准备工作

如需使用 MaaS 模型,您必须在您的 Google Cloud 项目中启用 Agent Platform 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}")

后续步骤