透過 Vertex AI 從工作流程存取 Gemini 模型

您可透過 Vertex AI 生成式 AI (又稱「生成式 AI」) 存取 Google 的生成式 AI 模型,以便處理多種模態 (文字、程式碼、圖片、語音)。您可以測試及調整這些大型語言模型 (LLM),然後部署至 AI 輔助應用程式。詳情請參閱 Vertex AI 生成式 AI 總覽

Vertex AI 提供各種生成式 AI 基礎模型,可透過 API 存取,包括本指南中使用的模型。如要進一步瞭解如何選擇模型,請參閱「Google 模型」。

每個模型都是由您Google Cloud 專案特定的發布端點公開,除非您需要依據特定用途調整模型,否則不必部署基礎模型。您可以將提示傳送至發布商端點。提示是傳送至大型語言模型的自然語言要求,用來引導模型提供回覆。

本教學課程將示範工作流程,透過 Workflows 連接器或 HTTP POST 要求,將文字提示傳送至發布者端點,藉此從 Vertex AI 模型生成回覆。詳情請參閱「Vertex AI API 連接器總覽」和「提出 HTTP 要求」。

請注意,您可以獨立部署及執行每個工作流程。

部署描述圖片的工作流程

部署使用連接器方法 (generateContent) 的工作流程,向模型發布者端點提出要求。這項方法支援使用多模態輸入內容生成內容。

工作流程會提供文字提示,以及 Cloud Storage bucket 中可公開存取的圖片 URI。您可以查看圖片,並在 Google Cloud 控制台中查看物件詳細資料

工作流程會從模型生成的結果中,傳回圖片說明。

如要進一步瞭解提示 LLM 時使用的 HTTP 要求主體參數,以及回應主體元素,請參閱 Gemini API 參考資料

控制台

  1. 前往 Google Cloud 控制台的「Workflows」頁面。

    前往「Workflows」頁面

  2. 按一下 「建立」

  3. 輸入新工作流程的名稱:describe-image

  4. 在「Region」(區域) 清單中,選取「us-central1 (Iowa)」(us-central1 (愛荷華州))

  5. 在「Service account」(服務帳戶) 部分,選取您先前建立的服務帳戶。

  6. 點選「下一步」

  7. 在工作流程編輯器中,輸入下列工作流程定義:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-2.5-flash"
                - text_combined: ""
        - ask_llm:
            call: googleapis.aiplatform.v1.projects.locations.endpoints.generateContent
            args:
                model: ${"projects/" + project + "/locations/" + location + "/publishers/google/models/" + model}
                region: ${location}
                body:
                    contents:
                        role: user
                        parts:
                        - fileData:
                            mimeType: image/jpeg
                            fileUri: ${args.image_url}
                        - text: Describe this picture in detail
                    generation_config:
                        temperature: 0.4
                        max_output_tokens: 2048
                        top_p: 1
                        top_k: 32
            result: llm_response
        - return_result:
            return:
                image_url: ${args.image_url}
                image_description: ${llm_response.candidates[0].content.parts[0].text}
  8. 按一下 [Deploy] (部署)

gcloud

  1. 為工作流程建立原始碼檔案:

    touch describe-image.yaml
  2. 在文字編輯器中,將下列工作流程複製到原始碼檔案:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-2.5-flash"
                - text_combined: ""
        - ask_llm:
            call: googleapis.aiplatform.v1.projects.locations.endpoints.generateContent
            args:
                model: ${"projects/" + project + "/locations/" + location + "/publishers/google/models/" + model}
                region: ${location}
                body:
                    contents:
                        role: user
                        parts:
                        - fileData:
                            mimeType: image/jpeg
                            fileUri: ${args.image_url}
                        - text: Describe this picture in detail
                    generation_config:
                        temperature: 0.4
                        max_output_tokens: 2048
                        top_p: 1
                        top_k: 32
            result: llm_response
        - return_result:
            return:
                image_url: ${args.image_url}
                image_description: ${llm_response.candidates[0].content.parts[0].text}
  3. 輸入下列指令來部署工作流程:

    gcloud workflows deploy describe-image \
        --source=describe-image.yaml \
        --location=us-central1 \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

