Mengakses model Vertex AI dari alur kerja


AI Generatif di Vertex AI (juga dikenal sebagai genAI atau gen AI) memberi Anda akses ke model AI generatif Google untuk beberapa modalitas (teks, kode, gambar, ucapan). Anda dapat menguji dan menyesuaikan model bahasa besar (LLM) ini, lalu men-deploy-nya untuk digunakan dalam aplikasi yang didukung teknologi AI. Untuk mengetahui informasi selengkapnya, lihat Ringkasan AI Generatif di Vertex AI.

Vertex AI memiliki berbagai model dasar AI generatif yang dapat diakses melalui API, termasuk model yang digunakan dalam contoh berikut:

  • Gemini Pro dirancang untuk menangani tugas bahasa alami, teks multiturn dan chat kode, serta pembuatan kode.
  • Gemini Pro Vision mendukung perintah multimodal. Anda dapat menyertakan teks, gambar, dan video dalam permintaan perintah dan mendapatkan respons teks atau kode.
  • Pathways Language Model 2 (PaLM 2) untuk teks disesuaikan untuk tugas bahasa seperti klasifikasi, ringkasan, dan ekstraksi entity.

Setiap model diekspos melalui endpoint penayang yang khusus untuk project Google Cloud Anda, sehingga tidak perlu men-deploy model dasar kecuali jika Anda perlu menyesuaikannya untuk kasus penggunaan tertentu. Anda dapat mengirim perintah ke endpoint penerbit. Perintah adalah permintaan natural language yang dikirim ke LLM untuk mendapatkan respons kembali.

Tutorial ini menunjukkan alur kerja yang menghasilkan respons dari model Vertex AI dengan mengirimkan perintah teks ke endpoint penayang menggunakan konektor Workflows atau permintaan POST HTTP. Untuk informasi selengkapnya, lihat ringkasan konektor Vertex AI API dan Membuat permintaan HTTP.

Perhatikan bahwa Anda dapat men-deploy dan menjalankan setiap alur kerja secara terpisah.

Tujuan

Dalam tutorial ini, Anda akan melakukan hal berikut:

  1. Aktifkan Vertex AI dan Workflows API, lalu berikan peran Vertex AI User (roles/aiplatform.user) ke akun layanan Anda. Peran ini memungkinkan akses ke sebagian besar kemampuan Vertex AI. Untuk mengetahui informasi selengkapnya tentang cara menyiapkan Vertex AI, lihat Menyiapkan Google Cloud.
  2. Men-deploy dan menjalankan alur kerja yang meminta model Vertex AI (Gemini Pro Vision) untuk mendeskripsikan gambar yang tersedia secara publik melalui Cloud Storage. Untuk mengetahui informasi selengkapnya, lihat Membuat data bersifat publik.
  3. Deploy dan jalankan alur kerja yang melakukan loop melalui daftar negara secara paralel dan meminta model Vertex AI (Gemini Pro) untuk membuat dan menampilkan histori negara. Dengan menggunakan cabang paralel, Anda dapat mengurangi total waktu eksekusi dengan memulai panggilan ke LLM secara bersamaan dan menunggu semuanya selesai sebelum menggabungkan hasilnya. Untuk mengetahui informasi selengkapnya, lihat Menjalankan langkah alur kerja secara paralel.
  4. Deploy alur kerja yang mirip dengan alur kerja sebelumnya; namun, minta model Vertex AI (PaLM 2 untuk teks) untuk membuat dan menampilkan histori negara. Untuk mengetahui informasi selengkapnya tentang cara memilih model, lihat Informasi model.
  5. Men-deploy alur kerja yang dapat meringkas dokumen besar. Karena ada batas pada periode konteks yang menetapkan rentang waktu ke belakang model selama pelatihan (dan untuk perkiraan), alur kerja membagi dokumen menjadi bagian yang lebih kecil, lalu meminta model Vertex AI (Gemini Pro) untuk meringkas setiap bagian secara paralel. Untuk mengetahui informasi selengkapnya, lihat Perintah ringkasan dan Horizon perkiraan, periode konteks, dan periode perkiraan.

Biaya

Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:

Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga. Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

