invoke_model_with_response_stream
async
¶
invoke_model_with_response_stream(
model_id: InvokeModelIdentifier,
*,
config_overrides: Optional[AsyncBedrockRuntimeClientConfig] = None,
body: Optional[Body] = None,
content_type: Optional[MimeType] = None,
accept: Optional[MimeType] = None,
trace: Optional[Trace] = None,
guardrail_identifier: Optional[GuardrailIdentifier] = None,
guardrail_version: Optional[GuardrailVersion] = None,
performance_config_latency: Optional[PerformanceConfigLatency] = None,
service_tier: Optional[ServiceTierType] = None,
request_metadata: Optional[RequestMetadataJson] = None,
) -> AsyncGenerator[InvokeModelWithResponseStreamResponse]
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
Parameters:
-
body(Optional[Body], default:None) –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. -
content_type(Optional[MimeType], default:None) –The MIME type of the input data in the request. You must specify
application/json. -
accept(Optional[MimeType], default:None) –The desired MIME type of the inference body in the response. The default value is
application/json. -
model_id(InvokeModelIdentifier) –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.
-
-
trace(Optional[Trace], default:None) –Specifies whether to enable or disable the Bedrock trace. If enabled, you can see the full Bedrock trace.
-
guardrail_identifier(Optional[GuardrailIdentifier], default:None) –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.
-
-
guardrail_version(Optional[GuardrailVersion], default:None) –The version number for the guardrail. The value can also be
DRAFT. -
performance_config_latency(Optional[PerformanceConfigLatency], default:None) –Model performance settings for the request.
-
service_tier(Optional[ServiceTierType], default:None) –Specifies the processing tier type used for serving the request.
-
request_metadata(Optional[RequestMetadataJson], default:None) –Key-value pairs that you can use to filter invocation logs.
Raises:
-
AccessDeniedException–The request is denied because you do not have sufficient permissions to perform the requested action. For troubleshooting this error, see AccessDeniedException in the Amazon Bedrock User Guide
-
InternalServerException–An internal server error occurred. For troubleshooting this error, see InternalFailure in the Amazon Bedrock User Guide
-
ModelErrorException–The request failed due to an error while processing the model.
-
ModelNotReadyException–The model specified in the request is not ready to serve inference requests. The AWS SDK will automatically retry the operation up to 5 times. For information about configuring automatic retries, see Retry behavior in the AWS SDKs and Tools reference guide.
-
ModelStreamErrorException–An error occurred while streaming the response. Retry your request.
-
ModelTimeoutException–The request took too long to process. Processing time exceeded the model timeout length.
-
ResourceNotFoundException–The specified resource ARN was not found. For troubleshooting this error, see ResourceNotFound in the Amazon Bedrock User Guide
-
ServiceQuotaExceededException–Your request exceeds the service quota for your account. You can view your quotas at Viewing service quotas. You can resubmit your request later.
-
ServiceUnavailableException–The service isn't currently available. For troubleshooting this error, see ServiceUnavailable in the Amazon Bedrock User Guide
-
ThrottlingException–Your request was denied due to exceeding the account quotas for Amazon Bedrock. For troubleshooting this error, see ThrottlingException in the Amazon Bedrock User Guide
-
ValidationException–The input fails to satisfy the constraints specified by Amazon Bedrock. For troubleshooting this error, see ValidationError in the Amazon Bedrock User Guide
-
UnknownServiceError–The service returned an error code this client does not model.
Source code in docs_src/capo_bedrock_runtime/_services/async_bedrock_runtime.py
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 | |