Dokumen ini menunjukkan cara mendaftarkan dan menggunakan ekstensi Penerjemah Kode yang disediakan Google dari Konsol Google Cloud dan Vertex AI API. Ekstensi ini memungkinkan Anda membuat dan menjalankan kode Python untuk:
- Menganalisis, membersihkan, mentransformasi, dan membentuk ulang set data
- Memvisualisasikan data dalam bagan dan grafik
- Penghitungan berjalan
Ekstensi Penerjemah Kode menggunakan code_interpreter_tool
untuk
membuat dan menjalankan kode Python dari deskripsi natural language. code_interpreter_tool
ditentukan dalam file code_interpreter.yaml
Spesifikasi OpenAPI.
openapi: "3.0.0" info: version: 1.0.0 title: code_interpreter_tool description: > This tool supports the following operations based on user input: 1. **Generates and Executes Code:** Accepts an user query in natural language, generates corresponding code, and executes it to produce results for the user query. Supported AuthTypes: - `GOOGLE_SERVICE_ACCOUNT_AUTH`: (Vertex AI Extension Service Agent is supported). paths: /generate_and_execute: post: operationId: generate_and_execute description: > Get the results of a natural language query by generating and executing a code snippet. Example queries: "Find the max in [1, 2, 5]" or "Plot average sales by year (from data.csv)". requestBody: required: true content: application/json: schema: type: object required: - query properties: query: type: string description: > Required. The Natural language query to get the results for. The query string can optionally contain data to use for the code generated. For example: "I have a list of numbers: [1, 2, 3, 4]. Find the largest number in the provided data." timeout: type: number description: > Optional. Timeout in miliseconds for the code execution. Default value: 30000. files: type: array description: > Optional. Input files to use when executing the generated code. If specified, the file contents are expected be base64-encoded. For example: [{"name": "data.csv", "contents": "aXRlbTEsaXRlbTI="}] Only one of `file_gcs_uris` and `files` field should be provided. items: $ref: "#/components/schemas/File" file_gcs_uris: type: array description: > Optional. GCS URIs of input files to use when executing the generated code. For example: ["gs://input-bucket/data.csv"] Only one of `file_gcs_uris` and `files` field should be provided. This option is only applicable when `file_input_gcs_bucket` is specified in `Extension.CodeInterpreterRuntimeConfig`. items: type: string responses: '200': description: > The results of generating and executing code based on the natual language query. The result contains the generated code, and the STDOUT, STDERR, and output files from code execution. content: application/json: schema: $ref: "#/components/schemas/GenerationAndExecutionResult" components: schemas: File: description: > File used as inputs and outputs of code execution. The `contents` string should be base64-encoded bytes. For example: [{"name": "data.csv", "contents": "aXRlbTEsaXRlbTI="}] type: object properties: name: type: string contents: type: string format: byte GenerationAndExecutionResult: description: > The results of generating and executing code based on the natual language query. properties: generated_code: type: string description: > The generated code in markdown format. For example: "```python\nprint(\"Hello World\")\n```" execution_result: type: string description: > The code execution result string from STDOUT. execution_error: type: string description: > The code execution error string from STDERR. output_files: type: array description: > The output files generated from code execution. If present, the file contents are required be base64-encoded. For example: [{"name": "data.csv", "contents": "aXRlbTEsaXRlbTI="}] items: $ref: "#/components/schemas/File" output_gcs_uris: type: array description: > The output GCS URIs of files generated from code execution. For example: ["gs://output-bucket/subfolder/output.csv"] This field is only applicable when `file_output_gcs_bucket` is specified in `Extension.CodeInterpreterRuntimeConfig`. items: type: string
Untuk mempelajari tentang ekstensi Google dengan tutorial end-to-end, lihat notebook Jupyter berikut:
- Alur kerja analis bisnis dengan Ekstensi Vertex AI: Gunakan ekstensi Code Interpreter dan ekstensi Vertex AI Search untuk menyelesaikan laporan riset peluang investasi perumahan bagi pemangku kepentingan bisnis.
Colab | GitHub | Vertex AI Workbench - Eksplorasi data dan pelatihan model dengan Penerjemah Kode Ekstensi Vertex AI: Melakukan tugas data science biasa, seperti analisis set data dan pelatihan model.
Colab | GitHub | Vertex AI Workbench - Alur kerja analisis ulasan game dengan Vertex AI Extensions: Gunakan ekstensi Penerjemah Kode untuk menganalisis ulasan game dari Steam. Gunakan ekstensi Vertex AI Search untuk meringkas ulasan game dari situs. Gunakan ekstensi Penerjemah Kode untuk membuat laporan dengan semua aset yang dibuat.
Colab | GitHub | Vertex AI Workbench - Bekerja dengan Panda menggunakan ekstensi Penerjemah Kode Ekstensi Vertex AI: Gunakan kode DataFrames pandas yang dihasilkan oleh ekstensi Penerjemah Kode untuk bekerja dengan set data yang sangat besar.
Colab | GitHub | Vertex AI Workbench - Alur kerja developer web dengan Ekstensi Vertex AI: Menggunakan ekstensi Penerjemah Kode untuk membangun dan men-deploy aplikasi web statis.
Colab | GitHub | Vertex AI Workbench
Sebelum memulai
- Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
-
Di konsol Google Cloud, pada halaman pemilih project, pilih atau buat project Google Cloud.
-
Pastikan penagihan telah diaktifkan untuk project Google Cloud Anda.
-
Enable the Vertex AI API.
-
Di konsol Google Cloud, pada halaman pemilih project, pilih atau buat project Google Cloud.
-
Pastikan penagihan telah diaktifkan untuk project Google Cloud Anda.
-
Enable the Vertex AI API.
Mendaftarkan, membuat kueri, dan menjalankan ekstensi Penerjemah Kode
Bagian berikut menunjukkan cara mendaftarkan ekstensi Penerjemah Kode menggunakan Konsol Google Cloud dan Vertex AI API. Setelah mendaftarkan ekstensi, Anda dapat membuat kuerinya menggunakan Konsol Google Cloud atau menjalankannya menggunakan Vertex AI API.
Konsol
Mendaftarkan ekstensi
Lakukan langkah-langkah berikut untuk mendaftarkan ekstensi Penerjemah Kode menggunakan konsol Google Cloud.
Di konsol Google Cloud, buka halaman Extensions Vertex AI.
Klik Buat Ekstensi.
Dalam dialog Create a new extension, lakukan:
- Nama ekstensi: Masukkan nama untuk ekstensi Anda, seperti "code_interpreter_extension".
- Deskripsi: (Opsional) Masukkan deskripsi ekstensi, seperti "Ekstensi penafsir kode".
- Jenis ekstensi: Pilih
Code interpreter
.
Di bagian OpenAPI Spec file yang sekarang muncul, pastikan kolom berikut telah disetel dengan benar:
- Nama API:
code_interpreter_tool
. - Deskripsi API:
Tool to generate and run valid Python code from a natural language description, or to run custom Python code...
- Sumber:
Cloud Storage
. - Spesifikasi OpenAPI:
vertex-extension-public/code_interpreter.yaml
. - Autentikasi:
Google service account
.
- Nama API:
(Opsional) Di bagian Runtime configurations, masukkan bucket input dan bucket output.
- Bucket input adalah bucket Cloud Storage yang akan digunakan ekstensi untuk membaca file input, termasuk awalan
gs://
, misalnya,gs://sample-bucket-name
. Jika ditentukan, Anda harus menetapkan peranroles/storage.objectViewer
di bucket ini ke akun layanan Vertex Extension Custom Code Service Agent. - Bucket output adalah bucket Cloud Storage yang akan digunakan ekstensi untuk menulis file output, termasuk awalan
gs://
, misalnya,gs://sample-bucket-name
. Jika ditentukan, Anda harus menetapkan peranroles/storage.objectUser
di bucket ini ke akun layanan Vertex Extension Custom Code Service Agent.
- Bucket input adalah bucket Cloud Storage yang akan digunakan ekstensi untuk membaca file input, termasuk awalan
Klik Buat Ekstensi.
(Opsional) Membuat kueri ekstensi
Anda dapat menggunakan konsol Google Cloud untuk bereksperimen dengan ekstensi Penerjemah Kode. Lakukan langkah-langkah berikut untuk memanggil ekstensi dengan prompt natural language.
Di konsol Google Cloud, buka halaman Extensions Vertex AI.
Klik nama ekstensi Penerjemah Kode untuk membuka halaman Detail ekstensi.
Di kotak Masukkan pesan, masukkan kueri, lalu lihat responsnya. Perluas bagian Respons Ekstensi untuk melihat kode yang dihasilkan dan dijalankan ekstensi untuk membuahkan hasil.
Contoh berikut menunjukkan hasil kueri yang menghitung nilai rata-rata daftar angka yang dimasukkan oleh pengguna.
REST
Mendaftarkan ekstensi
Kirimkan permintaan extensions.import
Vertex AI API untuk mendaftarkan ekstensi Penerjemah Kode.
Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:
- PROJECT_ID: ID project Google Cloud Anda.
- REGION: Region Compute Engine.
- DISPLAY_NAME: Ekstensi name yang ditampilkan kepada pengguna, seperti "my_code_interpreter_extension".
- DESCRIPTION: (Opsional) Deskripsi ekstensi yang ditampilkan kepada pengguna, seperti "Ekstensi penafsir kode".
- SERVICE_ACCOUNT: (Opsional) Ekstensi Penerjemah Kode
menggunakan GOOGLE_SERVICE_ACCOUNT_AUTH
seperti yang ditunjukkan dalam contoh isi permintaan. Jika akun layanan tidak ditentukan, ekstensi akan menggunakan akun layanan Agen Layanan Ekstensi Vertex AI default.
Jika Anda menentukan akun layanan lain, berikan izin
iam.serviceAccounts.getAccessToken
ke akun layanan Agen Layanan Ekstensi Vertex AI pada akun layanan yang ditentukan. - INPUT_BUCKET: (Opsional) Bucket Cloud Storage yang akan digunakan ekstensi untuk membaca file input, termasuk awalan
gs://
, misalnya,gs://sample-bucket-name
. Jika ditentukan, Anda harus menetapkan peranroles/storage.objectViewer
di bucket ini ke akun layanan Vertex Extension Custom Code Service Agent. - OUTPUT_BUCKET: (Opsional) Bucket Cloud Storage yang akan digunakan ekstensi untuk menulis file output, termasuk awalan
gs://
, misalnya,gs://sample-bucket-name
. Jika ditentukan, Anda harus menetapkan peranroles/storage.objectUser
di bucket ini ke akun layanan Vertex Extension Custom Code Service Agent.
Metode HTTP dan URL:
POST https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions:import
Isi JSON permintaan:
{ "displayName":"DISPLAY_NAME", "description":"DESCRIPTION", "manifest":{ "name":"code_interpreter_tool", "description":"A Google Code Interpreter tool", "apiSpec":{ "openApiGcsUri":"gs://vertex-extension-public/code_interpreter.yaml" }, "authConfig":{ "authType":"GOOGLE_SERVICE_ACCOUNT_AUTH", "googleServiceAccountConfig":{ "serviceAccount":"SERVICE_ACCOUNT" } } } "runtimeConfig": { "codeInterpreterRuntimeConfig": { "fileInputGcsBucket": "INPUT_BUCKET", "fileOutputGcsBucket": "OUTPUT_BUCKET" } } }
Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:
curl
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions:import"
PowerShell
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions:import" | Select-Object -Expand Content
Menjalankan ekstensi
Anda dapat mengirimkan operasi execute
ke Vertex AI API
untuk membuat dan menjalankan kode Python berdasarkan kueri natural language.
Contoh kueri:
- Kueri sederhana: Menemukan nilai maksimum daftar angka.
- Membuat kueri data inline: Data untuk kueri disediakan dalam isi permintaan.
- Kueri dengan data file: Mencetak data file.
- Membuat kueri dengan data Cloud Storage: Membaca data Cloud Storage.
Kueri sederhana
Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:
- PROJECT_ID: ID project Google Cloud Anda.
- REGION: Region Compute Engine.
- EXTENSION_ID: ID ekstensi penafsir kode Anda yang tercantum di bagian Detail ekstensi di Konsol Google Cloud.
Metode HTTP dan URL:
POST https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute
Isi JSON permintaan:
{ "operation_id":"generate_and_execute", "operation_params":{ "query":"find the max value in the list: [1,2,3,4,-5]" } }
Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:
curl
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute"
PowerShell
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute" | Select-Object -Expand Content
Data inline
Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:
- PROJECT_ID: ID project Google Cloud Anda.
- REGION: Region Compute Engine.
- EXTENSION_ID: ID ekstensi penafsir kode Anda yang tercantum di bagian Detail ekstensi di Konsol Google Cloud.
Metode HTTP dan URL:
POST https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute
Isi JSON permintaan:
{ "operation_id":"generate_and_execute", "operation_params":{ "query":"Calculate the total values of each column(mobile_subscribers, percent_internet_users, total_internet_users, fixed_broadband_subscribers) from the below dataset.\n\n\ncountry_name country_code year mobile_subscribers percent_internet_users total_internet_users fixed_broadband_subscribers\nUnited States US 2023 333.4 90.5 303.1 200.3\nChina CN 2023 1.613 70.2 1131.4 512.2\nIndia IN 2023 1.165 50.7 688.5 557.2\nJapan JP 2023 124.3 88.2 109.5 114.8\nGermany DE 2023 102.1 90.5 92.1 100\nUnited Kingdom UK 2023 67.1 92.7 62.2 65\nFrance FR 2023 66.7 89 63 69.7\nBrazil BR 2023 213.5 68 144.1 69.4\nRussia RU 2023 203.8 74.9 152.7 51.1" } }
Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:
curl
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute"
PowerShell
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute" | Select-Object -Expand Content
Cetakan file
Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:
- PROJECT_ID: ID project Google Cloud Anda.
- REGION: Region Compute Engine.
- EXTENSION_ID: ID ekstensi penafsir kode Anda yang tercantum di bagian Detail ekstensi di Konsol Google Cloud.
- FILE_NAME: Data file CSV dalam isi permintaan ditulis ke file ini di direktori kerja.
- BASE64_ENCODED_FILE_BYTES: Byte file dalam isi permintaan harus berenkode base64.
Metode HTTP dan URL:
POST https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute
Isi JSON permintaan:
{ "operation_id":"generate_and_execute", "operation_params":{ "query":"print the csv file", "files":[ { "name":"FILE_NAME", "contents":"BASE64_ENCODED_FILE_BYTES" } ] } }
Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:
curl
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute"
PowerShell
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute" | Select-Object -Expand Content
Bacaan Cloud Storage
Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:
- PROJECT_ID: ID project Google Cloud Anda.
- REGION: Region Compute Engine.
- EXTENSION_ID: ID ekstensi penafsir kode Anda yang tercantum di bagian Detail ekstensi di Konsol Google Cloud.
- BUCKET_NAME: Bucket Cloud Storage yang berisi file CSV yang akan dicetak. Anda harus menetapkan bucket input ini saat mendaftarkan ekstensi penafsir kode.
- FILE_NAME: Data file CSV di BUCKET_NAME untuk dicetak.
Metode HTTP dan URL:
POST https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute
Isi JSON permintaan:
{ "operation_id":"generate_and_execute", "operation_params":{ "query":"print the csv file", "file_gcs_uris": ["gs://BUCKET_NAME/FILE_NAME"] } }
Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:
curl
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute"
PowerShell
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute" | Select-Object -Expand Content