Setelah menyelesaikan tugas yang dijelaskan dalam dokumen ini, Anda dapat menghindari penagihan berkelanjutan dengan menghapus resource yang Anda buat. Untuk mengetahui informasi selengkapnya, lihat Pembersihan.

Sebelum memulai

Sebelum mencoba contoh dalam tutorial ini, pastikan Anda telah menyelesaikan hal berikut.

Konsol

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI and Workflows APIs.

    Enable the APIs

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Vertex AI > Vertex AI User role to the service account.

      To grant the role, find the Select a role list, then select Vertex AI > Vertex AI User.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Vertex AI and Workflows APIs.

    Enable the APIs

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Vertex AI > Vertex AI User role to the service account.

      To grant the role, find the Select a role list, then select Vertex AI > Vertex AI User.

    6. Click Continue.
    7. Click Done to finish creating the service account.

gcloud

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Vertex AI and Workflows APIs:

    gcloud services enable aiplatform.googleapis.com workflows.googleapis.com
  7. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/aiplatform.user IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/aiplatform.user

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  11. Make sure that billing is enabled for your Google Cloud project.

  12. Enable the Vertex AI and Workflows APIs:

    gcloud services enable aiplatform.googleapis.com workflows.googleapis.com
  13. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/aiplatform.user IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/aiplatform.user

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account

Men-deploy alur kerja yang mendeskripsikan gambar (Gemini Pro Vision)

Deploy alur kerja yang menggunakan metode konektor (generateContent) untuk membuat permintaan ke endpoint penayang Gemini Pro Vision. Metode ini memberikan dukungan untuk pembuatan konten dengan input multimodal.

Alur kerja ini memberikan perintah teks dan URI gambar yang tersedia secara publik di bucket Cloud Storage. Anda dapat melihat gambar dan, di konsol Google Cloud, Anda dapat melihat detail objek.

Alur kerja menampilkan deskripsi gambar dari respons yang dihasilkan model.

Untuk informasi selengkapnya tentang parameter isi permintaan HTTP yang digunakan saat meminta LLM, dan elemen isi respons, lihat referensi Gemini API.

Konsol

  1. Di konsol Google Cloud, buka halaman Workflows.

    Buka Workflows

  2. Klik Buat.

  3. Masukkan nama untuk alur kerja baru: describe-image.

  4. Di daftar Region, pilih us-central1 (Iowa).

  5. Untuk Service account, pilih akun layanan yang sebelumnya Anda buat.

  6. Klik Berikutnya.

  7. Di editor alur kerja, masukkan definisi berikut untuk alur kerja Anda:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-1.0-pro-vision"
                - 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. Klik Deploy.

gcloud

  1. Buat file kode sumber untuk alur kerja Anda:

    touch describe-image.yaml
  2. Di editor teks, salin alur kerja berikut ke file kode sumber Anda:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-1.0-pro-vision"
                - 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. Deploy alur kerja dengan memasukkan perintah berikut:

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

Menjalankan alur kerja

Mengeksekusi alur kerja akan menjalankan definisi alur kerja saat ini yang terkait dengan alur kerja.

