Amazon Bedrock Runtime

2024/05/30 - Amazon Bedrock Runtime - 2 new api methods

Changes  This release adds Converse and ConverseStream APIs to Bedrock Runtime

ConverseStream (new) Link ¶

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. For more information, see Run inference in the Bedrock User Guide.

To find out if a model supports streaming, call GetFoundationModel and check the responseStreamingSupported field in the response.

For example code, see Invoke model with streaming code example in the Amazon Bedrock User Guide .

This operation requires permission for the bedrock:InvokeModelWithResponseStream action.

See also: AWS API Documentation

Request Syntax

client.converse_stream(
    modelId='string',
    messages=[
        {
            'role': 'user'|'assistant',
            'content': [
                {
                    'text': 'string',
                    'image': {
                        'format': 'png'|'jpeg'|'gif'|'webp',
                        'source': {
                            'bytes': b'bytes'
                        }
                    },
                    'toolUse': {
                        'toolUseId': 'string',
                        'name': 'string',
                        'input': {...}|[...]|123|123.4|'string'|True|None
                    },
                    'toolResult': {
                        'toolUseId': 'string',
                        'content': [
                            {
                                'json': {...}|[...]|123|123.4|'string'|True|None,
                                'text': 'string',
                                'image': {
                                    'format': 'png'|'jpeg'|'gif'|'webp',
                                    'source': {
                                        'bytes': b'bytes'
                                    }
                                }
                            },
                        ],
                        'status': 'success'|'error'
                    }
                },
            ]
        },
    ],
    system=[
        {
            'text': 'string'
        },
    ],
    inferenceConfig={
        'maxTokens': 123,
        'temperature': ...,
        'topP': ...,
        'stopSequences': [
            'string',
        ]
    },
    toolConfig={
        'tools': [
            {
                'toolSpec': {
                    'name': 'string',
                    'description': 'string',
                    'inputSchema': {
                        'json': {...}|[...]|123|123.4|'string'|True|None
                    }
                }
            },
        ],
        'toolChoice': {
            'auto': {}
            ,
            'any': {}
            ,
            'tool': {
                'name': 'string'
            }
        }
    },
    additionalModelRequestFields={...}|[...]|123|123.4|'string'|True|None,
    additionalModelResponseFieldPaths=[
        'string',
    ]
)
type modelId

string

param modelId

[REQUIRED]

The ID for the model.

The modelId to provide depends on the type of model that you use:

type messages

list

param messages

[REQUIRED]

The messages that you want to send to the model.

  • (dict) --

    A message in the Message field. Use to send a message in a call to Converse.

    • role (string) -- [REQUIRED]

      The role that the message plays in the message.

    • content (list) -- [REQUIRED]

      The message content.

      • (dict) --

        A block of content for a message.

        Note

        This is a Tagged Union structure. Only one of the following top level keys can be set: text, image, toolUse, toolResult.

        • text (string) --

          Text to include in the message.

        • image (dict) --

          Image to include in the message.

          Note

          This field is only supported by Anthropic Claude 3 models.

          • format (string) -- [REQUIRED]

            The format of the image.

          • source (dict) -- [REQUIRED]

            The source for the image.

            Note

            This is a Tagged Union structure. Only one of the following top level keys can be set: bytes.

            • bytes (bytes) --

              The raw image bytes for the image. If you use an AWS SDK, you don't need to base64 encode the image bytes.

        • toolUse (dict) --

          Information about a tool use request from a model.

          • toolUseId (string) -- [REQUIRED]

            The ID for the tool request.

          • name (string) -- [REQUIRED]

            The name of the tool that the model wants to use.

          • input (:ref:`document<document>`) -- [REQUIRED]

            The input to pass to the tool.

        • toolResult (dict) --

          The result for a tool request that a model makes.

          • toolUseId (string) -- [REQUIRED]

            The ID of the tool request that this is the result for.

          • content (list) -- [REQUIRED]

            The content for tool result content block.

            • (dict) --

              The tool result content block.

              Note

              This is a Tagged Union structure. Only one of the following top level keys can be set: json, text, image.

              • json (:ref:`document<document>`) --

                A tool result that is JSON format data.

              • text (string) --

                A tool result that is text.

              • image (dict) --

                A tool result that is an image.

                Note

                This field is only supported by Anthropic Claude 3 models.

                • format (string) -- [REQUIRED]

                  The format of the image.

                • source (dict) -- [REQUIRED]

                  The source for the image.

                  Note

                  This is a Tagged Union structure. Only one of the following top level keys can be set: bytes.

                  • bytes (bytes) --

                    The raw image bytes for the image. If you use an AWS SDK, you don't need to base64 encode the image bytes.

          • status (string) --

            The status for the tool result content block.

            Note

            This field is only supported Anthropic Claude 3 models.

