converse_stream
async
¶
converse_stream(
model_id: ConversationalModelId,
*,
config_overrides: Optional[AsyncBedrockRuntimeClientConfig] = None,
messages: Optional[Messages] = None,
system: Optional[SystemContentBlocks] = None,
inference_config: Optional[InferenceConfiguration] = None,
tool_config: Optional[ToolConfiguration] = None,
guardrail_config: Optional[GuardrailStreamConfiguration] = None,
additional_model_request_fields: Optional[object] = None,
prompt_variables: Optional[PromptVariableMap] = None,
additional_model_response_field_paths: Optional[
AdditionalModelResponseFieldPaths
] = None,
request_metadata: Optional[RequestMetadata] = None,
performance_config: Optional[PerformanceConfiguration] = None,
service_tier: Optional[ServiceTier] = None,
output_config: Optional[OutputConfig] = None,
) -> AsyncGenerator[ConverseStreamResponse]
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
Parameters:
-
model_id(ConversationalModelId) –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.
-
-
messages(Optional[Messages], default:None) –The messages that you want to send to the model.
-
system(Optional[SystemContentBlocks], default:None) –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.
-
inference_config(Optional[InferenceConfiguration], default:None) –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. -
tool_config(Optional[ToolConfiguration], default:None) –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.
-
guardrail_config(Optional[GuardrailStreamConfiguration], default:None) –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. -
additional_model_request_fields(Optional[object], default:None) –Additional inference parameters that the model supports, beyond the base set of inference parameters that
ConverseandConverseStreamsupport in theinferenceConfigfield. For more information, see Model parameters. -
prompt_variables(Optional[PromptVariableMap], default:None) –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. -
additional_model_response_field_paths(Optional[AdditionalModelResponseFieldPaths], default:None) –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. -
request_metadata(Optional[RequestMetadata], default:None) –Key-value pairs that you can use to filter invocation logs.
-
performance_config(Optional[PerformanceConfiguration], default:None) –Model performance settings for the request.
-
service_tier(Optional[ServiceTier], default:None) –Specifies the processing tier configuration used for serving the request.
-
output_config(Optional[OutputConfig], default:None) –Output configuration for a model response.
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
-
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
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | |