View a markdown version of this page

使用適用於 SAP ABAP 的 SDK 的 Amazon Bedrock 執行時期範例 - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用適用於 SAP ABAP 的 SDK 的 Amazon Bedrock 執行時期範例

下列程式碼範例示範如何使用適用於 SAP ABAP 的 AWS SDK 搭配 Amazon Bedrock 執行期來執行動作和實作常見案例。

每個範例均包含完整原始碼的連結,您可在連結中找到如何設定和執行內容中程式碼的相關指示。

Stable Image Core

下列程式碼範例示範如何在 Amazon Bedrock 上叫用 Stability.ai 穩定映像核心來產生映像。

適用於 SAP ABAP 的開發套件
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

使用 Stable Diffusion 建立映像。

"Stable Image Core Input Parameters should be in a format like this: * { * "prompt": "Draw a dolphin with a mustache, photorealistic", * "aspect_ratio": "1:1", * "seed": 0, * "output_format": "png" * } DATA: BEGIN OF ls_input, prompt TYPE /aws1/rt_shape_string, aspect_ratio TYPE /aws1/rt_shape_string, seed TYPE /aws1/rt_shape_integer, output_format TYPE /aws1/rt_shape_string, END OF ls_input. ls_input-prompt = iv_prompt. ls_input-aspect_ratio = '1:1'. ls_input-seed = 0. "or better, choose a random integer. ls_input-output_format = 'png'. DATA(lv_json) = /ui2/cl_json=>serialize( data = ls_input pretty_name = /ui2/cl_json=>pretty_mode-low_case ). TRY. DATA(lo_response) = lo_bdr->invokemodel( iv_body = /aws1/cl_rt_util=>string_to_xstring( lv_json ) iv_modelid = 'stability.stable-image-core-v1:1' iv_accept = 'application/json' iv_contenttype = 'application/json' ). "Stable Image Core Result Format: * { * "seeds": ["0"], * "finish_reasons": [null], * "images": ["iVBORw0KGgoAAAANSUhEUgAAAgAAA...."] * } DATA: BEGIN OF ls_response, images TYPE STANDARD TABLE OF /aws1/rt_shape_string, END OF ls_response. /ui2/cl_json=>deserialize( EXPORTING jsonx = lo_response->get_body( ) pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ). IF ls_response-images IS NOT INITIAL. DATA(lv_image) = cl_http_utility=>if_http_utility~decode_x_base64( ls_response-images[ 1 ] ). ENDIF. CATCH /aws1/cx_bdraccessdeniedex INTO DATA(lo_ex). WRITE / lo_ex->get_text( ). WRITE / |Don't forget to enable model access at https://console.aws.amazon.com/bedrock/home?#/modelaccess|. ENDTRY.

叫用 Stability.ai 穩定映像核心基礎模型,以使用 L2 高階用戶端產生映像。

TRY. DATA(lo_bdr_l2_sd) = /aws1/cl_bdr_l2_factory=>create_stable_diffusion_xl_1( lo_bdr ). " iv_prompt contains a prompt like 'Show me a picture of a unicorn reading an enterprise financial report'. DATA(lv_image) = lo_bdr_l2_sd->text_to_image( iv_prompt ). CATCH /aws1/cx_bdraccessdeniedex INTO DATA(lo_ex). WRITE / lo_ex->get_text( ). WRITE / |Don't forget to enable model access at https://console.aws.amazon.com/bedrock/home?#/modelaccess|. ENDTRY.
  • 如需 API 詳細資訊,請參閱《適用於 SAP ABAP 的AWS SDK API 參考》中的 InvokeModel