Send a text prompt to Gen AI and receive a image response

Check my progress

/ 100

This lab may incorporate AI tools to support your learning.

bb-ide-genai-002

Google Cloud 自學實驗室

總覽

  • 實驗室活動會計時且無法暫停,點選「Start Lab」後就會開始計時。
  • 內建的 Cloud 終端機已預先設定了 gcloud SDK。
  • 使用終端機執行指令,然後點選「Check my progress」,確認工作已完成。

目標

您可透過 Agent Platform 生成式 AI (又稱「生成式 AI」) 測試、調整及部署 Google 的大型生成式 AI 模型,以便在 AI 應用程式中使用。本實驗室的學習內容包括:

  • 連結至 Agent Platform (Google Cloud AI 平台):瞭解如何使用 Agent Platform SDK 建立與 Google AI 服務的連線。
  • 載入預先訓練的圖像生成模型:瞭解如何使用強大的預先訓練 AI 模型,無須從頭建立。
  • 將文字傳送至 AI 模型:瞭解如何提供輸入內容給 AI 處理。
  • 從 AI 擷取圖像式答案:瞭解如何處理及解讀 AI 模型生成的圖像回覆。
  • 瞭解建構 AI 應用程式的基本知識:深入瞭解將 AI 整合至軟體專案的核心概念。

運用生成式 AI

啟動實驗室後,會出現分割窗格檢視畫面,左側是程式碼編輯器,右側是實驗室操作說明。請按照下列步驟,使用 Agent Platform Python SDK 與生成式 AI API 互動。

  1. 依序點選「File」>「New File」,在程式碼編輯器中開啟新檔案。
  2. 複製以下程式碼片段並貼到檔案。
import argparse import vertexai from vertexai.preview.vision_models import ImageGenerationModel def generate_image( project_id: str, location: str, output_file: str, prompt: str ) -> vertexai.preview.vision_models.ImageGenerationResponse: """使用文字提示詞生成圖片。 Args: project_id: Google Cloud 專案 ID,用來初始化 Agent Platform。 location: Google Cloud 區域,用來初始化 Agent Platform。 output_file: 本機至輸出圖片檔的路徑。 prompt: 描述您期望景象的文字提示詞。""" vertexai.init(project=project_id, location=location) model = ImageGenerationModel.from_pretrained("imagen-3.0-generate-002") images = model.generate_images( prompt=prompt, # Optional parameters number_of_images=1, seed=1, add_watermark=False, ) images[0].save(location=output_file) return images generate_image( project_id='{{{ project_0.project_id | "project-id" }}}', location='{{{ project_0.default_region | "REGION" }}}', output_file='image.jpeg', prompt='Create an image of a cricket ground in the heart of Los Angeles', )
  1. 依序點選「File」>「Save」,在「Name」欄位輸入 GenerateImage.py,然後點選「Save」
  2. 點選程式碼編輯器的三角形圖示,或在程式碼編輯器窗格的終端機叫用下列指令,即可執行 Python 檔案。這會生成名為 image.jpeg 的圖片檔。
/usr/bin/python3 /GenerateImage.py
  1. 點選「EXPLORER」>「image.jpeg」,即可查看生成的圖片。

解釋程式碼

  • 程式碼片段會在 Agent Platform 上載入名為 ImageGenerationModel 的預先訓練 AI 模型 (imagen-3.0-generate-002)。
  • 程式碼會呼叫已載入 Gemini 模型的 generate_image 方法。
  • 這個方法的輸入內容是文字提示詞。
  • 程式碼會運用 Gemini 理解文字提示詞的能力,並使用提示詞來建立 AI 圖像。
注意:圖片預設會加上 SynthID 浮水印,但您可以指定選用參數 add_watermark=False 來停用這項功能,無法同時使用種子值和浮水印。進一步瞭解 SynthID 浮水印

快來親自試試!用不同提示詞,探索 Gemini 的功能。

點選「Check my progress」確認目標已達成。

向生成式 AI 傳送文字提示詞要求,並接收回覆

恭喜!

您已完成這個實驗室!辛苦了!

Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Using an Incognito or private browser window is the best way to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.

Complete this quick step to start your lab.