/AWS1/IF_BDR=>INVOKEMODELWITHRSPSTREAM()¶
About InvokeModelWithResponseStream¶
Invoke the specified Amazon Bedrock model to run inference using the prompt and inference parameters provided in the request body. The response is returned in a stream.
To see if a model supports streaming, call GetFoundationModel and check the responseStreamingSupported field in the response.
The CLI doesn't support streaming operations in Amazon Bedrock, including InvokeModelWithResponseStream.
For example code, see Invoke model with streaming code example in the Amazon Bedrock User Guide.
This operation requires permissions to perform the bedrock:InvokeModelWithResponseStream action.
To deny all inference access to resources that you specify in the modelId field, you need to deny access to the bedrock:InvokeModel and bedrock:InvokeModelWithResponseStream actions. Doing this also denies access to the resource through the Converse API actions (Converse and ConverseStream). For more information see Deny access for inference on specific models.
For troubleshooting some of the common errors you might encounter when using the InvokeModelWithResponseStream API, see Troubleshooting Amazon Bedrock API Error Codes in the Amazon Bedrock User Guide
Method Signature¶
METHODS /AWS1/IF_BDR~INVOKEMODELWITHRSPSTREAM
IMPORTING
!IV_BODY TYPE /AWS1/BDRBODY OPTIONAL
!IV_CONTENTTYPE TYPE /AWS1/BDRMIMETYPE OPTIONAL
!IV_ACCEPT TYPE /AWS1/BDRMIMETYPE OPTIONAL
!IV_MODELID TYPE /AWS1/BDRINVOKEMODELIDENTIFIER OPTIONAL
!IV_TRACE TYPE /AWS1/BDRTRACE OPTIONAL
!IV_GUARDRAILIDENTIFIER TYPE /AWS1/BDRGUARDRAILIDENTIFIER OPTIONAL
!IV_GUARDRAILVERSION TYPE /AWS1/BDRGUARDRAILVERSION OPTIONAL
!IV_PERFORMANCECONFIGLATENCY TYPE /AWS1/BDRPERFORMANCECFGLATENCY OPTIONAL
!IV_SERVICETIER TYPE /AWS1/BDRSERVICETIERTYPE OPTIONAL
RETURNING
VALUE(OO_OUTPUT) TYPE REF TO /aws1/cl_bdrinvmdelwrspstrmrsp
RAISING
/AWS1/CX_BDRACCESSDENIEDEX
/AWS1/CX_BDRINTERNALSERVEREX
/AWS1/CX_BDRMODELERROREX
/AWS1/CX_BDRMODELNOTREADYEX
/AWS1/CX_BDRMODELSTREAMERROREX
/AWS1/CX_BDRMODELTIMEOUTEX
/AWS1/CX_BDRRESOURCENOTFOUNDEX
/AWS1/CX_BDRSERVICEQUOTAEXCDEX
/AWS1/CX_BDRSERVICEUNAVAILEX
/AWS1/CX_BDRTHROTTLINGEX
/AWS1/CX_BDRVALIDATIONEX
/AWS1/CX_BDRCLIENTEXC
/AWS1/CX_BDRSERVEREXC
/AWS1/CX_RT_TECHNICAL_GENERIC
/AWS1/CX_RT_SERVICE_GENERIC.
IMPORTING¶
Required arguments:¶
iv_modelid TYPE /AWS1/BDRINVOKEMODELIDENTIFIER /AWS1/BDRINVOKEMODELIDENTIFIER¶
The unique identifier of the model to invoke to run inference.
The
modelIdto provide depends on the type of model or throughput that you use:
If you use a base model, specify the model ID or its ARN. For a list of model IDs for base models, see Amazon Bedrock base model IDs (on-demand throughput) in the Amazon Bedrock User Guide.
If you use an inference profile, specify the inference profile ID or its ARN. For a list of inference profile IDs, see Supported Regions and models for cross-region inference in the Amazon Bedrock User Guide.
If you use a provisioned model, specify the ARN of the Provisioned Throughput. For more information, see Run inference using a Provisioned Throughput in the Amazon Bedrock User Guide.
If you use a custom model, specify the ARN of the custom model deployment (for on-demand inference) or the ARN of your provisioned model (for Provisioned Throughput). For more information, see Use a custom model in Amazon Bedrock in the Amazon Bedrock User Guide.
If you use an imported model, specify the ARN of the imported model. You can get the model ARN from a successful call to CreateModelImportJob or from the Imported models page in the Amazon Bedrock console.
Optional arguments:¶
iv_body TYPE /AWS1/BDRBODY /AWS1/BDRBODY¶
The prompt and inference parameters in the format specified in the
contentTypein the header. You must provide the body in JSON format. To see the format and content of the request and response bodies for different models, refer to Inference parameters. For more information, see Run inference in the Bedrock User Guide.
iv_contenttype TYPE /AWS1/BDRMIMETYPE /AWS1/BDRMIMETYPE¶
The MIME type of the input data in the request. You must specify
application/json.
iv_accept TYPE /AWS1/BDRMIMETYPE /AWS1/BDRMIMETYPE¶
The desired MIME type of the inference body in the response. The default value is
application/json.
iv_trace TYPE /AWS1/BDRTRACE /AWS1/BDRTRACE¶
Specifies whether to enable or disable the Bedrock trace. If enabled, you can see the full Bedrock trace.
iv_guardrailidentifier TYPE /AWS1/BDRGUARDRAILIDENTIFIER /AWS1/BDRGUARDRAILIDENTIFIER¶
The unique identifier of the guardrail that you want to use. If you don't provide a value, no guardrail is applied to the invocation.
An error is thrown in the following situations.
You don't provide a guardrail identifier but you specify the
amazon-bedrock-guardrailConfigfield in the request body.You enable the guardrail but the
contentTypeisn'tapplication/json.You provide a guardrail identifier, but
guardrailVersionisn't specified.
iv_guardrailversion TYPE /AWS1/BDRGUARDRAILVERSION /AWS1/BDRGUARDRAILVERSION¶
The version number for the guardrail. The value can also be
DRAFT.
iv_performanceconfiglatency TYPE /AWS1/BDRPERFORMANCECFGLATENCY /AWS1/BDRPERFORMANCECFGLATENCY¶
Model performance settings for the request.
iv_servicetier TYPE /AWS1/BDRSERVICETIERTYPE /AWS1/BDRSERVICETIERTYPE¶
Specifies the processing tier type used for serving the request.
RETURNING¶
oo_output TYPE REF TO /aws1/cl_bdrinvmdelwrspstrmrsp /AWS1/CL_BDRINVMDELWRSPSTRMRSP¶
Domain /AWS1/RT_ACCOUNT_ID Primitive Type NUMC
Examples¶
Syntax Example¶
This is an example of the syntax for calling the method. It includes every possible argument and initializes every possible value. The data provided is not necessarily semantically accurate (for example the value "string" may be provided for something that is intended to be an instance ID, or in some cases two arguments may be mutually exclusive). The syntax shows the ABAP syntax for creating the various data structures.
DATA(lo_result) = lo_client->invokemodelwithrspstream(
iv_accept = |string|
iv_body = '5347567362473873563239796247513D'
iv_contenttype = |string|
iv_guardrailidentifier = |string|
iv_guardrailversion = |string|
iv_modelid = |string|
iv_performanceconfiglatency = |string|
iv_servicetier = |string|
iv_trace = |string|
).
This is an example of reading all possible response values
lo_result = lo_result.
IF lo_result IS NOT INITIAL.
TRY.
WHILE lo_result->get_body( )->/aws1/if_rt_stream_reader~data_available( ) = ABAP_TRUE.
lo_event = lo_result->get_body( )->READ( ).
IF lo_event->get_chunk( ) IS NOT INITIAL.
" process this kind of event
ENDIF.
ENDWHILE.
CATCH /aws1/cx_bdrinternalserverex.
" handle error in stream
CATCH /aws1/cx_bdrvalidationex.
" handle error in stream
CATCH /aws1/cx_bdrmodeltimeoutex.
" handle error in stream
CATCH /aws1/cx_bdrthrottlingex.
" handle error in stream
CATCH /aws1/cx_bdrserviceunavailex.
" handle error in stream
CATCH /aws1/cx_bdrmodelstreamerrorex.
" handle error in stream
ENDTRY.
lv_mimetype = lo_result->get_contenttype( ).
lv_performanceconfiglatenc = lo_result->get_performanceconfiglatency( ).
lv_servicetiertype = lo_result->get_servicetier( ).
ENDIF.