관리형 AI 함수로 시맨틱 분석 실행
이 튜토리얼에서는 BigQuery ML 관리형 AI 함수를 사용하여 고객 의견에 대한 시맨틱 분석을 실행하는 방법을 보여줍니다.
목표
이 가이드의 목표는 다음과 같습니다.
- 데이터 세트를 만들고 감정 데이터를 테이블에 로드합니다.
- 다음 AI 함수를 사용하여 시맨틱 분석을 실행합니다.
AI.IF: 자연어 조건으로 데이터를 필터링합니다.AI.SCORE: 감정별로 입력을 평가합니다.AI.CLASSIFY: 입력을 사용자 정의 카테고리로 분류합니다.
비용
이 튜토리얼에서는 비용이 청구될 수 있는 다음과 같은 Google Cloud구성요소를 사용합니다.
- BigQuery
- BigQuery ML
BigQuery 비용에 대한 자세한 내용은 BigQuery 가격 책정 페이지를 참조하세요.
BigQuery ML 비용에 대한 자세한 내용은 BigQuery ML 가격 책정을 참조하세요.
시작하기 전에
- 계정에 로그인합니다. Google Cloud 를 처음 사용하는 경우 Google Cloud, 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
-
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
BigQuery API를 사용 설정합니다.
API 사용 설정에 필요한 역할
API를 사용 설정하려면
serviceusage.services.enable권한이 포함된 서비스 사용량 관리자 IAM 역할(roles/serviceusage.serviceUsageAdmin)이 필요합니다. 역할 부여 방법 알아보기새 프로젝트에서는 BigQuery API가 자동으로 사용 설정됩니다.
- (선택사항) 프로젝트에 대한 결제를 사용 설정합니다. 결제를 사용 설정하거나 신용카드를 제공하지 않는 경우 이 문서의 단계가 계속 작동합니다. BigQuery에서는 단계를 수행하기 위한 샌드박스를 제공합니다. 자세한 내용은 BigQuery 샌드박스 사용 설정을 참조하세요.
필요한 역할
AI 함수를 사용하는 데 필요한 권한을 얻으려면 관리자에게 프로젝트에 대한 다음 IAM 역할을 부여해 달라고 요청하세요.
-
쿼리 작업 및 로드 작업 실행:
BigQuery 작업 사용자 (
roles/bigquery.jobUser) -
데이터 세트 만들기, 테이블 만들기, 테이블에 데이터 로드, 테이블 쿼리:
BigQuery 데이터 편집자 (
roles/bigquery.dataEditor)
역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.
커스텀 역할이나 다른 사전 정의된 역할을 통해 필요한 권한을 얻을 수도 있습니다.
샘플 데이터 만들기
이 튜토리얼의 데이터 세트 my_dataset을 만들려면 다음 쿼리를 실행합니다.
CREATE SCHEMA my_dataset OPTIONS (location = 'LOCATION');
다음으로 기기에 대한 샘플 고객 리뷰가 포함된 customer_feedback 테이블을 만듭니다.
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.")
])
);
전반적인 감정 분류
다음과 같은 사용 사례를 지원하기 위해 텍스트에 표현된 전반적인 감정을 추출하는 것이 유용할 수 있습니다.
- 리뷰에서 고객 만족도를 측정합니다.
- 소셜 미디어에서 브랜드 인식을 모니터링합니다.
- 사용자의 불쾌감 정도에 따라 지원 티켓의 우선순위를 지정합니다.
다음 쿼리는 AI.CLASSIFY 함수를 사용하여
customer_feedback 테이블의 리뷰를 긍정적, 부정적 또는
중립적으로 분류하는 방법을 보여줍니다.
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => ['positive', 'negative', 'neutral']) AS sentiment
FROM
my_dataset.customer_feedback;
결과는 다음과 유사합니다.
+-----------+------------------------------------------+-----------+ | 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." | | +-----------+------------------------------------------+-----------+
측면 기반 감정 분석
사용 사례에 긍정적 또는 부정적 과 같은 전반적인 감정이 충분하지 않은 경우 텍스트 의미의 특정 측면을 분석할 수 있습니다. 예를 들어 가격에 대한 사용자의 생각과 관계없이 제품 품질에 대한 사용자의 태도를 파악할 수 있습니다. 특정 측면이 적용되지 않음을 나타내는 커스텀 값을 요청할 수도 있습니다.
다음 예시에서는 AI.SCORE 함수를 사용하여 customer_feedback 테이블의 각 리뷰가 가격, 고객 서비스, 품질에 얼마나 우호적인지에 따라 사용자 감정을 1에서 10까지 평가하는 방법을 보여줍니다. 함수는 리뷰에서 측면이 언급되지 않은 경우 나중에 필터링할 수 있도록 커스텀 값 -1을 반환합니다.
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")) 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")) 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")) AS quality_score
FROM
my_dataset.customer_feedback
LIMIT 3;
결과는 다음과 유사합니다.
+-----------+------------------------------------------+--------------+---------------+---------------+ | 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." | | | | +-----------+------------------------------------------+--------------+---------------+---------------+
감정 감지
긍정적 또는 부정적 감정 외에도 선택한 특정 감정을 기준으로 텍스트를 분류할 수 있습니다. 이는 사용자 응답을 더 잘 이해하거나 검토를 위해 감정적인 의견에 플래그를 지정하려는 경우에 유용합니다.
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => ['joy', 'anger', 'sadness', 'surprise', 'fear', 'disgust', 'neutral', 'other']
) AS emotion
FROM
my_dataset.customer_feedback;
결과는 다음과 유사합니다.
+-----------+------------------------------------------+---------+ | 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." | | +-----------+------------------------------------------+---------+
주제별 리뷰 분류
AI.CLASSIFY 함수를 사용하여 리뷰를 사전 정의된 주제로 그룹화할 수 있습니다.
예를 들어 다음을 수행할 수 있습니다.
- 고객 의견에서 일반적인 주제를 발견합니다.
- 주제별로 문서를 정리합니다.
- 주제별로 지원 티켓을 라우팅합니다.
다음 예시에서는 고객 의견을 결제 문제 또는 계정 액세스 와 같은 다양한 유형으로 분류한 후 각 카테고리에 속하는 리뷰 수를 계산하는 방법을 보여줍니다.
SELECT
AI.CLASSIFY(
review_text,
categories => ['Billing Issue', 'Account Access',
'Product Bug', 'Feature Request',
'Shipping Delay', 'Other']) AS topic,
COUNT(*) AS number_of_reviews,
FROM
my_dataset.customer_feedback
GROUP BY topic
ORDER BY number_of_reviews DESC;
결과는 다음과 유사합니다.
+----------------+-------------------+ | topic | number_of_reviews | +----------------+-------------------+ | Other | 5 | | Shipping Delay | 2 | | Product Bug | 1 | +----------------+-------------------+
의미상 유사한 리뷰 식별
AI.SCORE 함수를 사용하여 의미 유사성을 평가하도록 요청하여 두 텍스트가 의미상 얼마나 유사한지 평가할 수 있습니다. 이는 다음과 같은 작업을 수행하는 데 도움이 될 수 있습니다.
- 중복 또는 거의 중복된 항목을 찾습니다.
- 유사한 의견을 그룹화합니다.
- 시맨틱 검색 애플리케이션을 지원합니다.
다음 쿼리는 제품 설정의 어려움을 논의하는 리뷰를 찾습니다.
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)) AS setup_difficulty
FROM my_dataset.customer_feedback
ORDER BY setup_difficulty DESC
LIMIT 2;
결과는 다음과 유사합니다.
+-----------+------------------------------------------+------------------+ | 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! | | +-----------+------------------------------------------+------------------+
AI.IF 함수를 사용하여 텍스트와 관련된 리뷰를 찾을 수도 있습니다.
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));
함수 결합
단일 쿼리에서 이러한 함수를 결합하는 것이 유용할 수 있습니다. 예를 들어 다음 쿼리는 먼저 부정적인 감정의 리뷰를 필터링한 후 불만의 유형별로 분류합니다.
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => [
'Poor Quality', 'Bad Customer Service', 'High Price', 'Other Negative']) AS negative_topic
FROM my_dataset.customer_feedback
WHERE
AI.IF(
("Does this review express a negative sentiment? Review: ", review_text));
재사용 가능한 프롬프트 UDF 만들기
쿼리를 읽기 쉽게 유지하려면
사용자 정의 함수를 만들어 프롬프트 로직을 재사용할 수 있습니다. 다음 쿼리는 커스텀 프롬프트로 AI.IF를 호출하여 부정적인 감정을 감지하는 함수를 만듭니다. 그런 다음 해당 함수를 호출하여 부정적인 리뷰로 필터링합니다.
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))
);
SELECT
review_id,
review_text
FROM my_dataset.customer_feedback
WHERE my_dataset.is_negative_sentiment(review_text);
삭제
비용이 청구되지 않도록 하려면 만든 리소스가 포함된 프로젝트를 삭제하거나 프로젝트를 유지하고 개별 리소스를 삭제하면 됩니다.
프로젝트 삭제
프로젝트를 삭제하는 방법은 다음과 같습니다.
- 콘솔에서 리소스 관리 페이지로 이동합니다. Google Cloud
- 프로젝트 목록에서 삭제할 프로젝트를 선택하고 삭제를 클릭합니다.
- 대화상자에서 프로젝트 ID를 입력한 후 종료 를 클릭하여 프로젝트를 삭제합니다.
데이터 세트 삭제
모든 테이블과 함수를 포함하여 데이터 세트와 데이터 세트에 포함된 모든 리소스를 삭제하려면 다음 쿼리를 실행합니다.
DROP SCHEMA my_dataset CASCADE;