執行工作流程

執行工作流程時,系統會執行與該工作流程相關聯的目前工作流程定義。

控制台

  1. 前往 Google Cloud 控制台的「Workflows」頁面。

    前往「Workflows」頁面

  2. 在「Workflows」頁面中,選取「describe-image」工作流程,前往詳細資料頁面。

  3. 在「工作流程詳細資料」頁面中,按一下 「執行」

  4. 在「Input」(輸入) 中輸入下列內容:

    {"image_url":"gs://generativeai-downloads/images/scones.jpg"}
  5. 再次按一下「執行」

  6. 在「輸出」窗格中查看工作流程結果。

    畫面會顯示如下的輸出內容:

    {
      "image_description": "There are three pink peony flowers on the right side of the picture[]...]There is a white napkin on the table.",
      "image_url": "gs://generativeai-downloads/images/scones.jpg"
    }

gcloud

  1. 開啟終端機。

  2. 執行工作流程:

    gcloud workflows run describe-image \
        --data='{"image_url":"gs://generativeai-downloads/images/scones.jpg"}'

    執行結果應與下列內容類似:

      Waiting for execution [258b530e-a093-46d7-a4ff-cbf5392273c0] to complete...done.
      argument: '{"image_url":"gs://generativeai-downloads/images/scones.jpg"}'
      createTime: '2024-02-09T13:59:32.166409938Z'
      duration: 4.174708484s
      endTime: '2024-02-09T13:59:36.341118422Z'
      name: projects/1051295516635/locations/us-central1/workflows/describe-image/executions/258b530e-a093-46d7-a4ff-cbf5392273c0
      result: "{\"image_description\":\"The picture shows a rustic table with a white surface,\
        \ on which there are several scones with blueberries, as well as two cups of coffee\
        [...]
        \ on the table. The background of the table is a dark blue color.\",\"image_url\"\
        :\"gs://generativeai-downloads/images/scones.jpg\"}"
      startTime: '2024-02-09T13:59:32.166409938Z'
      state: SUCCEEDED

部署可產生國家/地區記錄的工作流程

部署工作流程,以平行方式逐一處理國家/地區輸入清單,並使用連接器方法 (generateContent) 向模型發布商端點提出要求。這項方法支援使用多模態輸入內容生成內容。

工作流程會傳回模型產生的國家/地區記錄,並將這些記錄合併到 map 中。

如要進一步瞭解提示 LLM 時使用的 HTTP 要求主體參數,以及回應主體元素,請參閱 Gemini API 參考資料

控制台

  1. 前往 Google Cloud 控制台的「Workflows」頁面。

    前往「Workflows」頁面

  2. 按一下 「建立」

  3. 輸入新工作流程的名稱:gemini-pro-country-histories

  4. 在「Region」(區域) 清單中,選取「us-central1 (Iowa)」(us-central1 (愛荷華州))

  5. 在「Service account」(服務帳戶) 部分,選取您先前建立的服務帳戶。

  6. 點選「下一步」

  7. 在工作流程編輯器中,輸入下列工作流程定義:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-2.5-flash"
                - histories: {}
        - loop_over_countries:
            parallel:
                shared: [histories]
                for:
                    value: country
                    in: ${args.countries}
                    steps:
                        - ask_llm:
                            call: googleapis.aiplatform.v1.projects.locations.endpoints.generateContent
                            args:
                                model: ${"projects/" + project + "/locations/" + location + "/publishers/google/models/" + model}
                                region: ${location}
                                body:
                                    contents:
                                        role: "USER"
                                        parts:
                                            text: ${"Can you tell me about the history of " + country}
                                    generation_config:
                                        temperature: 0.5
                                        max_output_tokens: 2048
                                        top_p: 0.8
                                        top_k: 40
                            result: llm_response
                        - add_to_histories:
                            assign:
                                - histories[country]: ${llm_response.candidates[0].content.parts[0].text}
        - return_result:
            return: ${histories}
  8. 按一下 [Deploy] (部署)