type system

list

param system

A system prompt to send to the model.

  • (dict) --

    A system content block

    Note

    This is a Tagged Union structure. Only one of the following top level keys can be set: text.

    • text (string) --

      A system prompt for the model.

type inferenceConfig

dict

param inferenceConfig

Inference parameters to pass to the model. ConverseStream supports a base set of inference parameters. If you need to pass additional parameters that the model supports, use the additionalModelRequestFields request field.

type toolConfig

dict

param toolConfig

Configuration information for the tools that the model can use when generating a response.

Note

This field is only supported by Anthropic Claude 3 models.

  • tools (list) -- [REQUIRED]

    An array of tools that you want to pass to a model.

    • (dict) --

      Information about a tool that you can use with the Converse API.

      Note

      This is a Tagged Union structure. Only one of the following top level keys can be set: toolSpec.

      • toolSpec (dict) --

        The specfication for the tool.

        • name (string) -- [REQUIRED]

          The name for the tool.

        • description (string) --

          The description for the tool.

        • inputSchema (dict) -- [REQUIRED]

          The input schema for the tool in JSON format.

          Note

          This is a Tagged Union structure. Only one of the following top level keys can be set: json.

  • toolChoice (dict) --

    If supported by model, forces the model to request a tool.

    Note

    This is a Tagged Union structure. Only one of the following top level keys can be set: auto, any, tool.

    • auto (:class:`.EventStream`) --

      The Model automatically decides if a tool should be called or to whether to generate text instead.

    • any (:class:`.EventStream`) --

      The model must request at least one tool (no text is generated).

    • tool (dict) --

      The Model must request the specified tool.

      • name (string) -- [REQUIRED]

        The name of the tool that the model must request.

type additionalModelRequestFields

:ref:`document<document>`

param additionalModelRequestFields

Additional inference parameters that the model supports, beyond the base set of inference parameters that ConverseStream supports in the inferenceConfig field.

type additionalModelResponseFieldPaths

list

param additionalModelResponseFieldPaths

Additional model parameters field paths to return in the response. ConverseStream returns the requested fields as a JSON Pointer object in the additionalModelResultFields field. The following is example JSON for additionalModelResponseFieldPaths .

[ "/stop_sequence" ]

For information about the JSON Pointer syntax, see the Internet Engineering Task Force (IETF) documentation.

ConverseStream rejects an empty JSON Pointer or incorrectly structured JSON Pointer with a 400 error code. if the JSON Pointer is valid, but the requested field is not in the model response, it is ignored by ConverseStream .

  • (string) --

rtype

dict

returns

The response of this operation contains an :class:`.EventStream` member. When iterated the :class:`.EventStream` will yield events based on the structure below, where only one of the top level keys will be present for any given event.

Response Syntax

{
    'stream': EventStream({
        'messageStart': {
            'role': 'user'|'assistant'
        },
        'contentBlockStart': {
            'start': {
                'toolUse': {
                    'toolUseId': 'string',
                    'name': 'string'
                }
            },
            'contentBlockIndex': 123
        },
        'contentBlockDelta': {
            'delta': {
                'text': 'string',
                'toolUse': {
                    'input': 'string'
                }
            },
            'contentBlockIndex': 123
        },
        'contentBlockStop': {
            'contentBlockIndex': 123
        },
        'messageStop': {
            'stopReason': 'end_turn'|'tool_use'|'max_tokens'|'stop_sequence'|'content_filtered',
            'additionalModelResponseFields': {...}|[...]|123|123.4|'string'|True|None
        },
        'metadata': {
            'usage': {
                'inputTokens': 123,
                'outputTokens': 123,
                'totalTokens': 123
            },
            'metrics': {
                'latencyMs': 123
            }
        },
        'internalServerException': {
            'message': 'string'
        },
        'modelStreamErrorException': {
            'message': 'string',
            'originalStatusCode': 123,
            'originalMessage': 'string'
        },
        'validationException': {
            'message': 'string'
        },
        'throttlingException': {
            'message': 'string'
        }
    })
}

