Melakukan analisis semantik dengan fungsi AI terkelola
Tutorial ini menunjukkan cara menggunakan fungsi AI terkelola BigQuery ML untuk melakukan analisis semantik pada masukan pelanggan.
Tujuan
Dalam tutorial ini, Anda telah:
- Membuat set data dan memuat data sentimen ke dalam tabel
- Membuat Koneksi Resource Cloud
- Gunakan fungsi AI berikut untuk melakukan analisis semantik:
AI.IF: untuk memfilter data Anda dengan kondisi bahasa alamiAI.SCORE: untuk memberi rating input berdasarkan sentimenAI.CLASSIFY: untuk mengklasifikasikan input ke dalam kategori yang ditentukan pengguna
Biaya
Tutorial ini menggunakan komponen Google Cloudyang dapat ditagih, termasuk:
- BigQuery
- BigQuery ML
Untuk informasi selengkapnya tentang biaya BigQuery, lihat halaman harga BigQuery.
Untuk informasi selengkapnya tentang biaya BigQuery ML, lihat harga BigQuery ML.
Sebelum memulai
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Jika Anda menggunakan project yang sudah ada untuk panduan ini, pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini. Jika berhasil membuat project baru, berarti Anda sudah memiliki izin yang diperlukan.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Jika Anda menggunakan project yang sudah ada untuk panduan ini, pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini. Jika berhasil membuat project baru, berarti Anda sudah memiliki izin yang diperlukan.
-
Enable the BigQuery API and BigQuery Connection API APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.Untuk project baru, BigQuery API diaktifkan secara otomatis.
- Opsional: Aktifkan penagihan untuk project. Jika Anda tidak ingin mengaktifkan penagihan atau memberikan kartu kredit, langkah-langkah dalam dokumen ini tetap berfungsi. BigQuery menyediakan sandbox untuk melakukan langkah-langkah tersebut. Untuk mengetahui informasi selengkapnya, lihat Mengaktifkan sandbox BigQuery.
-
Menjalankan tugas kueri dan tugas pemuatan:
BigQuery Job User (
roles/bigquery.jobUser) -
Membuat koneksi:
BigQuery Connection Admin (
roles/bigquery.connectionAdmin) -
Buat set data, buat tabel, muat data ke dalam tabel, dan kueri tabel:
Editor Data BigQuery (
roles/bigquery.dataEditor) Buka halaman BigQuery.
Di panel kiri, klik Explorer:

