Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAP ABAP: Add new code examples for Amazon Bedrock Runtime #5560

Merged
merged 9 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .doc_gen/cross-content/phrases-code-examples.ent
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@
<!ENTITY TSCM '&TSC; Medical'>

<!--Elastic Load Balancing-->
<!ENTITY ELBacro 'ELB'>
<!ENTITY ELBacro 'ELB'>

<!--Bedrock-->
<!ENTITY BRlong 'Amazon Bedrock'>
<!ENTITY BR 'Amazon Bedrock'>

<!--Bedrock Runtime-->
<!ENTITY BRRUNlong '&BRlong; Runtime'>
<!ENTITY BRRUN '&BR; Runtime'>
20 changes: 20 additions & 0 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# zexi 0.4.0
bedrock-runtime_InvokeModel:
title: Invoke the specified &BR; model to run inference
title_abbrev: Invoke a model
synopsis: invoke an &BR; model to run inference.
category:
languages:
SAP ABAP:
versions:
- sdk_version: 1
github: sap-abap/services/bdr
excerpts:
- description: Invoke a Claude V2 foundation model. This example uses features of /US2/CL_JSON which might not be available on some NetWeaver versions.
snippet_tags:
- bdr.abapv1.invokemodel_claude_v2
- description: Invoke a Stable Diffusion foundation model.
snippet_tags:
- bdr.abapv1.invokemodel_stable_diffusion
services:
bedrock-runtime: {InvokeModel}
15 changes: 15 additions & 0 deletions .doc_gen/metadata/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ batch:
tags:
product_categories: {'Compute'}
version: batch-2016-08-10
bedrock-runtime:
long: '&BRRUNlong;'
short: '&BRRUN;'
sort: Bedrock Runtime
expanded:
long: Amazon Bedrock Runtime
short: Amazon Bedrock Runtime
blurb: is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon.
guide:
subtitle: User Guide
url: bedrock/latest/userguide/what-is-bedrock.html
api_ref: bedrock/latest/APIReference/welcome.html
tags:
product_categories: {'Machine Learning &amp; AI'}
version: bedrock-runtime-2023-09-30
cloudformation:
long: '&CFNlong;'
short: '&CFN;'
Expand Down
10 changes: 10 additions & 0 deletions sap-abap/services/bdr/package.devc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DEVC" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<DEVC>
<CTEXT>Package for Amazon Bedrock Runtime</CTEXT>
</DEVC>
</asx:values>
</asx:abap>
</abapGit>
192 changes: 192 additions & 0 deletions sap-abap/services/bdr/zcl_aws1_bdr_actions.clas.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
class ZCL_AWS1_BDR_ACTIONS definition
public
final
create public .

public section.
protected section.
private section.

methods PROMPT_CLAUDE_V2
importing
!IV_PROMPT type STRING
returning
value(OV_ANSWER) type STRING
raising
/AWS1/CX_BDRSERVEREXC
/AWS1/CX_BDRCLIENTEXC
/AWS1/CX_RT_TECHNICAL_GENERIC
/AWS1/CX_RT_SERVICE_GENERIC
/AWS1/CX_RT_NO_AUTH_GENERIC .
methods PROMPT_STABLE_DIFFUSION
importing
!IV_PROMPT type STRING
returning
value(OV_IMAGE) type XSTRING
raising
/AWS1/CX_BDRSERVEREXC
/AWS1/CX_BDRCLIENTEXC
/AWS1/CX_RT_TECHNICAL_GENERIC
/AWS1/CX_RT_SERVICE_GENERIC
/AWS1/CX_RT_NO_AUTH_GENERIC .
ENDCLASS.



CLASS ZCL_AWS1_BDR_ACTIONS IMPLEMENTATION.


METHOD prompt_claude_v2.
CONSTANTS: cv_pfl TYPE /aws1/rt_profile_id VALUE 'ZCODE_DEMO'.

DATA(lo_session) = /aws1/cl_rt_session_aws=>create( cv_pfl ).
DATA(lo_bdr) = /aws1/cl_bdr_factory=>create( lo_session ).
"snippet-start:[bdr.abapv1.invokemodel_claude_v2]
"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(
EXPORTING 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://us-west-2.console.aws.amazon.com/bedrock/home?#/modelaccess|.
"Catch other exceptions as desired...
ENDTRY.
"snippet-end:[bdr.abapv1.invokemodel_claude_v2]
ov_answer = lv_answer.
ENDMETHOD.


METHOD prompt_stable_diffusion.
CONSTANTS: cv_pfl TYPE /aws1/rt_profile_id VALUE 'ZCODE_DEMO'.

DATA(lo_session) = /aws1/cl_rt_session_aws=>create( cv_pfl ).
DATA(lo_bdr) = /aws1/cl_bdr_factory=>create( lo_session ).
"snippet-start:[bdr.abapv1.invokemodel_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(
EXPORTING 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-v0'
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://us-west-2.console.aws.amazon.com/bedrock/home?#/modelaccess|.
"Catch other exceptions as desired...
ENDTRY.

"snippet-end:[bdr.abapv1.invokemodel_stable_diffusion]
ov_image = lv_image.
ENDMETHOD.
ENDCLASS.
46 changes: 46 additions & 0 deletions sap-abap/services/bdr/zcl_aws1_bdr_actions.clas.testclasses.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
" """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" " Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights
" " Reserved.
" " SPDX-License-Identifier: MIT-0
" """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

CLASS ltc_zcl_aws1_bdr_actions DEFINITION DEFERRED.
CLASS zcl_aws1_bdr_actions DEFINITION LOCAL FRIENDS ltc_zcl_aws1_bdr_actions.

CLASS ltc_zcl_aws1_bdr_actions DEFINITION FOR TESTING DURATION LONG RISK LEVEL HARMLESS.

PRIVATE SECTION.
CONSTANTS: cv_pfl TYPE /aws1/rt_profile_id VALUE 'ZCODE_DEMO'.

DATA ao_bdr TYPE REF TO /aws1/if_bdr.
DATA ao_session TYPE REF TO /aws1/cl_rt_session_base.
DATA ao_bdr_actions TYPE REF TO zcl_aws1_bdr_actions.

METHODS: test_claude_v2 FOR TESTING RAISING /aws1/cx_rt_generic.
METHODS: test_stable_diffusion FOR TESTING RAISING /aws1/cx_rt_generic.

METHODS: setup RAISING /aws1/cx_rt_generic.

ENDCLASS.
CLASS ltc_zcl_aws1_bdr_actions IMPLEMENTATION.

METHOD setup.
ao_session = /aws1/cl_rt_session_aws=>create( iv_profile_id = cv_pfl ).
ao_bdr = /aws1/cl_bdr_factory=>create( ao_session ).
ao_bdr_actions = NEW zcl_aws1_bdr_actions( ).
ENDMETHOD.
METHOD test_claude_v2.
DATA(lv_joke) = ao_bdr_actions->prompt_claude_v2(
'Tell me a joke about ABAP programmers and Java programmers walking into a bar'
).
cl_abap_unit_assert=>assert_not_initial( act = lv_joke ).
ENDMETHOD.

METHOD test_stable_diffusion.
DATA(lv_joke) = ao_bdr_actions->prompt_stable_diffusion(
'Show me a picture of a kitten coding in ABAP on an SAP system'
).
cl_abap_unit_assert=>assert_not_initial( act = lv_joke ).
ENDMETHOD.

ENDCLASS.
Loading
Loading