Response Structure

  • (dict) --

    • stream (:class:`.EventStream`) --

      The output stream that the model generated.

      • messageStart (dict) --

        Message start information.

        • role (string) --

          The role for the message.

      • contentBlockStart (dict) --

        Start information for a content block.

        • start (dict) --

          Start information about a content block start event.

          Note

          This is a Tagged Union structure. Only one of the following top level keys will be set: toolUse. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

          'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
          • toolUse (dict) --

            Information about a tool that the model is requesting to use.

            • toolUseId (string) --

              The ID for the tool request.

            • name (string) --

              The name of the tool that the model is requesting to use.

        • contentBlockIndex (integer) --

          The index for a content block start event.

      • contentBlockDelta (dict) --

        The messages output content block delta.

        • delta (dict) --

          The delta for a content block delta event.

          Note

          This is a Tagged Union structure. Only one of the following top level keys will be set: text, toolUse. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

          'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
          • text (string) --

            The content text.

          • toolUse (dict) --

            Information about a tool that the model is requesting to use.

            • input (string) --

              The input for a requested tool.

        • contentBlockIndex (integer) --

          The block index for a content block delta event.

      • contentBlockStop (dict) --

        Stop information for a content block.

        • contentBlockIndex (integer) --

          The index for a content block.

      • messageStop (dict) --

        Message stop information.

        • stopReason (string) --

          The reason why the model stopped generating output.

        • additionalModelResponseFields (:ref:`document<document>`) --

          The additional model response fields.

      • metadata (dict) --

        Metadata for the converse output stream.

        • usage (dict) --

          Usage information for the conversation stream event.

          • inputTokens (integer) --

            The number of tokens sent in the request to the model.

          • outputTokens (integer) --

            The number of tokens that the model generated for the request.

          • totalTokens (integer) --

            The total of input tokens and tokens generated by the model.

        • metrics (dict) --

          The metrics for the conversation stream metadata event.

          • latencyMs (integer) --

            The latency for the streaming request, in milliseconds.

      • internalServerException (dict) --

        An internal server error occurred. Retry your request.

        • message (string) --

      • modelStreamErrorException (dict) --

        A streaming error occurred. Retry your request.

        • message (string) --

        • originalStatusCode (integer) --

          The original status code.

        • originalMessage (string) --

          The original message.

      • validationException (dict) --

        Input validation failed. Check your request parameters and retry the request.

        • message (string) --

      • throttlingException (dict) --

        The number of requests exceeds the limit. Resubmit your request later.

        • message (string) --

Converse (new) Link ¶

Sends messages to the specified Amazon Bedrock model. Converse provides a consistent interface that works with all 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. For more information, see Run inference in the Bedrock User Guide.

This operation requires permission for the bedrock:InvokeModel action.

See also: AWS API Documentation

Request Syntax

client.converse(
    modelId='string',
    messages=[
        {
            'role': 'user'|'assistant',
            'content': [
                {
                    'text': 'string',
                    'image': {
                        'format': 'png'|'jpeg'|'gif'|'webp',
                        'source': {
                            'bytes': b'bytes'
                        }
                    },
                    'toolUse': {
                        'toolUseId': 'string',
                        'name': 'string',
                        'input': {...}|[...]|123|123.4|'string'|True|None
                    },
                    'toolResult': {
                        'toolUseId': 'string',
                        'content': [
                            {
                                'json': {...}|[...]|123|123.4|'string'|True|None,
                                'text': 'string',
                                'image': {
                                    'format': 'png'|'jpeg'|'gif'|'webp',
                                    'source': {
                                        'bytes': b'bytes'
                                    }
                                }
                            },
                        ],
                        'status': 'success'|'error'
                    }
                },
            ]
        },
    ],
    system=[
        {
            'text': 'string'
        },
    ],
    inferenceConfig={
        'maxTokens': 123,
        'temperature': ...,
        'topP': ...,
        'stopSequences': [
            'string',
        ]
    },
    toolConfig={
        'tools': [
            {
                'toolSpec': {
                    'name': 'string',
                    'description': 'string',
                    'inputSchema': {
                        'json': {...}|[...]|123|123.4|'string'|True|None
                    }
                }
            },
        ],
        'toolChoice': {
            'auto': {}
            ,
            'any': {}
            ,
            'tool': {
                'name': 'string'
            }
        }
    },
    additionalModelRequestFields={...}|[...]|123|123.4|'string'|True|None,
    additionalModelResponseFieldPaths=[
        'string',
    ]
)
type modelId

string

param modelId

[REQUIRED]

The identifier for the model that you want to call.

The modelId to provide depends on the type of model that you use:

type messages

list

param messages

[REQUIRED]

