本文說明參考架構,可用於建構由 Gemini 等大型語言模型 (LLM) 提供動力的商業規則引擎,以便在 SAP 業務應用程式中自動做出決策。這項服務會使用 ABAP SDK for Google Cloud 的各種整合功能。
本文件的目標對象包括 ABAP 開發人員、SAP 解決方案架構師和雲端架構師。本文假設您對 ABAP SDK for Google Cloud、Vertex AI SDK for ABAP 和 LLM 簡介有基本瞭解。
什麼是商業規則引擎 (BRE)?
業務規則引擎 (BRE) 是一種軟體系統,可讓您定義、部署及執行業務規則。這些規則基本上是邏輯陳述式,用於決定商家的運作方式。BRE 會將邏輯陳述式外部化,而非將業務規則硬式編碼至應用程式,因此可有效管理及修改規則。
Gemini 輔助的 BRE 會使用 Google 新一代 AI 模型 Gemini 的進階語言處理和推理功能,根據用於做為業務規則的設計提示存放區,自動做出決策。這些提示包含特定指示和預期的輸出格式,可引導 Gemini 按照預先定義的業務邏輯分析資料、找出模式,並做出決策。
架構
下圖顯示為 SAP 應用程式建立 Gemini 技術輔助的 BRE 的參考架構:
這個參考架構包含下列元件:
元件 | 子系統 | 詳細資料 |
---|---|---|
1 | SAP ERP | SAP ERP 系統 (例如 SAP S/4HANA),使用 Gemini 技術輔助的 BRE 擷取洞察資料,以便自動做出決策。根據用途,您可以為 BRE 模組提供規則 ID 和必要的輸入資料。 |
2 | BRE 模組 | BRE 模組會使用 ABAP 適用的 Google Cloud SDK 從 Cloud Storage 擷取已設定的規則,並使用 ABAP 適用的 Vertex AI SDK 呼叫 Gemini 來處理規則。 |
3 | 規則存放區 | 將規則 (設計的提示) 儲存在 Cloud Storage 中,並以文字檔的形式 (例如 .txt 或 .prompt ) 儲存,並使用一致的命名慣例。例如:RiskShiftingClause.prompt 。如要以結構化方式管理設計的提示,您也可以考慮使用 Cloud SQL 等資料庫。 |
4 | 規則處理器 | ABAP 適用的 Vertex AI SDK 會將輸入資料 (例如運送指示) 和擷取的規則 (提示) 傳送至 Gemini。Gemini 會處理提示和輸入資料、執行分析,並以指定格式傳回結果。 |
使用的產品
此參考架構使用以下 Google Cloud 產品:
ABAP SDK for Google Cloud:協助您開發 ABAP 應用程式,將 SAP 系統連結至 Cloud Storage 等 Google Cloud 服務。
ABAP 版 Vertex AI SDK:可讓您在 SAP 環境中使用 Google 的 Vertex AI。這可簡化在 SAP 應用程式中建構 AI 技術輔助功能的程序。
Cloud Storage:可用於線上儲存及存取任何資料量的雲端服務。
Vertex AI:可讓您為各種應用程式建構、部署及擴充機器學習模型。
用途
請考慮以下情境:貴公司收到的進貨訂單 (PO) 是以 PDF 格式,且使用自動化程序在 SAP 中產生銷售訂單。您需要更新這項程序,以便找出並減輕在傳入的 PO 中提交的提交評論中所列的風險轉移條款。
合約中的風險轉移條款是指將風險從一方轉移至另一方的條款。如果未能識別及處理這些條款,公司可能會面臨意料之外的成本、法律訴訟和聲譽受損。
以下是採購單文件中提交評論中所寫的風險轉移條款範例。
「賣方保證,自交貨日起算,產品將在十年內免於瑕疵。」
您需要為下列項目編寫規則:
- 找出並分類風險轉移條款。
- 說明為何這項內容可能會觸發警示。
- 提供如何減輕風險的建議。
您可以使用 Gemini 建構 BRE,產生智慧型 JSON 回應,讓 SAP 業務應用程式做出自動決策、觸發任何程序拒絕,或啟動下游人工驗證。
風險轉移條款範例
下表列出進站訂單中可能出現的風險轉移條款範例:
問題類別 | 例子條款 (交付說明) | 潛在問題 |
---|---|---|
賠償過度擴張 | 賣方應針對因產品而生或與產品相關的所有請求、損失、損害和費用 (包括但不限於律師費),向買方提供賠償。 | 如果範圍過於廣泛,且賣方須為意外事件或客戶濫用行為負責,就可能會出現問題。 |
過期機密性 | 買方向賣方揭露的所有資訊 (包括但不限於價格、規格和行銷計畫) 均視為機密資訊,屬於買方的專屬資訊。 | 這可能會妨礙賣方運用一般產業知識或與其他客戶合作。 |
不明確的準據法 | 本協議受該地法律規範,例如伊利諾州法律。 | 如果買方和賣方位於不同管轄區,可能會導致法律衝突。 |
不可抗力限制 | 賣方不得因任何不可抗力事件 (包括天災、戰爭或恐怖主義) 而免除履行本協議的責任。 | 這可能會讓賣家為無法控制的事件負責。 |
智慧財產權不明確 | 產品的所有智慧財產權應歸買方所有。 | 如果賣家來自受制裁國家/地區或與該國家/地區有聯繫,這可能會是警訊。 |
設計的提示範例
本節將說明使用 RTF 架構設計的提示範例。提示工程中的 RTF 架構代表角色、任務和格式。這是有效的提示結構化方式,可讓 Gemini 等大型語言模型提供更優質且一致的結果。
- 角色:您希望 LLM 採用的角色,例如「銷售專家」或「法律顧問」。
- 工作:LLM 的特定動作,例如「摘要這段文字」或「分析某些內容」。
- 格式:所需的輸出結構,例如清單或 JSON。
如要瞭解可用來影響模型回應的常見提示策略,請參閱「提示策略簡介」一文。
以下提示可做為定義規則,協助 Gemini 找出風險轉移條款等問題:
RTF 架構 |
---|
Role You are a legal advisor for my company. Task Analyze the below delivery instruction on a purchase order from my customer and let me know if it could cause any issues to my company. Format Respond to me in plain text JSON format with 4 fields. Field 1 should be "issue" with possible values Yes or No. Field 2 should be "issueCategory" with possible values Indemnification Overreach, Unilateral Termination Clause, Confidentiality Overextension, Unreasonable Warranty, Ambiguous Governing Law, Unclear Dispute Resolution, Force Majeure Limitation, Unbalanced Liability, Intellectual Property Ambiguity, Compliance with Sanctions, Others or None. Field 3 should be "explanation" - use this field to give a short explanation for your response on Field 1. Field 4 should be "recommendation" - use this field to give a short recommendation of how to mitigate such issues. Do not include backticks in the response. Delivery Instruction: "Seller warrants that the products will be free from defects for a period of ten years from the date of delivery." { "issue": "Yes", "issueCategory": "Unreasonable Warranty", "explanation": "A ten-year warranty is significantly longer than industry standard for most products. This could expose your company to substantial financial risk if issues arise with the products years down the line.", "recommendation": "Negotiate a shorter warranty period that aligns with industry standards and your product's expected lifespan. Offer extended warranty options at an additional cost if the customer requires longer coverage." } |
您可以將這個提示訊息儲存在 Cloud Storage 等存放區中。SAP 交易可將輸入資料傳遞至 BRE 模組。
接著,您可以使用 ABAP 適用的 Google Cloud SDK,從 Cloud Storage 擷取提示,並透過 ABAP 適用的 Vertex AI SDK 將提示傳遞給 Gemini 執行。
這是 Gemini 為這個風險轉移子句範例產生的 JSON 回應範例。
以下程式碼範例說明如何擷取規則,並在程式碼中使用這些規則。假設您將 BRE 服務實作為 ABAP 類別,例如 lcl_bre
。
DATA(lo_bre) = NEW lcl_bre( iv_key_name = 'CLIENT_KEY'
iv_model_key = 'MODEL_KEY' ).
" Sample delivery text
DATA(lv_delivery_text) = |Seller warrants that the products will be free from defects for| &&
|a period of ten years from the date of delivery.|.
" Execute the rule for identifying Risk-Shifting clauses.
DATA(lv_response) = lo_bre->execute_rule( iv_rule_id = 'RiskShiftingClause-Rule-001.prompt'
iv_data = lv_delivery_text ).
" JSON response
cl_demo_output=>display_json( lv_response ).
更改下列內容:
CLIENT_KEY
:用於驗證 Vertex AI 和 Cloud Storage 的用戶端金鑰。MODEL_KEY
:用於存取 LLM 的模型鍵名稱,可在模型產生參數中設定。
execute_rule
方法會執行下列操作:
使用
/GOOG/CL_STORAGE_V1
類別,從 Cloud Storage 讀取規則檔案。使用
/GOOG/CL_GENERATIVE_MODEL
類別,將處理規則傳送至 Gemini,並將其設為模型的指示。然後模型會產生 JSON 回應。
以下是完整的程式碼範例供您參考:
REPORT zra_bre_example.
" Local class definition
CLASS lcl_bre DEFINITION FINAL.
PUBLIC SECTION.
METHODS constructor
IMPORTING iv_key_name TYPE /goog/keyname
iv_model_key TYPE /goog/model_key
RAISING /goog/cx_sdk.
METHODS convert_xstring_to_string
IMPORTING iv_xstring TYPE xstring
RETURNING VALUE(iv_string) TYPE string.
METHODS execute_rule
IMPORTING iv_rule_id TYPE string
iv_data TYPE string
RETURNING VALUE(rv_response_text) TYPE string
RAISING /goog/cx_sdk.
PRIVATE SECTION.
DATA mo_storage TYPE REF TO /goog/cl_storage_v1.
DATA mo_model TYPE REF TO /goog/cl_generative_model.
ENDCLASS.
" Local class implementation
CLASS lcl_bre IMPLEMENTATION.
METHOD constructor.
mo_storage = NEW /goog/cl_storage_v1( iv_key_name = iv_key_name ).
mo_model = NEW /goog/cl_generative_model( iv_model_key = iv_model_key ).
ENDMETHOD.
METHOD convert_xstring_to_string.
DATA lv_file_length TYPE i.
DATA lt_bin_data TYPE STANDARD TABLE OF char1024.
" Call function module to convert xstring to binary format
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING buffer = iv_xstring
IMPORTING output_length = lv_file_length
TABLES binary_tab = lt_bin_data.
IF sy-subrc <> 0.
" Handle error
ENDIF.
" Call function module to convert from binary to string format
CALL FUNCTION 'SCMS_BINARY_TO_STRING'
EXPORTING input_length = lv_file_length
IMPORTING text_buffer = iv_string
TABLES binary_tab = lt_bin_data.
IF sy-subrc <> 0.
" Handle error
ENDIF.
ENDMETHOD.
METHOD execute_rule.
DATA lv_xdata TYPE xstring.
DATA lv_instruction TYPE string.
" Get the rule file from rule repository(cloud storage)
mo_storage->add_common_qparam( iv_name = 'alt'
iv_value = 'media' ).
mo_storage->get_objects( EXPORTING iv_p_bucket = 'gemini-bre-repo'
iv_p_object = iv_rule_id
IMPORTING es_output = DATA(ls_output)
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp)
es_raw = lv_xdata ).
IF mo_storage->is_success( lv_ret_code ) = abap_true.
" Set the instruction for Gemini as per the configured rule
lv_instruction = convert_xstring_to_string( iv_xstring = lv_xdata ).
ELSE.
" Handle error
ENDIF.
" Set the text to be analyzed
DATA(lv_prompt) = |Please analyze the Delivery Instruction:{ cl_abap_char_utilities=>newline }{ iv_data }|.
" Call Gemini to process the rule and identify Risk-Shifting clauses
rv_response_text = mo_model->set_system_instructions( iv_text = lv_instruction
)->add_safety_settings( iv_harm_category = 'HARM_CATEGORY_DANGEROUS_CONTENT'
iv_harm_block_threshold = 'BLOCK_NONE'
)->generate_content( iv_prompt_text = lv_prompt
)->get_text( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TRY.
DATA(lo_bre) = NEW lcl_bre( iv_key_name = 'DEMO_AIPLATFORM'
iv_model_key = 'gemini-flash' ).
" Sample delivery text
DATA(lv_delivery_text) = |Seller warrants that the products will be free from defects for| &&
|a period of ten years from the date of delivery.|.
" Execute the rule for identifying Risk-Shifting clauses.
DATA(lv_response) = lo_bre->execute_rule( iv_rule_id = 'RiskShiftingClause-Rule-001.prompt'
iv_data = lv_delivery_text ).
" JSON response
cl_demo_output=>display_json( lv_response ).
CATCH /goog/cx_sdk INTO DATA(lo_exception).
DATA(lv_msg) = lo_exception->get_text( ).
" Handle error
ENDTRY.
設計須知
本節提供指引,協助您使用這個參考架構開發一或多個架構,以滿足安全性、隱私權、法規遵循、成本和效能方面的特定需求。
安全性、隱私權和法規遵循
安全性和法規遵循是共同的責任。詳情請參閱「Vertex AI 共享責任」。
如要瞭解 Gemini 如何使用您的資料,請參閱「Gemini for Google Cloud 如何使用您的資料」。
費用
如要預估 Cloud Storage 等 Google Cloud 資源的費用,請使用Google Cloud Pricing Calculator。
如需瞭解 Vertex AI 和 Gemini API 相關的定價資訊,請參閱「Vertex AI 定價」。
設計替代方案
雖然本文件著重於 ABAP SDK for Google Cloud 的內部部署或任何雲端版本,但您也可以使用 ABAP SDK for Google Cloud 的 SAP BTP 版本,取得類似的結果。Vertex AI SDK for ABAP 不適用於 SAP BTP 環境,但您可以使用本文件提供的 SDK 內部版本程式碼範例,進行調整,並在 SAP BTP 環境中建構類似的解決方案。
後續步驟
- 如要瞭解 ABAP 適用的 Vertex AI SDK,請參閱「ABAP 適用的 Vertex AI SDK 總覽」。
- 如要瞭解 ABAP SDK for Google Cloud,請參閱「ABAP SDK for Google Cloud 簡介」。
如需協助解決 ABAP SDK for Google Cloud 的問題,請按照下列步驟操作:
- 請參閱 ABAP SDK for Google Cloud 疑難排解指南。
- 在 Cloud 論壇上向社群提問,並討論 ABAP 適用的 Google Cloud SDK。
- 收集所有可用的診斷資訊,然後聯絡 Cloud 客戶服務團隊。如要瞭解如何與 Customer Care 聯絡,請參閱「取得 SAP 支援 Google Cloud」一文。
貢獻者
作者:Ameya Suvarna | SAP 應用程式工程師團隊主管
其他貢獻者: Vikash Kumar | 技術作家