invoke_model
async
¶
invoke_model(
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,
) -> InvokeModelResponse
Invokes the specified Amazon Bedrock model to run inference using the prompt and inference parameters provided in the request body. You use model inference to generate text, images, and embeddings.
For example code, see Invoke model code examples in the Amazon Bedrock User Guide.
This operation requires permission for the bedrock:InvokeModel 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 InvokeModel 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 will be 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.
-
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
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 | |