The messages that you want to send to the model.

  • (dict) --

    A message in the Message field. Use to send a message in a call to Converse.

    • role (string) -- [REQUIRED]

      The role that the message plays in the message.

    • content (list) -- [REQUIRED]

      The message content.

      • (dict) --

        A block of content for a message.

        Note

        This is a Tagged Union structure. Only one of the following top level keys can be set: text, image, toolUse, toolResult.

        • text (string) --

          Text to include in the message.

        • image (dict) --

          Image to include in the message.

          Note

          This field is only supported by Anthropic Claude 3 models.

          • format (string) -- [REQUIRED]

            The format of the image.

          • source (dict) -- [REQUIRED]

            The source for the image.

            Note

            This is a Tagged Union structure. Only one of the following top level keys can be set: bytes.

            • bytes (bytes) --

              The raw image bytes for the image. If you use an AWS SDK, you don't need to base64 encode the image bytes.

        • toolUse (dict) --

          Information about a tool use request from a model.

          • toolUseId (string) -- [REQUIRED]

            The ID for the tool request.

          • name (string) -- [REQUIRED]

            The name of the tool that the model wants to use.

          • input (:ref:`document<document>`) -- [REQUIRED]

            The input to pass to the tool.

        • toolResult (dict) --

          The result for a tool request that a model makes.

          • toolUseId (string) -- [REQUIRED]

            The ID of the tool request that this is the result for.

          • content (list) -- [REQUIRED]

            The content for tool result content block.

            • (dict) --

              The tool result content block.

              Note

              This is a Tagged Union structure. Only one of the following top level keys can be set: json, text, image.

              • json (:ref:`document<document>`) --

                A tool result that is JSON format data.

              • text (string) --

                A tool result that is text.

              • image (dict) --

                A tool result that is an image.

                Note

                This field is only supported by Anthropic Claude 3 models.

                • format (string) -- [REQUIRED]

                  The format of the image.

                • source (dict) -- [REQUIRED]

                  The source for the image.

                  Note

                  This is a Tagged Union structure. Only one of the following top level keys can be set: bytes.

                  • bytes (bytes) --

                    The raw image bytes for the image. If you use an AWS SDK, you don't need to base64 encode the image bytes.

          • status (string) --

            The status for the tool result content block.

            Note

            This field is only supported Anthropic Claude 3 models.

type system

list

param system

A system prompt to pass to the model.

  • (dict) --

    A system content block

    Note

    This is a Tagged Union structure. Only one of the following top level keys can be set: text.

    • text (string) --

      A system prompt for the model.

type inferenceConfig

dict

param inferenceConfig

Inference parameters to pass to the model. Converse supports a base set of inference parameters. If you need to pass additional parameters that the model supports, use the additionalModelRequestFields request field.

type toolConfig

dict

param toolConfig

Configuration information for the tools that the model can use when generating a response.

Note

This field is only supported by Anthropic Claude 3, Cohere Command R, Cohere Command R+, and Mistral Large models.

  • tools (list) -- [REQUIRED]

    An array of tools that you want to pass to a model.

    • (dict) --

      Information about a tool that you can use with the Converse API.

      Note

      This is a Tagged Union structure. Only one of the following top level keys can be set: toolSpec.

      • toolSpec (dict) --

        The specfication for the tool.

        • name (string) -- [REQUIRED]

          The name for the tool.

        • description (string) --

          The description for the tool.

        • inputSchema (dict) -- [REQUIRED]

          The input schema for the tool in JSON format.

          Note

          This is a Tagged Union structure. Only one of the following top level keys can be set: json.

  • toolChoice (dict) --

    If supported by model, forces the model to request a tool.

    Note

    This is a Tagged Union structure. Only one of the following top level keys can be set: auto, any, tool.

    • auto (dict) --

      The Model automatically decides if a tool should be called or to whether to generate text instead.

    • any (dict) --

      The model must request at least one tool (no text is generated).

    • tool (dict) --

      The Model must request the specified tool.

      • name (string) -- [REQUIRED]

        The name of the tool that the model must request.

type additionalModelRequestFields

:ref:`document<document>`

param additionalModelRequestFields

Additional inference parameters that the model supports, beyond the base set of inference parameters that Converse supports in the inferenceConfig field. For more information, see Model parameters.

type additionalModelResponseFieldPaths

list

param additionalModelResponseFieldPaths

Additional model parameters field paths to return in the response. Converse returns the requested fields as a JSON Pointer object in the additionalModelResultFields field. The following is example JSON for additionalModelResponseFieldPaths .

[ "/stop_sequence" ]

For information about the JSON Pointer syntax, see the Internet Engineering Task Force (IETF) documentation.

