使用 for 的 Amazon 基岩运行时示例 SDK SAP ABAP - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 for 的 Amazon 基岩运行时示例 SDK SAP ABAP

以下代码示例向您展示了如何在 Amazon Bedrock Runtime 中使用 for AWS SDK 来执行操作和实现常见场景。SAP ABAP

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

Anthropic Claude

以下代码示例显示了如何使用调用模型向 Anthropic Claude 发送短信。API

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

调用 Anthropic Claude 2 基础模型以生成文本。此示例使用JSON了某些 NetWeaver 版本中可能不可用的功能 of /US2/CL _。

"Claude V2 Input Parameters should be in a format like this: * { * "prompt":"\n\nHuman:\\nTell me a joke\n\nAssistant:\n", * "max_tokens_to_sample":2048, * "temperature":0.5, * "top_k":250, * "top_p":1.0, * "stop_sequences":[] * } DATA: BEGIN OF ls_input, prompt TYPE string, max_tokens_to_sample TYPE /aws1/rt_shape_integer, temperature TYPE /aws1/rt_shape_float, top_k TYPE /aws1/rt_shape_integer, top_p TYPE /aws1/rt_shape_float, stop_sequences TYPE /aws1/rt_stringtab, END OF ls_input. "Leave ls_input-stop_sequences empty. ls_input-prompt = |\n\nHuman:\\n{ iv_prompt }\n\nAssistant:\n|. ls_input-max_tokens_to_sample = 2048. ls_input-temperature = '0.5'. ls_input-top_k = 250. ls_input-top_p = 1. "Serialize into JSON with /ui2/cl_json -- this assumes SAP_UI is installed. 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 = 'anthropic.claude-v2' iv_accept = 'application/json' iv_contenttype = 'application/json' ). "Claude V2 Response format will be: * { * "completion": "Knock Knock...", * "stop_reason": "stop_sequence" * } DATA: BEGIN OF ls_response, completion TYPE string, stop_reason TYPE 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 ). DATA(lv_answer) = ls_response-completion. 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.

调用 Anthropic Claude 2 基础模型,使用 L2 高级客户端生成文本。

TRY. DATA(lo_bdr_l2_claude) = /aws1/cl_bdr_l2_factory=>create_claude_2( lo_bdr ). " iv_prompt can contain a prompt like 'tell me a joke about Java programmers'. DATA(lv_answer) = lo_bdr_l2_claude->prompt_for_text( 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.

调用 Anthropic Claude 3 基础模型,使用 L2 高级客户端生成文本。

TRY. " Choose a model ID from Anthropic that supports the Messages API - currently this is " Claude v2, Claude v3 and v3.5. For the list of model ID, see: " https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html " for the list of models that support the Messages API see: " https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html DATA(lo_bdr_l2_claude) = /aws1/cl_bdr_l2_factory=>create_anthropic_msg_api( io_bdr = lo_bdr iv_model_id = 'anthropic.claude-3-sonnet-20240229-v1:0' ). " choosing Claude v3 Sonnet " iv_prompt can contain a prompt like 'tell me a joke about Java programmers'. DATA(lv_answer) = lo_bdr_l2_claude->prompt_for_text( iv_prompt = iv_prompt iv_max_tokens = 100 ). 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详细信息,请参阅InvokeModel中的AWS SDK以供SAPABAPAPI参考

Stable Diffusion

以下代码示例展示了如何在 Amazon Bedrock 上调用 Stability.ai Stable Diffusion XL 来生成图像。

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

使用 “稳定扩散” 创建图像。

"Stable Diffusion Input Parameters should be in a format like this: * { * "text_prompts": [ * {"text":"Draw a dolphin with a mustache"}, * {"text":"Make it photorealistic"} * ], * "cfg_scale":10, * "seed":0, * "steps":50 * } TYPES: BEGIN OF prompt_ts, text TYPE /aws1/rt_shape_string, END OF prompt_ts. DATA: BEGIN OF ls_input, text_prompts TYPE STANDARD TABLE OF prompt_ts, cfg_scale TYPE /aws1/rt_shape_integer, seed TYPE /aws1/rt_shape_integer, steps TYPE /aws1/rt_shape_integer, END OF ls_input. APPEND VALUE prompt_ts( text = iv_prompt ) TO ls_input-text_prompts. ls_input-cfg_scale = 10. ls_input-seed = 0. "or better, choose a random integer. ls_input-steps = 50. 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-diffusion-xl-v1' iv_accept = 'application/json' iv_contenttype = 'application/json' ). "Stable Diffusion Result Format: * { * "result": "success", * "artifacts": [ * { * "seed": 0, * "base64": "iVBORw0KGgoAAAANSUhEUgAAAgAAA.... * "finishReason": "SUCCESS" * } * ] * } TYPES: BEGIN OF artifact_ts, seed TYPE /aws1/rt_shape_integer, base64 TYPE /aws1/rt_shape_string, finishreason TYPE /aws1/rt_shape_string, END OF artifact_ts. DATA: BEGIN OF ls_response, result TYPE /aws1/rt_shape_string, artifacts TYPE STANDARD TABLE OF artifact_ts, 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-artifacts IS NOT INITIAL. DATA(lv_image) = cl_http_utility=>if_http_utility~decode_x_base64( ls_response-artifacts[ 1 ]-base64 ). 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 Stable Diffusion XL 基础模型,使用 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详细信息,请参阅InvokeModel中的AWS SDK以供SAPABAPAPI参考