gcloud

  1. 為工作流程建立原始碼檔案:

    touch gemini-pro-country-histories.yaml
  2. 在文字編輯器中,將下列工作流程複製到原始碼檔案:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-2.5-flash"
                - histories: {}
        - loop_over_countries:
            parallel:
                shared: [histories]
                for:
                    value: country
                    in: ${args.countries}
                    steps:
                        - ask_llm:
                            call: googleapis.aiplatform.v1.projects.locations.endpoints.generateContent
                            args:
                                model: ${"projects/" + project + "/locations/" + location + "/publishers/google/models/" + model}
                                region: ${location}
                                body:
                                    contents:
                                        role: "USER"
                                        parts:
                                            text: ${"Can you tell me about the history of " + country}
                                    generation_config:
                                        temperature: 0.5
                                        max_output_tokens: 2048
                                        top_p: 0.8
                                        top_k: 40
                            result: llm_response
                        - add_to_histories:
                            assign:
                                - histories[country]: ${llm_response.candidates[0].content.parts[0].text}
        - return_result:
            return: ${histories}
  3. 輸入下列指令來部署工作流程:

    gcloud workflows deploy gemini-pro-country-histories \
        --source=gemini-pro-country-histories.yaml \
        --location=us-central1 \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

執行工作流程

執行工作流程時,系統會執行與該工作流程相關聯的目前工作流程定義。

控制台

  1. 前往 Google Cloud 控制台的「Workflows」頁面。

    前往「Workflows」頁面

  2. 在「Workflows」頁面中,選取「gemini-pro-country-histories」工作流程,前往詳細資料頁面。

  3. 在「工作流程詳細資料」頁面中,按一下 「執行」

  4. 在「Input」(輸入) 中輸入下列內容:

    {"countries":["Argentina", "Bhutan", "Cyprus", "Denmark", "Ethiopia"]}
  5. 再次按一下「執行」

  6. 在「輸出」窗格中查看工作流程結果。

    畫面會顯示如下的輸出內容:

    {
      "Argentina": "The history of Argentina is a complex and fascinating one, marked by periods of prosperity and decline, political [...]
      "Bhutan": "The history of Bhutan is a rich and fascinating one, dating back to the 7th century AD. Here is a brief overview: [...]
      "Cyprus": "The history of Cyprus is a long and complex one, spanning over 10,000 years. The island has been ruled by a succession [...]
      "Denmark": "1. **Prehistory and Early History (c. 12,000 BC - 800 AD)**\\n   - The earliest evidence of human habitation in Denmark [...]
      "Ethiopia": "The history of Ethiopia is a long and complex one, stretching back to the earliest human civilizations. The country is [...]
    }

gcloud

  1. 開啟終端機。

  2. 執行工作流程:

    gcloud workflows run gemini-pro-country-histories \
        --data='{"countries":["Argentina", "Bhutan", "Cyprus", "Denmark", "Ethiopia"]}' \
        --location=us-central1

    執行結果應與下列內容類似:

      Waiting for execution [7ae1ccf1-29b7-4c2c-99ec-7a12ae289391] to complete...done.
      argument: '{"countries":["Argentina","Bhutan","Cyprus","Denmark","Ethiopia"]}'
      createTime: '2024-02-09T16:25:16.742349156Z'
      duration: 12.075968673s
      endTime: '2024-02-09T16:25:28.818317829Z'
      name: projects/1051295516635/locations/us-central1/workflows/gemini-pro-country-histories/executions/7ae1ccf1-29b7-4c2c-99ec-7a12ae289391
      result: "{\"Argentina\":\"The history of Argentina can be traced back to the arrival\
        [...]
        n* 2015: Argentina elects Mauricio Macri as president.\",\"Bhutan\":\"The history\
        [...]
        \ natural beauty, ancient monasteries, and friendly people.\",\"Cyprus\":\"The history\
        [...]
        ,\"Denmark\":\"The history of Denmark can be traced back to the Stone Age, with\
        [...]
        \ a high standard of living.\",\"Ethiopia\":\"The history of Ethiopia is long and\
        [...]
      startTime: '2024-02-09T16:25:16.742349156Z'
      state: SUCCEEDED

