本文說明如何在 Gemini Enterprise Agent Platform,透過模型即服務 (MaaS) 使用開放原始碼模型。MaaS 提供無伺服器存取權,可存取特定合作夥伴和開放原始碼模型,因此不需要佈建或管理基礎架構。
Gemini Enterprise Agent Platform Model Garden 是集中式程式庫,提供 Google、Google 合作夥伴的 AI 和機器學習模型,以及開放模型 (開放權重和開放原始碼),包括 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 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}")