Weitere AWS-SDK-Beispiele sind im GitHub-Repository Beispiele für AWS Doc SDKs
Beispiele für Amazon Bedrock Runtime unter Verwendung von SDK für SAP ABAP
Die folgenden Codebeispiele zeigen, wie Sie Aktionen durchführen und gängige Szenarien implementieren, indem Sie das AWS-SDK für SAP ABAP mit Amazon Bedrock Runtime nutzen.
Jedes Beispiel enthält einen Link zum vollständigen Quellcode, wo Sie Anweisungen zum Einrichten und Ausführen des Codes im Kodex finden.
Anthropic Claude
Das folgende Codebeispiel zeigt, wie mit der Invoke-Model-API eine Textnachricht an Anthropic Claude gesendet wird.
- SDK für SAP ABAP
-
Anmerkung
Auf GitHub finden Sie noch mehr. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Codebeispiel-Repository
einrichten und ausführen. Rufen Sie das Basismodell von Anthropic Claude 2 auf, um Text zu generieren. In diesem Beispiel werden Funktionen von /US2/CL_JSON verwendet, die in einigen NetWeaver-Versionen möglicherweise nicht verfügbar sind.
"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.Rufen Sie das Basismodell von Anthropic Claude 2 auf, um Text mit dem L2-High-Level-Client zu generieren.
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.Rufen Sie das Basismodell von Anthropic Claude 3 auf, um Text mit dem L2-High-Level-Client zu generieren.
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.-
Weitere API-Informationen finden Sie unter InvokeModel in der API-Referenz für das AWS-SDK für SAP ABAP.
-
Stabile Diffusion
Das folgende Codebeispiel zeigt, wie Sie Stability.ai Stable Diffusion XL in Amazon Bedrock aufrufen, um ein Bild zu generieren.
- SDK für SAP ABAP
-
Anmerkung
Auf GitHub finden Sie noch mehr. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Codebeispiel-Repository
einrichten und ausführen. Erstellen Sie ein Bild mit Stable Diffusion.
"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.Rufen Sie das Basismodell von Stability.ai Stable Diffusion XL auf, um Bilder mit dem L2-High-Level-Client zu generieren.
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.-
Weitere API-Informationen finden Sie unter InvokeModel in der API-Referenz für das AWS-SDK für SAP ABAP.
-