Converse rejects an empty JSON Pointer or incorrectly structured JSON Pointer with a 400 error code. if the JSON Pointer is valid, but the requested field is not in the model response, it is ignored by Converse .

  • (string) --

rtype

dict

returns

Response Syntax

{
    'output': {
        'message': {
            'role': 'user'|'assistant',
            'content': [
                {
                    'text': 'string',
                    'image': {
                        'format': 'png'|'jpeg'|'gif'|'webp',
                        'source': {
                            'bytes': b'bytes'
                        }
                    },
                    'toolUse': {
                        'toolUseId': 'string',
                        'name': 'string',
                        'input': {...}|[...]|123|123.4|'string'|True|None
                    },
                    'toolResult': {
                        'toolUseId': 'string',
                        'content': [
                            {
                                'json': {...}|[...]|123|123.4|'string'|True|None,
                                'text': 'string',
                                'image': {
                                    'format': 'png'|'jpeg'|'gif'|'webp',
                                    'source': {
                                        'bytes': b'bytes'
                                    }
                                }
                            },
                        ],
                        'status': 'success'|'error'
                    }
                },
            ]
        }
    },
    'stopReason': 'end_turn'|'tool_use'|'max_tokens'|'stop_sequence'|'content_filtered',
    'usage': {
        'inputTokens': 123,
        'outputTokens': 123,
        'totalTokens': 123
    },
    'metrics': {
        'latencyMs': 123
    },
    'additionalModelResponseFields': {...}|[...]|123|123.4|'string'|True|None
}

Response Structure

  • (dict) --

    • output (dict) --

      The result from the call to Converse .

      Note

      This is a Tagged Union structure. Only one of the following top level keys will be set: message. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

      'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
      • message (dict) --

        The message that the model generates.

        • role (string) --

          The role that the message plays in the message.

        • content (list) --

          The message content.

          • (dict) --

            A block of content for a message.

            Note

            This is a Tagged Union structure. Only one of the following top level keys will be set: text, image, toolUse, toolResult. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

            'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
            • text (string) --

              Text to include in the message.

            • image (dict) --

              Image to include in the message.

              Note

              This field is only supported by Anthropic Claude 3 models.

              • format (string) --

                The format of the image.

              • source (dict) --

                The source for the image.

                Note

                This is a Tagged Union structure. Only one of the following top level keys will be set: bytes. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

                'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
                • bytes (bytes) --

                  The raw image bytes for the image. If you use an AWS SDK, you don't need to base64 encode the image bytes.

            • toolUse (dict) --

              Information about a tool use request from a model.

              • toolUseId (string) --

                The ID for the tool request.

              • name (string) --

                The name of the tool that the model wants to use.

              • input (:ref:`document<document>`) --

                The input to pass to the tool.

            • toolResult (dict) --

              The result for a tool request that a model makes.

              • toolUseId (string) --

                The ID of the tool request that this is the result for.

              • content (list) --

                The content for tool result content block.

                • (dict) --

                  The tool result content block.

                  Note

                  This is a Tagged Union structure. Only one of the following top level keys will be set: json, text, image. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

                  'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
                  • json (:ref:`document<document>`) --

                    A tool result that is JSON format data.

                  • text (string) --

                    A tool result that is text.

                  • image (dict) --

                    A tool result that is an image.

                    Note

                    This field is only supported by Anthropic Claude 3 models.

                    • format (string) --

                      The format of the image.

                    • source (dict) --

                      The source for the image.

                      Note

                      This is a Tagged Union structure. Only one of the following top level keys will be set: bytes. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

                      'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
                      • bytes (bytes) --

                        The raw image bytes for the image. If you use an AWS SDK, you don't need to base64 encode the image bytes.

              • status (string) --

                The status for the tool result content block.

                Note

                This field is only supported Anthropic Claude 3 models.

    • stopReason (string) --

      The reason why the model stopped generating output.

    • usage (dict) --

      The total number of tokens used in the call to Converse . The total includes the tokens input to the model and the tokens generated by the model.

      • inputTokens (integer) --

        The number of tokens sent in the request to the model.

      • outputTokens (integer) --

        The number of tokens that the model generated for the request.

      • totalTokens (integer) --

        The total of input tokens and tokens generated by the model.

    • metrics (dict) --

      Metrics for the call to Converse .

      • latencyMs (integer) --

        The latency of the call to Converse , in milliseconds.

    • additionalModelResponseFields (:ref:`document<document>`) --

      Additional fields in the response that are unique to the model.