部署可摘要大型文件的工作流程

部署工作流程,將大型文件分割成較小的部分,並http.post平行向模型發布者端點提出要求,讓模型同時摘要各部分內容。工作流程最後會將所有部分摘要合併為完整摘要。

如要進一步瞭解提示 LLM 時使用的 HTTP 要求主體參數,以及回應主體元素,請參閱 Gemini API 參考資料

工作流程定義會假設您已建立 Cloud Storage 值區,並可將文字檔案上傳至該值區。如要進一步瞭解用於從 Cloud Storage 值區擷取物件的 Workflows 連接器 (googleapis.storage.v1.objects.get),請參閱連接器參考資料

部署工作流程後,您可以建立適當的 Eventarc 觸發條件,然後將檔案上傳至 bucket,即可執行工作流程。詳情請參閱「將 Cloud Storage 事件轉送至 Workflows」。請注意,您必須啟用其他 API,並授予其他角色,包括將 Storage 物件使用者 (roles/storage.objectUser) 角色授予服務帳戶,以支援使用 Cloud Storage 物件。詳情請參閱「準備建立觸發條件」一節。

控制台

  1. 前往 Google Cloud 控制台的「Workflows」頁面。

    前往「Workflows」頁面

  2. 按一下 「建立」

  3. 輸入新工作流程的名稱:gemini-pro-summaries

  4. 在「Region」(區域) 清單中,選取「us-central1 (Iowa)」(us-central1 (愛荷華州))

  5. 在「Service account」(服務帳戶) 部分,選取您先前建立的服務帳戶。

  6. 點選「下一步」

  7. 在工作流程編輯器中,輸入下列工作流程定義:

    main:
        params: [input]
        steps:
        - assign_file_vars:
            assign:
                - file_size: ${int(input.data.size)}
                - chunk_size: 64000
                - n_chunks: ${int(file_size / chunk_size)}
                - summaries: []
                - all_summaries_concatenated: ""
        - loop_over_chunks:
            parallel:
                shared: [summaries]
                for:
                    value: chunk_idx
                    range: ${[0, n_chunks]}
                    steps:
                        - assign_bounds:
                            assign:
                                - lower_bound: ${chunk_idx * chunk_size}
                                - upper_bound: ${(chunk_idx + 1) * chunk_size}
                                - summaries: ${list.concat(summaries, "")}
                        - dump_file_content:
                            call: http.get
                            args:
                                url: ${"https://storage.googleapis.com/storage/v1/b/" + input.data.bucket + "/o/" + input.data.name + "?alt=media"}
                                auth:
                                    type: OAuth2
                                headers:
                                    Range: ${"bytes=" + lower_bound + "-" + upper_bound}
                            result: file_content
                        - assign_chunk:
                            assign:
                                - chunk: ${file_content.body}
                        - generate_chunk_summary:
                            call: ask_gemini_for_summary
                            args:
                                textToSummarize: ${chunk}
                            result: summary
                        - assign_summary:
                            assign:
                                - summaries[chunk_idx]: ${summary}
        - concat_summaries:
            for:
                value: summary
                in: ${summaries}
                steps:
                    - append_summaries:
                        assign:
                            - all_summaries_concatenated: ${all_summaries_concatenated + "\n" + summary}
        - reduce_summary:
            call: ask_gemini_for_summary
            args:
                textToSummarize: ${all_summaries_concatenated}
            result: final_summary
        - return_result:
            return:
                - summaries: ${summaries}
                - final_summary: ${final_summary}
    
    ask_gemini_for_summary:
        params: [textToSummarize]
        steps:
            - init:
                assign:
                    - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                    - location: "us-central1"
                    - model: "gemini-2.5-pro"
                    - summary: ""
            - call_gemini:
                call: http.post
                args:
                    url: ${"https://" + location + "-aiplatform.googleapis.com" + "/v1/projects/" + project + "/locations/" + location + "/publishers/google/models/" + model + ":generateContent"}
                    auth:
                        type: OAuth2
                    body:
                        contents:
                            role: user
                            parts:
                                - text: '${"Make a summary of the following text:\n\n" + textToSummarize}'
                        generation_config:
                            temperature: 0.2
                            maxOutputTokens: 2000
                            topK: 10
                            topP: 0.9
                result: gemini_response
            # Sometimes, there's no text, for example, due to safety settings
            - check_text_exists:
                switch:
                - condition: ${not("parts" in gemini_response.body.candidates[0].content)}
                  next: return_summary
            - extract_text:
                assign:
                    - summary: ${gemini_response.body.candidates[0].content.parts[0].text}
            - return_summary:
                return: ${summary}
  8. 按一下 [Deploy] (部署)

