1. 簡介
在本程式碼研究室中,您將使用 Gemini Pro 模型和 Google Cloud 的 ABAP SDK,對產品評論執行情緒分析。如果 SAP 系統是託管在 Compute Engine VM 執行個體上,我們會逐步說明如何設定驗證,以便使用權杖存取 Google Cloud Vertex AI API。
使用的服務清單如下:
- Compute Engine
- 網路服務
- Cloud Shell
- Vertex AI
建構項目
您將執行下列操作:
- 設定 SAP 系統上安裝的 ABAP SDK,以便連線至 Google API。
- 建立範例報表程式,呼叫 Gemini AI 並對產品評論執行情緒分析。
2. 需求條件
- 瀏覽器,例如 Chrome 或 Firefox。
- 啟用計費功能的 Google Cloud 專案,或建立 Google Cloud Platform 90 天免費試用帳戶。
- 系統中已安裝 SAP GUI (Windows 或 Java)。如果系統已安裝 SAP GUI,請使用 VM 外部 IP 位址做為應用程式伺服器 IP,連線至 SAP。如果您使用 Mac,也可以安裝 這個連結提供的 SAP GUI for Java。
3. 事前準備
- 在 Google Cloud 控制台的專案選取器頁面中,選取或建立 Google Cloud 專案 (例如:
abap-sdk-poc).
- 確認 Cloud 專案已啟用計費功能。瞭解如何檢查專案是否已啟用計費功能。如果您使用的是 90 天免費試用帳戶,請略過這個步驟。
- 您將使用 Cloud Shell,這是 Google Cloud 中執行的指令列環境。在 Cloud Shell 中,按一下右上角的「啟用 Cloud Shell」:
- 請確認所有必要的 API (AM Service Account Credentials API、Vertex AI API) 都已啟用。
- 執行下列指令,驗證帳戶並將預設專案設為
abap-sdk-poc
。以區域us-west4-b
為例。如有需要,請根據偏好設定,在下列指令中變更專案和區域。
gcloud auth login
gcloud config set project abap-sdk-poc
gcloud config set compute/zone us-west4-b
PROJECT_NAME=abap-sdk-poc
REGION=us-west4
ZONE=us-west4-b
- 確認您有權存取已安裝 ABAP SDK for Google Cloud 的 SAP 系統,並具備 SAP EPM 資料。
- 您可以參閱「在 Google Cloud Platform 上安裝 ABAP Platform Trial,並安裝 ABAP SDK」程式碼研究室,設定新系統。
4. 建立服務帳戶並設定 Vertex AI 使用者角色
- 執行下列指令來建立服務帳戶 (如果尚未建立)。
gcloud iam service-accounts create abap-sdk-dev \
--description="ABAP SDK Dev Account" \
--display-name="ABAP SDK Dev Account"
gcloud projects add-iam-policy-binding $PROJECT_NAME \
--member=serviceAccount:abap-sdk-codelabs@$PROJECT_NAME.iam.gserviceaccount.com \
--role=roles/aiplatform.user
5. 設定用戶端金鑰
登入 SAP 系統。如果您使用 Codelab「在 Google Cloud Platform 上安裝 ABAP Platform Trial 並安裝 ABAP SDK」佈建的系統,請使用預設佈建的使用者名稱和密碼登入。
- 在 SAP GUI 中輸入交易代碼 SPRO。
- 按一下「SAP Reference IMG」。
- 依序點選「ABAP SDK for Google Cloud」>「Basic Settings」>「Configure Client Key」。
- 按一下「New Entries」。
- 在下列欄位中輸入值:
欄位 | 說明 |
Google Cloud 金鑰名稱 | ABAP_SDK_DEMO |
Google Cloud 服務帳戶名稱 | abap-sdk-dev@abap-sdk-poc.iam.gserviceaccount.com |
Google Cloud 範圍 | https://www.googleapis.com/auth/cloud-platform |
Google Cloud 專案 ID | abap-sdk-poc |
授權類別 | /GOOG/CL_AUTH_GOOGLE. |
將其他欄位留空
6. 建立 RFC 目的地
使用交易代碼 SM59,為 IAM 憑證和 Vertex AI API 建立 RFC 目的地。如需建立 RFC 目的地,請參閱這篇文章的詳細步驟。
RFC 目的地名稱 | 目標主機 (API 端點) | 附註 |
ZGOOG_IAMCREDENTIALS |
| 這個 RFC 目的地以 IAM API 為目標。 |
ZGOOG_VERTEX_AI |
| 這個 RFC 目的地以 Vertex AI API us-central1 端點為目標。 |
- 在「Technical Settings」(技術設定) 分頁中,輸入 ZGOOG_IAMCREDENTIALS 目的地的下列詳細資料。
- 在「Technical Settings」(技術設定) 分頁中,輸入 ZGOOG_VERTEX_AI 目的地的下列詳細資料。
- 在「SSL Certificate」欄位中,確認已為兩個 RFC 目的地選取「DEFAULT SSL Client (Standard)」選項。
7. 設定服務對應
如要設定 IAM API 和 Vertex AI API 的服務對應表,請完成下列步驟:
- 在 SAP GUI 中輸入交易代碼 SPRO。
- 按一下「SAP Reference IMG」。
- 依序點選「ABAP SDK for Google Cloud」>「Basic Settings」>「Configure Service Mapping」。
- 按一下「IAM 憑證」和「Vertex AI API」的「新項目」,然後更新 RFC 目的地,如下所示。
8. 驗證設定
如要驗證驗證設定,請完成下列步驟:
- 在 SAP GUI 中輸入交易代碼 SPRO。
- 按一下「SAP Reference IMG」。
- 依序點選「ABAP SDK for Google Cloud」>「Utilities」>「Validate Authentication Configuration」。
- 輸入用戶端金鑰名稱,例如
ABAP_SDK_DEMO
。 - 按一下「執行」,檢查整體流程是否設定成功。
- 「結果」欄中的綠色勾號表示所有設定步驟都已順利完成。
9. 研究產品評論資料
ABAP Platform Trial 預先安裝了 SAP Enterprise Procurement Model (EPM),這是 SAP 提供的端對端應用程式,可用於示範和測試。用於情緒分析的產品評論會儲存在 SNWD_REV_ITEM 資料表中。
您可以使用交易代碼 SE16 查看資料表資料。我們將使用「RATING_TEXT」欄位中的範例文字。
您也可以選擇從 Fiori Launchpad (TCode:/UI2/FLP) 存取「管理產品」應用程式,查看資料。
下圖顯示產品清單。
按一下產品即可查看產品評分和範例評論。
在下一節中,我們將使用這些範例產品評論,透過 Google LLM 分析情緒。
10. 使用 Vertex AI Studio 分析情緒
- 開啟 Vertex AI 資訊主頁,然後選取 Vertex AI Studio 下方的「語言」。
- 建立新的文字提示。
- 確認已選取 Gemini Pro 模型。
- 在編輯器中輸入下列提示,指示模型分析產品評論中的顧客情緒。
- 按一下「提交」,即可生成模型回覆。
提示:
What's the Customer sentiment in the below product review
Horrible! Unsatisfied in every way! The description is wrong. I sent it back. I want my money back! It is so horrible that I can't even believe it! Too expensive for what I received. I'd expect a little more durability. No instructions included for use or installation. I'm actually really surprised by the positive reviews, which I relied on when ordering. Called customer service..no answer. Looks better than it works. The Worst I have ever seen! Honestly I have no clue what you had in mind when choosing to offer this product. Not sure if I should even post one star.... It broke after 1 day. Poor Quality. Didn't work, that is why I cannot recommend this product.
您可以使用「SNWD_REV_ITEM」表格中的評論資料,透過額外提示測試模型。
11. 建立報表程式來呼叫 Gemini Pro 模型
在這個步驟中,我們會從 ABAP 呼叫 Gemini Pro 模型,對產品評論執行情緒分析。為進行示範,程式會選取最多 10 則產品評論,並使用 ABAP SDK 呼叫 Gemini Pro 模型,判斷每則評論的情緒。根據分析結果,模型會傳回「正面」、「負面」、「中性」或「混合」等值。
- 登入 SAP 系統。
- 前往交易代碼 SE38,並建立名為 ZSENTIMENT_ANALYSIS 的報表程式。
- 在隨即開啟的彈出式視窗中,提供如下所示的詳細資料,然後按一下「儲存」。
- 在下一個彈出式視窗中,選取「本機物件」或提供適當的套件名稱。
REPORT zsentiment_analysis.
DATA lo_client TYPE REF TO /goog/cl_aiplatform_v1.
DATA lv_p_projects_id TYPE string.
DATA lv_p_locations_id TYPE string.
DATA lv_p_publishers_id TYPE string.
DATA lv_p_models_id TYPE string.
DATA ls_input TYPE /goog/cl_aiplatform_v1=>ty_726.
DATA ls_output TYPE /goog/cl_aiplatform_v1=>ty_727.
DATA lv_ret_code TYPE i.
DATA lv_err_text TYPE string.
DATA ls_err_resp TYPE /goog/err_resp.
DATA lv_msg TYPE string.
DATA lo_exception TYPE REF TO /goog/cx_sdk.
DATA es_raw TYPE string.
TYPES:
BEGIN OF t_reviews,
product_id TYPE snwd_product_id,
sentiment TYPE string,
rating_text TYPE snwd_rating_text,
END OF t_reviews.
DATA lt_reviews TYPE STANDARD TABLE OF t_reviews WITH DEFAULT KEY.
FIELD-SYMBOLS <fs_review> TYPE t_reviews.
TRY.
" Open HTTP Connection
lo_client = NEW #( iv_key_name = 'ABAP_SDK_DEMO' ).
" Populate relevant parameters
lv_p_projects_id = lo_client->gv_project_id.
lv_p_locations_id = 'us-central1'.
lv_p_publishers_id = 'google'.
lv_p_models_id = 'gemini-1.0-pro'.
SELECT a~product_id AS product_id c~rating_text
FROM ( ( snwd_pd AS a
INNER JOIN snwd_rev_head AS b ON a~node_key = b~entity_key )
INNER JOIN snwd_rev_item AS c ON b~node_key = c~parent_key )
INTO CORRESPONDING FIELDS OF TABLE lt_reviews UP TO 10 ROWS.
LOOP AT lt_reviews ASSIGNING <fs_review>.
" Construct the prompt
DATA(lv_prompt) = |DO NOT EXPLAIN and your response should not have more than one word.| &&
|Classify the overall sentiment of this product review as ONLY ONE of| &&
| the following: Positive, Negative, Neutral, or Mixed.| &&
cl_abap_char_utilities=>newline &&
<fs_review>-rating_text.
" Set the Model Parameters and Prompt
ls_input = VALUE #( generation_config = VALUE #( max_output_tokens = 10
temperature = '0.2'
top_p = '0.8'
top_k = '40' )
contents = VALUE #( ( role = 'user'
parts = VALUE #( ( text = lv_prompt ) ) ) ) ).
" Call Gemini Pro to identify sentiments.
lo_client->generate_content_models( EXPORTING iv_p_projects_id = lv_p_projects_id
iv_p_locations_id = lv_p_locations_id
iv_p_publishers_id = lv_p_publishers_id
iv_p_models_id = lv_p_models_id
is_input = ls_input
IMPORTING
es_output = ls_output
ev_ret_code = lv_ret_code
ev_err_text = lv_err_text
es_err_resp = ls_err_resp ).
IF lo_client->is_success( lv_ret_code ) = abap_true.
LOOP AT ls_output-candidates INTO DATA(ls_candidate).
LOOP AT ls_candidate-content-parts INTO DATA(ls_part).
<fs_review>-sentiment = ls_part-text.
EXIT.
ENDLOOP.
EXIT.
ENDLOOP.
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.
ENDLOOP.
cl_demo_output=>display( lt_reviews ).
" Close HTTP Connection
lo_client->close( ).
CATCH /goog/cx_sdk INTO lo_exception.
lv_msg = lo_exception->get_text( ).
MESSAGE lv_msg TYPE 'E'.
ENDTRY.
- 執行程式,查看評論文字的情緒分析結果。
12. 恭喜
恭喜!您已成功開發範例程式,使用 ABAP SDK for Google Cloud 搭配 Gemini Pro AI 執行情緒分析。
13. 清除所用資源
如不想繼續進行與 Google Cloud 適用的 ABAP SDK 相關的額外程式碼研究室,請繼續進行清理作業。
刪除專案
- 刪除 Google Cloud 專案:
gcloud projects delete abap-sdk-poc
刪除個別資源
- 依序前往 IMG > Google Cloud > Basic Settings > Configure Client Key,刪除用戶端金鑰設定項目。
- 前往「IMG」>「Google Cloud」>「Basic Settings」>「Configure Client Key」,刪除服務對應設定項目。
- 刪除 RFC 目的地 ZGOOG_IAMCREDENTIALS 和 ZGOOG_VERTEX_AI。
- 刪除回報程式
ZSENTIMENT_ANALYSIS
.
- 刪除服務帳戶。
gcloud iam service-accounts delete \
abap-sdk-dev@abap-sdk-poc.iam.gserviceaccount.com