Jika Anda tidak melihat panel kiri, klik Luaskan panel kiri untuk membuka panel.
Di panel Explorer, luaskan nama project Anda, lalu klik Connections.
Di halaman Koneksi, klik Buat koneksi.
Untuk Connection type, pilih Vertex AI remote models, remote functions, BigLake and Spanner (Cloud Resource).
Di kolom Connection ID, masukkan nama untuk koneksi Anda.
Untuk Location type, pilih lokasi untuk koneksi Anda. Koneksi harus ditempatkan bersama resource Anda yang lain seperti set data.
Klik Create connection.
Klik Go to connection.
Di panel Connection info, salin ID akun layanan untuk digunakan pada langkah berikutnya.
Di lingkungan command line, buat koneksi:
bq mk --connection --location=REGION --project_id=PROJECT_ID \ --connection_type=CLOUD_RESOURCE CONNECTION_ID
Parameter
--project_idakan mengganti project default.Ganti kode berikut:
REGION: region koneksi AndaPROJECT_ID: Project ID Google Cloud AndaCONNECTION_ID: ID untuk koneksi Anda
Saat Anda membuat resource koneksi, BigQuery akan membuat akun layanan sistem unik dan mengaitkannya dengan koneksi.
Pemecahan masalah: Jika Anda mendapatkan error koneksi berikut, update Google Cloud SDK:
Flags parsing error: flag --connection_type=CLOUD_RESOURCE: value should be one of...
Ambil dan salin ID akun layanan untuk digunakan pada langkah berikutnya:
bq show --connection PROJECT_ID.REGION.CONNECTION_ID
Outputnya mirip dengan hal berikut ini:
name properties 1234.REGION.CONNECTION_ID {"serviceAccountId": "connection-1234-9u56h9@gcp-sa-bigquery-condel.iam.gserviceaccount.com"}- Luncurkan Cloud Shell.
-
Tetapkan project Google Cloud default tempat Anda ingin menerapkan konfigurasi Terraform.
Anda hanya perlu menjalankan perintah ini sekali per project, dan dapat dijalankan di direktori mana pun.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Variabel lingkungan akan diganti jika Anda menetapkan nilai eksplisit dalam file konfigurasi Terraform.
-
Di Cloud Shell, buat direktori dan file baru di dalam direktori tersebut. Nama file harus memiliki
ekstensi
.tf—misalnyamain.tf. Dalam tutorial ini, file ini disebut sebagaimain.tf.mkdir DIRECTORY && cd DIRECTORY && touch main.tf
-
Jika mengikuti tutorial, Anda dapat menyalin kode contoh di setiap bagian atau langkah.
Salin kode contoh ke dalam
main.tfyang baru dibuat.Atau, salin kode dari GitHub. Tindakan ini direkomendasikan jika cuplikan Terraform adalah bagian dari solusi menyeluruh.
- Tinjau dan ubah contoh parameter untuk diterapkan pada lingkungan Anda.
- Simpan perubahan Anda.
-
Lakukan inisialisasi Terraform. Anda hanya perlu melakukan ini sekali per direktori.
terraform init
Secara opsional, untuk menggunakan versi penyedia Google terbaru, sertakan opsi
-upgrade:terraform init -upgrade
-
Tinjau konfigurasi dan pastikan resource yang akan dibuat atau
diupdate oleh Terraform sesuai yang Anda inginkan:
terraform plan
Koreksi konfigurasi jika diperlukan.
-
Terapkan konfigurasi Terraform dengan menjalankan perintah berikut dan memasukkan
yespada prompt:terraform apply
Tunggu hingga Terraform menampilkan pesan "Apply complete!".
- Buka Google Cloud project Anda untuk melihat hasilnya. Di konsol Google Cloud , buka resource Anda di UI untuk memastikan bahwa Terraform telah membuat atau mengupdatenya.
Buka halaman IAM & Admin.
Klik Berikan Akses.
Di kolom New principals, masukkan ID akun layanan yang Anda salin sebelumnya.
Di kolom Pilih peran, pilih Vertex AI, lalu pilih Peran Pengguna Vertex AI.
Klik Simpan.
- Ukur kepuasan pelanggan dari ulasan.
- Pantau persepsi merek di media sosial.
- Prioritaskan tiket dukungan berdasarkan seberapa kesal pengguna.
- Temukan tema umum dalam masukan pelanggan.
- Mengatur dokumen berdasarkan pokok bahasan.
- Merutekan tiket dukungan menurut topik.
- Menemukan entri duplikat atau hampir duplikat.
- Kelompokkan masukan yang serupa.
- Mendukung aplikasi penelusuran semantik.
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Peran yang diperlukan
Untuk mendapatkan izin yang Anda perlukan guna menggunakan fungsi AI, minta administrator Anda untuk memberi Anda peran IAM berikut pada project:
Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.
Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.
Membuat data sampel
Untuk membuat set data bernama my_dataset untuk tutorial ini, jalankan kueri berikut.
CREATE SCHEMA my_dataset OPTIONS (location = 'LOCATION');
Selanjutnya, buat tabel bernama customer_feedback yang berisi contoh ulasan pelanggan
untuk sebuah perangkat:
CREATE TABLE my_dataset.customer_feedback AS (
SELECT
*
FROM
UNNEST( [STRUCT<review_id INT64, review_text STRING>
(1, "The battery life is incredible, and the screen is gorgeous! Best phone I've ever had. Totally worth the price."),
(2, "Customer support was a nightmare. It took three weeks for my order to arrive, and when it did, the box was damaged. Very frustrating!"),
(3, "The product does exactly what it says on the box. No complaints, but not exciting either."),
(4, "I'm so happy with this purchase! It arrived early and exceeded all my expectations. The quality is top-notch, although the setup was a bit tricky."),
(5, "The price is a bit too high for what you get. The material feels cheap and I'm worried it won't last. Service was okay."),
(6, "Absolutely furious! The item arrived broken, and getting a refund is proving impossible. I will never buy from them again."),
(7, "This new feature for account access is confusing. I can't find where to update my profile. Please fix this bug!"),
(8, "The shipping was delayed, but the support team was very helpful and kept me informed. The product itself is great, especially for the price.")
])
);
Membuat koneksi
Buat koneksi resource Cloud dan dapatkan akun layanan koneksi.
Pilih salah satu opsi berikut:
Konsol
bq
Terraform
Gunakan resource google_bigquery_connection.
Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.
Contoh berikut membuat koneksi resource Cloud bernama
my_cloud_resource_connection di region US:
Untuk menerapkan konfigurasi Terraform di project, selesaikan langkah-langkah di bagian berikut. Google Cloud
Menyiapkan Cloud Shell
Menyiapkan direktori
Setiap file konfigurasi Terraform harus memiliki direktorinya sendiri (juga disebut modul root).
Menerapkan perubahan
Memberikan izin ke akun layanan koneksi
Berikan peran Vertex AI User ke akun layanan koneksi. Anda harus memberikan peran ini di project yang sama dengan yang Anda buat atau pilih di bagian
Sebelum memulai. Pemberian peran dalam project lain akan menghasilkan error bqcx-1234567890-xxxx@gcp-sa-bigquery-condel.iam.gserviceaccount.com does not have the permission to access resource.
Untuk memberikan peran, ikuti langkah-langkah berikut:
Mengategorikan sentimen keseluruhan
Ekstraksi sentimen keseluruhan yang dinyatakan dalam teks dapat membantu mendukung kasus penggunaan seperti berikut:
Kueri berikut menunjukkan cara menggunakan fungsi AI.CLASSIFY untuk mengklasifikasikan ulasan dari tabel customer_feedback sebagai positif, negatif, atau netral:
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => ['positive', 'negative', 'neutral'],
connection_id => "CONNECTION_ID") AS sentiment
FROM
my_dataset.customer_feedback;
Hasilnya akan terlihat mirip dengan berikut ini:
+-----------+------------------------------------------+-----------+ | review_id | review_text | sentiment | +-----------+------------------------------------------+-----------+ | 7 | This new feature for account access is | negative | | | confusing. I can't find where to update | | | | my profile. Please fix this bug! | | +-----------+------------------------------------------+-----------+ | 4 | "I'm so happy with this purchase! It | positive | | | arrived early and exceeded all my | | | | expectations. The quality is top-notch, | | | | although the setup was a bit tricky." | | +-----------+------------------------------------------+-----------+ | 2 | "Customer support was a nightmare. It | negative | | | took three weeks for my order to | | | | arrive, and when it did, the box was | | | | damaged. Very frustrating!" | | +-----------+------------------------------------------+-----------+ | 1 | "The battery life is incredible, and | positive | | | the screen is gorgeous! Best phone I've | | | | ever had. Totally worth the price." | | +-----------+------------------------------------------+-----------+ | 8 | "The shipping was delayed, but the | positive | | | support team was very helpful and kept | | | | me informed. The product itself is | | | | great, especially for the price." | | +-----------+------------------------------------------+-----------+ | 5 | The price is a bit too high for what | negative | | | you get. The material feels cheap and | | | | I'm worried it won't last. Service was | | | | okay. | | +-----------+------------------------------------------+-----------+ | 3 | "The product does exactly what it says | neutral | | | on the box. No complaints, but not | | | | exciting either." | | +-----------+------------------------------------------+-----------+ | 6 | "Absolutely furious! The item arrived | negative | | | broken, and getting a refund is proving | | | | impossible. I will never buy from them | | | | again." | | +-----------+------------------------------------------+-----------+
Menganalisis sentimen berbasis aspek
Jika sentimen keseluruhan seperti positif atau negatif tidak cukup untuk kasus penggunaan Anda, Anda dapat menganalisis aspek tertentu dari makna teks. Misalnya, Anda mungkin ingin memahami sikap pengguna terhadap kualitas produk, tanpa mempertimbangkan pendapat mereka tentang harganya. Anda bahkan dapat meminta nilai kustom untuk menunjukkan bahwa aspek tertentu tidak berlaku.
Contoh berikut menunjukkan cara menggunakan fungsi AI.SCORE untuk memberi rating sentimen pengguna dari 1 hingga 10 berdasarkan seberapa positif setiap ulasan dalam tabel customer_feedback terhadap harga, layanan pelanggan, dan kualitas. Fungsi
ini menampilkan nilai kustom -1 jika aspek tidak disebutkan dalam
ulasan sehingga Anda dapat memfilternya nanti.
SELECT
review_id,
review_text,
AI.SCORE(
("Score 0.0 to 10 on positive sentiment about PRICE for review: ", review_text,
"If price is not mentioned, return -1.0"),
connection_id => "CONNECTION_ID") AS price_score,
AI.SCORE(
("Score 0.0 to 10 on positive sentiment about CUSTOMER SERVICE for review: ", review_text,
"If customer service is not mentioned, return -1.0"),
connection_id => "CONNECTION_ID") AS service_score,
AI.SCORE(
("Score 0.0 to 10 on positive sentiment about QUALITY for review: ", review_text,
"If quality is not mentioned, return -1.0"),
connection_id => "CONNECTION_ID") AS quality_score
FROM
my_dataset.customer_feedback
LIMIT 3;
Hasilnya akan terlihat mirip dengan berikut ini:
+-----------+------------------------------------------+--------------+---------------+---------------+ | review_id | review_text | price_score | service_score | quality_score | +-----------+------------------------------------------+--------------+---------------+---------------+ | 4 | "I'm so happy with this purchase! It | -1.0 | -1.0 | 9.5 | | | arrived early and exceeded all my | | | | | | expectations. The quality is top-notch, | | | | | | although the setup was a bit tricky." | | | | +-----------+------------------------------------------+--------------+---------------+---------------+ | 8 | "The shipping was delayed, but the | 9.0 | 8.5 | 9.0 | | | support team was very helpful and kept | | | | | | me informed. The product itself is | | | | | | great, especially for the price." | | | | +-----------+------------------------------------------+--------------+---------------+---------------+ | 6 | "Absolutely furious! The item arrived | -1.0 | 1.0 | 0.0 | | | broken, and getting a refund is proving | | | | | | impossible. I will never buy from them | | | | | | again." | | | | +-----------+------------------------------------------+--------------+---------------+---------------+
Mendeteksi emosi
Selain sentimen positif atau negatif, Anda dapat mengklasifikasikan teks berdasarkan emosi tertentu yang Anda pilih. Hal ini berguna jika Anda ingin lebih memahami respons pengguna, atau menandai masukan yang sangat emosional untuk ditinjau.
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => ['joy', 'anger', 'sadness', 'surprise', 'fear', 'disgust', 'neutral', 'other'],
connection_id => "CONNECTION_ID"
) AS emotion
FROM
my_dataset.customer_feedback;
Hasilnya akan terlihat mirip dengan berikut ini:
+-----------+------------------------------------------+---------+ | review_id | review_text | emotion | +-----------+------------------------------------------+---------+ | 2 | "Customer support was a nightmare. It | anger | | | took three weeks for my order to | | | | arrive, and when it did, the box was | | | | damaged. Very frustrating!" | | +-----------+------------------------------------------+---------+ | 7 | This new feature for account access is | anger | | | confusing. I can't find where to update | | | | my profile. Please fix this bug! | | +-----------+------------------------------------------+---------+ | 4 | "I'm so happy with this purchase! It | joy | | | arrived early and exceeded all my | | | | expectations. The quality is top-notch, | | | | although the setup was a bit tricky." | | +-----------+------------------------------------------+---------+ | 1 | "The battery life is incredible, and | joy | | | the screen is gorgeous! Best phone I've | | | | ever had. Totally worth the price." | | +-----------+------------------------------------------+---------+ | 8 | "The shipping was delayed, but the | joy | | | support team was very helpful and kept | | | | me informed. The product itself is | | | | great, especially for the price." | | +-----------+------------------------------------------+---------+ | 5 | The price is a bit too high for what | sadness | | | you get. The material feels cheap and | | | | I'm worried it won't last. Service was | | | | okay. | | +-----------+------------------------------------------+---------+ | 3 | "The product does exactly what it says | neutral | | | on the box. No complaints, but not | | | | exciting either." | | +-----------+------------------------------------------+---------+ | 6 | "Absolutely furious! The item arrived | anger | | | broken, and getting a refund is proving | | | | impossible. I will never buy from them | | | | again." | | +-----------+------------------------------------------+---------+
Mengategorikan ulasan menurut topik
Anda dapat menggunakan fungsi AI.CLASSIFY untuk mengelompokkan ulasan ke dalam topik yang telah ditentukan.
Misalnya, Anda dapat melakukan hal berikut:
Contoh berikut menunjukkan cara mengklasifikasikan masukan pelanggan ke dalam berbagai jenis seperti masalah penagihan atau akses akun, lalu menghitung jumlah ulasan yang termasuk dalam setiap kategori:
SELECT
AI.CLASSIFY(
review_text,
categories => ['Billing Issue', 'Account Access',
'Product Bug', 'Feature Request',
'Shipping Delay', 'Other'],
connection_id => "CONNECTION_ID") AS topic,
COUNT(*) AS number_of_reviews,
FROM
my_dataset.customer_feedback
GROUP BY topic
ORDER BY number_of_reviews DESC;
Hasilnya akan terlihat mirip dengan berikut ini:
+----------------+-------------------+ | topic | number_of_reviews | +----------------+-------------------+ | Other | 5 | | Shipping Delay | 2 | | Product Bug | 1 | +----------------+-------------------+
Mengidentifikasi ulasan yang mirip secara semantik
Anda dapat menggunakan fungsi AI.SCORE untuk menilai seberapa mirip dua teks secara semantik dengan memintanya untuk menilai kesamaan makna. Cara ini dapat membantu Anda
melakukan tugas seperti berikut:
Kueri berikut menemukan ulasan yang membahas kesulitan dalam menyiapkan produk:
SELECT
review_id,
review_text,
AI.SCORE(
(
"""How similar is the review to the concept of 'difficulty in setting up the product'?
A higher score indicates more similarity. Review: """,
review_text),
connection_id => "CONNECTION_ID") AS setup_difficulty
FROM my_dataset.customer_feedback
ORDER BY setup_difficulty DESC
LIMIT 2;
Hasilnya akan terlihat mirip dengan berikut ini:
+-----------+------------------------------------------+------------------+ | review_id | review_text | setup_difficulty | +-----------+------------------------------------------+------------------+ | 4 | "I'm so happy with this purchase! It | 3 | | | arrived early and exceeded all my | | | | expectations. The quality is top-notch, | | | | although the setup was a bit tricky." | | +-----------+------------------------------------------+------------------+ | 7 | This new feature for account access is | 1 | | | confusing. I can't find where to update | | | | my profile. Please fix this bug! | | +-----------+------------------------------------------+------------------+
Anda juga dapat menggunakan fungsi AI.IF untuk menemukan ulasan yang terkait dengan teks:
SELECT
review_id,
review_text
FROM my_dataset.customer_feedback
WHERE
AI.IF(
(
"Does this review discuss difficulty setting up the product? Review: ",
review_text),
connection_id => "CONNECTION_ID");
Menggabungkan fungsi
Sebaiknya gabungkan fungsi ini dalam satu kueri. Misalnya, kueri berikut pertama-tama memfilter ulasan untuk sentimen negatif, lalu mengklasifikasikannya berdasarkan jenis kekecewaan:
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => [
'Poor Quality', 'Bad Customer Service', 'High Price', 'Other Negative'],
connection_id => "CONNECTION_ID") AS negative_topic
FROM my_dataset.customer_feedback
WHERE
AI.IF(
("Does this review express a negative sentiment? Review: ", review_text),
connection_id => "CONNECTION_ID");
Membuat UDF perintah yang dapat digunakan kembali
Agar kueri Anda tetap mudah dibaca, Anda dapat menggunakan kembali logika perintah dengan membuat
fungsi yang ditentukan pengguna. Kueri berikut membuat fungsi untuk mendeteksi sentimen negatif dengan memanggil AI.IF dengan perintah kustom. Kemudian, fungsi tersebut memanggil fungsi itu untuk memfilter berdasarkan ulasan negatif.
CREATE OR REPLACE FUNCTION my_dataset.is_negative_sentiment(review_text STRING)
RETURNS BOOL
AS (
AI.IF(
("Does this review express a negative sentiment? Review: ", review_text),
connection_id => "CONNECTION_ID")
);
SELECT
review_id,
review_text
FROM my_dataset.customer_feedback
WHERE my_dataset.is_negative_sentiment(review_text);
Pembersihan
Agar tidak dikenai biaya, Anda dapat menghapus project yang berisi resource yang Anda buat, atau menyimpan project dan menghapus setiap resource.
Menghapus project Anda
Untuk menghapus project:
Menghapus set data
Untuk menghapus set data dan semua resource yang ada di dalamnya, termasuk semua tabel dan fungsi, jalankan kueri berikut:
DROP SCHEMA my_dataset CASCADE;