gcloud

  1. 為工作流程建立原始碼檔案:

    touch gemini-pro-summaries.yaml
  2. 在文字編輯器中,將下列工作流程複製到原始碼檔案:

    main:
        params: [input]
        steps:
        - assign_file_vars:
            assign:
                - file_size: ${int(input.data.size)}
                - chunk_size: 64000
                - n_chunks: ${int(file_size / chunk_size)}
                - summaries: []
                - all_summaries_concatenated: ""
        - loop_over_chunks:
            parallel:
                shared: [summaries]
                for:
                    value: chunk_idx
                    range: ${[0, n_chunks]}
                    steps:
                        - assign_bounds:
                            assign:
                                - lower_bound: ${chunk_idx * chunk_size}
                                - upper_bound: ${(chunk_idx + 1) * chunk_size}
                                - summaries: ${list.concat(summaries, "")}
                        - dump_file_content:
                            call: http.get
                            args:
                                url: ${"https://storage.googleapis.com/storage/v1/b/" + input.data.bucket + "/o/" + input.data.name + "?alt=media"}
                                auth:
                                    type: OAuth2
                                headers:
                                    Range: ${"bytes=" + lower_bound + "-" + upper_bound}
                            result: file_content
                        - assign_chunk:
                            assign:
                                - chunk: ${file_content.body}
                        - generate_chunk_summary:
                            call: ask_gemini_for_summary
                            args:
                                textToSummarize: ${chunk}
                            result: summary
                        - assign_summary:
                            assign:
                                - summaries[chunk_idx]: ${summary}
        - concat_summaries:
            for:
                value: summary
                in: ${summaries}
                steps:
                    - append_summaries:
                        assign:
                            - all_summaries_concatenated: ${all_summaries_concatenated + "\n" + summary}
        - reduce_summary:
            call: ask_gemini_for_summary
            args:
                textToSummarize: ${all_summaries_concatenated}
            result: final_summary
        - return_result:
            return:
                - summaries: ${summaries}
                - final_summary: ${final_summary}
    
    ask_gemini_for_summary:
        params: [textToSummarize]
        steps:
            - init:
                assign:
                    - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                    - location: "us-central1"
                    - model: "gemini-2.5-pro"
                    - summary: ""
            - call_gemini:
                call: http.post
                args:
                    url: ${"https://" + location + "-aiplatform.googleapis.com" + "/v1/projects/" + project + "/locations/" + location + "/publishers/google/models/" + model + ":generateContent"}
                    auth:
                        type: OAuth2
                    body:
                        contents:
                            role: user
                            parts:
                                - text: '${"Make a summary of the following text:\n\n" + textToSummarize}'
                        generation_config:
                            temperature: 0.2
                            maxOutputTokens: 2000
                            topK: 10
                            topP: 0.9
                result: gemini_response
            # Sometimes, there's no text, for example, due to safety settings
            - check_text_exists:
                switch:
                - condition: ${not("parts" in gemini_response.body.candidates[0].content)}
                  next: return_summary
            - extract_text:
                assign:
                    - summary: ${gemini_response.body.candidates[0].content.parts[0].text}
            - return_summary:
                return: ${summary}
  3. 輸入下列指令來部署工作流程:

    gcloud workflows deploy gemini-pro-summaries \
        --source=gemini-pro-summaries.yaml \
        --location=us-central1 \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com