Konsol

  1. Di konsol Google Cloud, buka halaman Workflows.

    Buka Workflows

  2. Di halaman Alur kerja, pilih alur kerja describe-image untuk membuka halaman detailnya.

  3. Di halaman Workflow details, klik Execute.

  4. Untuk Input, masukkan hal berikut:

    {"image_url":"gs://generativeai-downloads/images/scones.jpg"}
  5. Klik Execute lagi.

  6. Lihat hasil alur kerja di panel Output.

    Outputnya akan mirip dengan berikut ini:

    {
      "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. Buka terminal.

  2. Menjalankan alur kerja:

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

    Hasil eksekusi akan mirip dengan berikut ini:

      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

Men-deploy alur kerja yang menghasilkan histori negara (Gemini Pro)

Deploy alur kerja yang melakukan loop melalui daftar input negara secara paralel dan menggunakan metode konektor (generateContent) untuk membuat permintaan ke endpoint penayang Gemini Pro. Metode ini memberikan dukungan untuk pembuatan konten dengan input multimodal.

Alur kerja menampilkan histori negara yang dihasilkan oleh model, yang menggabungkannya dalam peta.

Untuk informasi selengkapnya tentang parameter isi permintaan HTTP yang digunakan saat meminta LLM, dan elemen isi respons, lihat referensi Gemini API.

Konsol

  1. Di konsol Google Cloud, buka halaman Workflows.

    Buka Workflows

  2. Klik Buat.

  3. Masukkan nama untuk alur kerja baru: gemini-pro-country-histories.

  4. Di daftar Region, pilih us-central1 (Iowa).

  5. Untuk Service account, pilih akun layanan yang sebelumnya Anda buat.

  6. Klik Berikutnya.

  7. Di editor alur kerja, masukkan definisi berikut untuk alur kerja Anda:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-1.0-pro"
                - 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. Klik Deploy.

gcloud

  1. Buat file kode sumber untuk alur kerja Anda:

    touch gemini-pro-country-histories.yaml
  2. Di editor teks, salin alur kerja berikut ke file kode sumber Anda:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "gemini-1.0-pro"
                - 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. Deploy alur kerja dengan memasukkan perintah berikut:

    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

Menjalankan alur kerja

Mengeksekusi alur kerja akan menjalankan definisi alur kerja saat ini yang terkait dengan alur kerja.

Konsol

  1. Di konsol Google Cloud, buka halaman Workflows.

    Buka Workflows

  2. Di halaman Alur kerja, pilih alur kerja gemini-pro-country-histories untuk membuka halaman detailnya.

  3. Di halaman Workflow details, klik Execute.

  4. Untuk Input, masukkan hal berikut:

    {"countries":["Argentina", "Bhutan", "Cyprus", "Denmark", "Ethiopia"]}
  5. Klik Execute lagi.

  6. Lihat hasil alur kerja di panel Output.

    Outputnya akan mirip dengan berikut ini:

    {
      "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. Buka terminal.

  2. Menjalankan alur kerja:

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

    Hasil eksekusi akan mirip dengan berikut ini:

      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

Men-deploy alur kerja yang menghasilkan histori negara (PaLM 2 untuk teks)

Anda mungkin tidak ingin menggunakan Gemini Pro sebagai model. Contoh berikut menggunakan alur kerja yang mirip dengan contoh sebelumnya; tetapi, contoh ini menggunakan metode konektor (predict) untuk membuat permintaan ke endpoint penayang teks PaLM 2. Metode ini melakukan prediksi online.

Untuk informasi selengkapnya tentang parameter isi permintaan HTTP yang digunakan saat meminta LLM, dan elemen isi respons, lihat referensi API PaLM 2 untuk teks.

Konsol

  1. Di konsol Google Cloud, buka halaman Workflows.

    Buka Workflows

  2. Klik Buat.

  3. Masukkan nama untuk alur kerja baru: text-bison-country-histories.

  4. Di daftar Region, pilih us-central1 (Iowa).

  5. Untuk Service account, pilih akun layanan yang sebelumnya Anda buat.

  6. Klik Berikutnya.

  7. Di editor alur kerja, masukkan definisi berikut untuk alur kerja Anda:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "text-bison"
                - histories: {}
        - loop_over_countries:
            parallel:
                shared: [histories]
                for:
                    value: country
                    in: ${args.countries}
                    steps:
                        - ask_llm:
                            call: googleapis.aiplatform.v1.projects.locations.endpoints.predict
                            args:
                                endpoint: ${"projects/" + project + "/locations/" + location + "/publishers/google/models/" + model }
                                region: ${location}
                                body:
                                    instances:
                                        - prompt: '${"Can you  tell me about the history of " + country}'
                                    parameters:
                                        temperature: 0.5
                                        maxOutputTokens: 2048
                                        topP: 0.8
                                        topK: 40
                            result: llm_response
                        - add_to_histories:
                            assign:
                                - history: ${llm_response.predictions[0].content}
                                # Remove leading whitespace from start of text
                                - history: ${text.substring(history, 1, len(history))}
                                - histories[country]: ${history}
        - return_result:
            return: ${histories}

    Perhatikan bahwa bergantung pada model yang digunakan, Anda mungkin perlu menghapus spasi kosong yang tidak diperlukan dari respons.

  8. Klik Deploy.

gcloud

  1. Buat file kode sumber untuk alur kerja Anda:

    touch text-bison-country-histories.yaml
  2. Di editor teks, salin alur kerja berikut ke file kode sumber Anda:

    main:
        params: [args]
        steps:
        - init:
            assign:
                - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                - location: "us-central1"
                - model: "text-bison"
                - histories: {}
        - loop_over_countries:
            parallel:
                shared: [histories]
                for:
                    value: country
                    in: ${args.countries}
                    steps:
                        - ask_llm:
                            call: googleapis.aiplatform.v1.projects.locations.endpoints.predict
                            args:
                                endpoint: ${"projects/" + project + "/locations/" + location + "/publishers/google/models/" + model }
                                region: ${location}
                                body:
                                    instances:
                                        - prompt: '${"Can you  tell me about the history of " + country}'
                                    parameters:
                                        temperature: 0.5
                                        maxOutputTokens: 2048
                                        topP: 0.8
                                        topK: 40
                            result: llm_response
                        - add_to_histories:
                            assign:
                                - history: ${llm_response.predictions[0].content}
                                # Remove leading whitespace from start of text
                                - history: ${text.substring(history, 1, len(history))}
                                - histories[country]: ${history}
        - return_result:
            return: ${histories}

    Perhatikan bahwa bergantung pada model yang digunakan, Anda mungkin perlu menghapus spasi kosong yang tidak diperlukan dari respons.

  3. Deploy alur kerja dengan memasukkan perintah berikut:

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

Men-deploy alur kerja yang meringkas dokumen besar (Gemini Pro)

Deploy alur kerja yang membagi dokumen besar menjadi bagian-bagian yang lebih kecil, sehingga membuat permintaan http.post ke endpoint penayang Gemini Pro secara paralel sehingga model dapat meringkas setiap bagian secara bersamaan. Alur kerja akhirnya menggabungkan semua ringkasan parsial menjadi ringkasan lengkap.

Untuk informasi selengkapnya tentang parameter isi permintaan HTTP yang digunakan saat meminta LLM, dan elemen isi respons, lihat referensi Gemini API.

Definisi alur kerja mengasumsikan bahwa Anda telah membuat bucket Cloud Storage tempat Anda dapat mengupload file teks. Untuk informasi selengkapnya tentang konektor Alur Kerja (googleapis.storage.v1.objects.get) yang digunakan untuk mengambil objek dari bucket Cloud Storage, lihat Referensi konektor.

Setelah men-deploy alur kerja, Anda dapat mengeksekusinya dengan membuat pemicu Eventarc yang sesuai, lalu mengupload file ke bucket. Untuk mengetahui informasi selengkapnya, lihat Merutekan peristiwa Cloud Storage ke Alur Kerja. Perhatikan bahwa API tambahan harus diaktifkan, dan peran tambahan harus diberikan, termasuk memberikan peran Storage Object User (roles/storage.objectUser) ke akun layanan Anda yang mendukung penggunaan objek Cloud Storage. Untuk mengetahui informasi selengkapnya, lihat bagian Bersiap untuk membuat pemicu.

Konsol

  1. Di konsol Google Cloud, buka halaman Workflows.

    Buka Workflows

  2. Klik Buat.

  3. Masukkan nama untuk alur kerja baru: gemini-pro-summaries.

  4. Di daftar Region, pilih us-central1 (Iowa).

  5. Untuk Service account, pilih akun layanan yang sebelumnya Anda buat.

  6. Klik Berikutnya.

  7. Di editor alur kerja, masukkan definisi berikut untuk alur kerja Anda:

    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-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. Klik Deploy.

gcloud

  1. Buat file kode sumber untuk alur kerja Anda:

    touch gemini-pro-summaries.yaml
  2. Di editor teks, salin alur kerja berikut ke file kode sumber Anda:

    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-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. Deploy alur kerja dengan memasukkan perintah berikut:

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

Pembersihan

Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus setiap resource.

Menghapus project

Konsol

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

gcloud

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

Menghapus resource satu per satu

Hapus alur kerja yang Anda buat dalam tutorial ini.

Langkah selanjutnya