/AWS1/IF_BDR=>CONVERSESTREAM()¶
About ConverseStream¶
Sends messages to the specified Amazon Bedrock model and returns the response in a stream. ConverseStream provides a consistent API that works with all Amazon Bedrock models that support messages. This allows you to write code once and use it with different models. Should a model have unique inference parameters, you can also pass those unique parameters to the model.
To find out 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 ConverseStream.
Amazon Bedrock doesn't store any text, images, or documents that you provide as content. The data is only used to generate the response.
You can submit a prompt by including it in the messages field, specifying the modelId of a foundation model or inference profile to run inference on it, and including any other fields that are relevant to your use case.
You can also submit a prompt from Prompt management by specifying the ARN of the prompt version and including a map of variables to values in the promptVariables field. You can append more messages to the prompt by using the messages field. If you use a prompt from Prompt management, you can't include the following fields in the request: additionalModelRequestFields, inferenceConfig, system, or toolConfig. Instead, these fields must be defined through Prompt management. For more information, see Use a prompt from Prompt management.
For information about the Converse API, see Use the Converse API in the Amazon Bedrock User Guide. To use a guardrail, see Use a guardrail with the Converse API in the Amazon Bedrock User Guide. To use a tool with a model, see Tool use (Function calling) in the Amazon Bedrock User Guide
For example code, see Conversation streaming example in the Amazon Bedrock User Guide.
This operation requires permission for 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 base inference actions (InvokeModel and InvokeModelWithResponseStream). For more information see Deny access for inference on specific models.
For troubleshooting some of the common errors you might encounter when using the ConverseStream API, see Troubleshooting Amazon Bedrock API Error Codes in the Amazon Bedrock User Guide
Method Signature¶
METHODS /AWS1/IF_BDR~CONVERSESTREAM
IMPORTING
!IV_MODELID TYPE /AWS1/BDRCONVERSATIONALMODELID OPTIONAL
!IT_MESSAGES TYPE /AWS1/CL_BDRMESSAGE=>TT_MESSAGES OPTIONAL
!IT_SYSTEM TYPE /AWS1/CL_BDRSYSTEMCONTENTBLOCK=>TT_SYSTEMCONTENTBLOCKS OPTIONAL
!IO_INFERENCECONFIG TYPE REF TO /AWS1/CL_BDRINFERENCECONF OPTIONAL
!IO_TOOLCONFIG TYPE REF TO /AWS1/CL_BDRTOOLCONFIGURATION OPTIONAL
!IO_GUARDRAILCONFIG TYPE REF TO /AWS1/CL_BDRGUARDRAILSTRMCONF OPTIONAL
!IO_ADDLMODELREQUESTFIELDS TYPE REF TO /AWS1/CL_RT_DOCUMENT OPTIONAL
!IT_PROMPTVARIABLES TYPE /AWS1/CL_BDRPROMPTVARIABLEVALS=>TT_PROMPTVARIABLEMAP OPTIONAL
!IT_ADDLMODELRSPFIELDPATHS TYPE /AWS1/CL_BDRADDLMDELRSPFIELD00=>TT_ADDLMODELRESPONSEFIELDPATHS OPTIONAL
!IT_REQUESTMETADATA TYPE /AWS1/CL_BDRREQUESTMETADATA_W=>TT_REQUESTMETADATA OPTIONAL
!IO_PERFORMANCECONFIG TYPE REF TO /AWS1/CL_BDRPERFORMANCECONF OPTIONAL
!IO_SERVICETIER TYPE REF TO /AWS1/CL_BDRSERVICETIER OPTIONAL
RETURNING
VALUE(OO_OUTPUT) TYPE REF TO /aws1/cl_bdrconversestreamrsp
RAISING
/AWS1/CX_BDRACCESSDENIEDEX
/AWS1/CX_BDRINTERNALSERVEREX
/AWS1/CX_BDRMODELERROREX
/AWS1/CX_BDRMODELNOTREADYEX
/AWS1/CX_BDRMODELTIMEOUTEX
/AWS1/CX_BDRRESOURCENOTFOUNDEX
/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/BDRCONVERSATIONALMODELID /AWS1/BDRCONVERSATIONALMODELID¶
Specifies the model or throughput with which to run inference, or the prompt resource to use in inference. The value depends on the resource 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, first purchase Provisioned Throughput for it. Then specify the ARN of the resulting provisioned model. For more information, see Use a custom model in Amazon Bedrock in the Amazon Bedrock User Guide.
To include a prompt that was defined in Prompt management, specify the ARN of the prompt version to use.
The Converse API doesn't support imported models.
Optional arguments:¶
it_messages TYPE /AWS1/CL_BDRMESSAGE=>TT_MESSAGES TT_MESSAGES¶
The messages that you want to send to the model.
it_system TYPE /AWS1/CL_BDRSYSTEMCONTENTBLOCK=>TT_SYSTEMCONTENTBLOCKS TT_SYSTEMCONTENTBLOCKS¶
A prompt that provides instructions or context to the model about the task it should perform, or the persona it should adopt during the conversation.
io_inferenceconfig TYPE REF TO /AWS1/CL_BDRINFERENCECONF /AWS1/CL_BDRINFERENCECONF¶
Inference parameters to pass to the model.
ConverseandConverseStreamsupport a base set of inference parameters. If you need to pass additional parameters that the model supports, use theadditionalModelRequestFieldsrequest field.
io_toolconfig TYPE REF TO /AWS1/CL_BDRTOOLCONFIGURATION /AWS1/CL_BDRTOOLCONFIGURATION¶
Configuration information for the tools that the model can use when generating a response.
For information about models that support streaming tool use, see Supported models and model features.
io_guardrailconfig TYPE REF TO /AWS1/CL_BDRGUARDRAILSTRMCONF /AWS1/CL_BDRGUARDRAILSTRMCONF¶
Configuration information for a guardrail that you want to use in the request. If you include
guardContentblocks in thecontentfield in themessagesfield, the guardrail operates only on those messages. If you include noguardContentblocks, the guardrail operates on all messages in the request body and in any included prompt resource.
io_addlmodelrequestfields TYPE REF TO /AWS1/CL_RT_DOCUMENT /AWS1/CL_RT_DOCUMENT¶
Additional inference parameters that the model supports, beyond the base set of inference parameters that
ConverseandConverseStreamsupport in theinferenceConfigfield. For more information, see Model parameters.
it_promptvariables TYPE /AWS1/CL_BDRPROMPTVARIABLEVALS=>TT_PROMPTVARIABLEMAP TT_PROMPTVARIABLEMAP¶
Contains a map of variables in a prompt from Prompt management to objects containing the values to fill in for them when running model invocation. This field is ignored if you don't specify a prompt resource in the
modelIdfield.
it_addlmodelrspfieldpaths TYPE /AWS1/CL_BDRADDLMDELRSPFIELD00=>TT_ADDLMODELRESPONSEFIELDPATHS TT_ADDLMODELRESPONSEFIELDPATHS¶
Additional model parameters field paths to return in the response.
ConverseandConverseStreamreturn the requested fields as a JSON Pointer object in theadditionalModelResponseFieldsfield. The following is example JSON foradditionalModelResponseFieldPaths.
[ "/stop_sequence" ]For information about the JSON Pointer syntax, see the Internet Engineering Task Force (IETF) documentation.
ConverseandConverseStreamreject an empty JSON Pointer or incorrectly structured JSON Pointer with a400error code. if the JSON Pointer is valid, but the requested field is not in the model response, it is ignored byConverse.
it_requestmetadata TYPE /AWS1/CL_BDRREQUESTMETADATA_W=>TT_REQUESTMETADATA TT_REQUESTMETADATA¶
Key-value pairs that you can use to filter invocation logs.
io_performanceconfig TYPE REF TO /AWS1/CL_BDRPERFORMANCECONF /AWS1/CL_BDRPERFORMANCECONF¶
Model performance settings for the request.
io_servicetier TYPE REF TO /AWS1/CL_BDRSERVICETIER /AWS1/CL_BDRSERVICETIER¶
Specifies the processing tier configuration used for serving the request.
RETURNING¶
oo_output TYPE REF TO /aws1/cl_bdrconversestreamrsp /AWS1/CL_BDRCONVERSESTREAMRSP¶
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->conversestream(
io_addlmodelrequestfields = /AWS1/CL_RT_DOCUMENT=>FROM_JSON_STR( |\{"foo":"this is a JSON object..."\}| )
io_guardrailconfig = new /aws1/cl_bdrguardrailstrmconf(
iv_guardrailidentifier = |string|
iv_guardrailversion = |string|
iv_streamprocessingmode = |string|
iv_trace = |string|
)
io_inferenceconfig = new /aws1/cl_bdrinferenceconf(
it_stopsequences = VALUE /aws1/cl_bdrnonemptystrlist_w=>tt_nonemptystringlist(
( new /aws1/cl_bdrnonemptystrlist_w( |string| ) )
)
iv_maxtokens = 123
iv_temperature = '0.1'
iv_topp = '0.1'
)
io_performanceconfig = new /aws1/cl_bdrperformanceconf( |string| )
io_servicetier = new /aws1/cl_bdrservicetier( |string| )
io_toolconfig = new /aws1/cl_bdrtoolconfiguration(
io_toolchoice = new /aws1/cl_bdrtoolchoice(
io_any = new /aws1/cl_bdranytoolchoice( )
io_auto = new /aws1/cl_bdrautotoolchoice( )
io_tool = new /aws1/cl_bdrspecifictoolchoice( |string| )
)
it_tools = VALUE /aws1/cl_bdrtool=>tt_tools(
(
new /aws1/cl_bdrtool(
io_cachepoint = new /aws1/cl_bdrcachepointblock( |string| )
io_systemtool = new /aws1/cl_bdrsystemtool( |string| )
io_toolspec = new /aws1/cl_bdrtoolspecification(
io_inputschema = new /aws1/cl_bdrtoolinputschema(
io_json = /AWS1/CL_RT_DOCUMENT=>FROM_JSON_STR( |\{"foo":"this is a JSON object..."\}| )
)
iv_description = |string|
iv_name = |string|
)
)
)
)
)
it_addlmodelrspfieldpaths = VALUE /aws1/cl_bdraddlmdelrspfield00=>tt_addlmodelresponsefieldpaths(
( new /aws1/cl_bdraddlmdelrspfield00( |string| ) )
)
it_messages = VALUE /aws1/cl_bdrmessage=>tt_messages(
(
new /aws1/cl_bdrmessage(
it_content = VALUE /aws1/cl_bdrcontentblock=>tt_contentblocks(
(
new /aws1/cl_bdrcontentblock(
io_audio = new /aws1/cl_bdraudioblock(
io_error = new /aws1/cl_bdrerrorblock( |string| )
io_source = new /aws1/cl_bdraudiosource(
io_s3location = new /aws1/cl_bdrs3location(
iv_bucketowner = |string|
iv_uri = |string|
)
iv_bytes = '5347567362473873563239796247513D'
)
iv_format = |string|
)
io_cachepoint = new /aws1/cl_bdrcachepointblock( |string| )
io_citationscontent = new /aws1/cl_bdrcitationscontblock(
it_citations = VALUE /aws1/cl_bdrcitation=>tt_citations(
(
new /aws1/cl_bdrcitation(
io_location = new /aws1/cl_bdrcitationlocation(
io_documentchar = new /aws1/cl_bdrdoccharlocation(
iv_documentindex = 123
iv_end = 123
iv_start = 123
)
io_documentchunk = new /aws1/cl_bdrdocchunklocation(
iv_documentindex = 123
iv_end = 123
iv_start = 123
)
io_documentpage = new /aws1/cl_bdrdocpagelocation(
iv_documentindex = 123
iv_end = 123
iv_start = 123
)
io_searchresultlocation = new /aws1/cl_bdrsearchresultloc(
iv_end = 123
iv_searchresultindex = 123
iv_start = 123
)
io_web = new /aws1/cl_bdrweblocation(
iv_domain = |string|
iv_url = |string|
)
)
it_sourcecontent = VALUE /aws1/cl_bdrcitationsourcecont=>tt_citationsourcecontentlist(
( new /aws1/cl_bdrcitationsourcecont( |string| ) )
)
iv_source = |string|
iv_title = |string|
)
)
)
it_content = VALUE /aws1/cl_bdrcitationgenr8dcont=>tt_citationgenr8dcontentlist(
( new /aws1/cl_bdrcitationgenr8dcont( |string| ) )
)
)
io_document = new /aws1/cl_bdrdocumentblock(
io_citations = new /aws1/cl_bdrcitationsconfig( ABAP_TRUE )
io_source = new /aws1/cl_bdrdocumentsource(
io_s3location = new /aws1/cl_bdrs3location(
iv_bucketowner = |string|
iv_uri = |string|
)
it_content = VALUE /aws1/cl_bdrdoccontentblock=>tt_documentcontentblocks(
( new /aws1/cl_bdrdoccontentblock( |string| ) )
)
iv_bytes = '5347567362473873563239796247513D'
iv_text = |string|
)
iv_context = |string|
iv_format = |string|
iv_name = |string|
)
io_guardcontent = new /aws1/cl_bdrguardrailconvers00(
io_image = new /aws1/cl_bdrguardrailconvers03(
io_source = new /aws1/cl_bdrguardrailconvers04( '5347567362473873563239796247513D' )
iv_format = |string|
)
io_text = new /aws1/cl_bdrguardrailconvers01(
it_qualifiers = VALUE /aws1/cl_bdrguardrailconvers02=>tt_guardrailconversecontqual00(
( new /aws1/cl_bdrguardrailconvers02( |string| ) )
)
iv_text = |string|
)
)
io_image = new /aws1/cl_bdrimageblock(
io_error = new /aws1/cl_bdrerrorblock( |string| )
io_source = new /aws1/cl_bdrimagesource(
io_s3location = new /aws1/cl_bdrs3location(
iv_bucketowner = |string|
iv_uri = |string|
)
iv_bytes = '5347567362473873563239796247513D'
)
iv_format = |string|
)
io_reasoningcontent = new /aws1/cl_bdrreasoningcontblock(
io_reasoningtext = new /aws1/cl_bdrreasoningtextblock(
iv_signature = |string|
iv_text = |string|
)
iv_redactedcontent = '5347567362473873563239796247513D'
)
io_searchresult = new /aws1/cl_bdrsearchresultblock(
io_citations = new /aws1/cl_bdrcitationsconfig( ABAP_TRUE )
it_content = VALUE /aws1/cl_bdrsrchrsltcontblock=>tt_searchresultcontentblocks(
( new /aws1/cl_bdrsrchrsltcontblock( |string| ) )
)
iv_source = |string|
iv_title = |string|
)
io_toolresult = new /aws1/cl_bdrtoolresultblock(
it_content = VALUE /aws1/cl_bdrtoolrsltcontblock=>tt_toolresultcontentblocks(
(
new /aws1/cl_bdrtoolrsltcontblock(
io_document = new /aws1/cl_bdrdocumentblock(
io_citations = new /aws1/cl_bdrcitationsconfig( ABAP_TRUE )
io_source = new /aws1/cl_bdrdocumentsource(
io_s3location = new /aws1/cl_bdrs3location(
iv_bucketowner = |string|
iv_uri = |string|
)
it_content = VALUE /aws1/cl_bdrdoccontentblock=>tt_documentcontentblocks(
( new /aws1/cl_bdrdoccontentblock( |string| ) )
)
iv_bytes = '5347567362473873563239796247513D'
iv_text = |string|
)
iv_context = |string|
iv_format = |string|
iv_name = |string|
)
io_image = new /aws1/cl_bdrimageblock(
io_error = new /aws1/cl_bdrerrorblock( |string| )
io_source = new /aws1/cl_bdrimagesource(
io_s3location = new /aws1/cl_bdrs3location(
iv_bucketowner = |string|
iv_uri = |string|
)
iv_bytes = '5347567362473873563239796247513D'
)
iv_format = |string|
)
io_json = /AWS1/CL_RT_DOCUMENT=>FROM_JSON_STR( |\{"foo":"this is a JSON object..."\}| )
io_searchresult = new /aws1/cl_bdrsearchresultblock(
io_citations = new /aws1/cl_bdrcitationsconfig( ABAP_TRUE )
it_content = VALUE /aws1/cl_bdrsrchrsltcontblock=>tt_searchresultcontentblocks(
( new /aws1/cl_bdrsrchrsltcontblock( |string| ) )
)
iv_source = |string|
iv_title = |string|
)
io_video = new /aws1/cl_bdrvideoblock(
io_source = new /aws1/cl_bdrvideosource(
io_s3location = new /aws1/cl_bdrs3location(
iv_bucketowner = |string|
iv_uri = |string|
)
iv_bytes = '5347567362473873563239796247513D'
)
iv_format = |string|
)
iv_text = |string|
)
)
)
iv_status = |string|
iv_tooluseid = |string|
iv_type = |string|
)
io_tooluse = new /aws1/cl_bdrtooluseblock(
io_input = /AWS1/CL_RT_DOCUMENT=>FROM_JSON_STR( |\{"foo":"this is a JSON object..."\}| )
iv_name = |string|
iv_tooluseid = |string|
iv_type = |string|
)
io_video = new /aws1/cl_bdrvideoblock(
io_source = new /aws1/cl_bdrvideosource(
io_s3location = new /aws1/cl_bdrs3location(
iv_bucketowner = |string|
iv_uri = |string|
)
iv_bytes = '5347567362473873563239796247513D'
)
iv_format = |string|
)
iv_text = |string|
)
)
)
iv_role = |string|
)
)
)
it_promptvariables = VALUE /aws1/cl_bdrpromptvariablevals=>tt_promptvariablemap(
(
VALUE /aws1/cl_bdrpromptvariablevals=>ts_promptvariablemap_maprow(
key = |string|
value = new /aws1/cl_bdrpromptvariablevals( |string| )
)
)
)
it_requestmetadata = VALUE /aws1/cl_bdrrequestmetadata_w=>tt_requestmetadata(
(
VALUE /aws1/cl_bdrrequestmetadata_w=>ts_requestmetadata_maprow(
value = new /aws1/cl_bdrrequestmetadata_w( |string| )
key = |string|
)
)
)
it_system = VALUE /aws1/cl_bdrsystemcontentblock=>tt_systemcontentblocks(
(
new /aws1/cl_bdrsystemcontentblock(
io_cachepoint = new /aws1/cl_bdrcachepointblock( |string| )
io_guardcontent = new /aws1/cl_bdrguardrailconvers00(
io_image = new /aws1/cl_bdrguardrailconvers03(
io_source = new /aws1/cl_bdrguardrailconvers04( '5347567362473873563239796247513D' )
iv_format = |string|
)
io_text = new /aws1/cl_bdrguardrailconvers01(
it_qualifiers = VALUE /aws1/cl_bdrguardrailconvers02=>tt_guardrailconversecontqual00(
( new /aws1/cl_bdrguardrailconvers02( |string| ) )
)
iv_text = |string|
)
)
iv_text = |string|
)
)
)
iv_modelid = |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_stream( )->/aws1/if_rt_stream_reader~data_available( ) = ABAP_TRUE.
lo_event = lo_result->get_stream( )->READ( ).
IF lo_event->get_messagestart( ) IS NOT INITIAL.
" process this kind of event
ELSEIF lo_event->get_contentblockstart( ) IS NOT INITIAL.
" process this kind of event
ELSEIF lo_event->get_contentblockdelta( ) IS NOT INITIAL.
" process this kind of event
ELSEIF lo_event->get_contentblockstop( ) IS NOT INITIAL.
" process this kind of event
ELSEIF lo_event->get_messagestop( ) IS NOT INITIAL.
" process this kind of event
ELSEIF lo_event->get_metadata( ) 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_bdrthrottlingex.
" handle error in stream
CATCH /aws1/cx_bdrserviceunavailex.
" handle error in stream
CATCH /aws1/cx_bdrmodelstreamerrorex.
" handle error in stream
ENDTRY.
ENDIF.