### Install Documentation Dependencies Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-sagemaker-runtime-http2/docs/README.md Run this command to install the necessary dependencies for building the documentation. ```bash make docs-install ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-lex-runtime-v2/docs/README.md Installs the required dependencies for generating documentation. ```bash make docs-install ``` -------------------------------- ### Install AWS SDK for Python with Specific Services Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-python/README.md Install the AWS SDK for Python with support for a select subset of services by specifying them in the installation command. ```bash pip install aws-sdk-python[bedrock-runtime,...] ``` -------------------------------- ### Install AWS SDK for Python with All Services Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-python/README.md Use this command to install the AWS SDK for Python, including support for all AWS services. ```bash pip install aws-sdk-python[all] ``` -------------------------------- ### Install Specific Version of Bedrock Runtime Client Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/installing.md Installs a precise version of the experimental Amazon Bedrock Runtime client for Python. This is recommended for maintaining compatibility as the client is experimental. ```bash # Install experimental Amazon Bedrock Runtime client for Python version 0.0.1 specifically python -m pip install aws-sdk-bedrock-runtime==0.0.1 ``` ```bash # Make sure the experimental Amazon Bedrock Runtime client for Python is no older than version 0.0.2 pip install aws-sdk-bedrock-runtime>=0.0.2 ``` ```bash # Avoid versions of the experimental Amazon Bedrock Runtime client for Python newer than version 0.0.3 pip install aws-sdk-bedrock-runtime<=0.0.3 ``` -------------------------------- ### Install Experimental Amazon Bedrock Runtime Client Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/installing.md Installs the latest version of the experimental Amazon Bedrock Runtime client for Python and its dependencies. This command should be run within an activated virtual environment. ```bash python -m pip install aws-sdk-bedrock-runtime ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/installing.md Sets up an isolated Python environment to avoid dependency conflicts. Ensure you have Python 3.12 or later installed. ```bash python -m venv .venv ... source .venv/bin/activate ``` -------------------------------- ### Run Polly Streaming Example with Inline Text Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-polly/examples/README.md Executes the stream_speech_to_file.py script with provided inline text. ```sh uv run stream_speech_to_file.py "Hello from Polly." ``` -------------------------------- ### Run Polly Streaming Example from Stdin Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-polly/examples/README.md Executes the stream_speech_to_file.py script, reading text input from stdin. ```sh cat story.txt | uv run stream_speech_to_file.py - ``` -------------------------------- ### Generate Client Documentation Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-polly/docs/README.md Commands to manage documentation generation for the AWS SDK Polly client. Includes installing dependencies, serving locally, building static HTML, and cleaning artifacts. ```bash # Install documentation dependencies make docs-install ``` ```bash # Serve documentation locally make docs-serve ``` ```bash # OR build static HTML documentation make docs ``` ```bash # Clean docs artifacts make docs-clean ``` -------------------------------- ### Run Polly Streaming Example with Default Text Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-polly/examples/README.md Executes the stream_speech_to_file.py script using default text input. ```sh uv run stream_speech_to_file.py ``` -------------------------------- ### Importing SDK Modules for Bedrock Runtime Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/simple-app.md Imports the necessary classes and types from the Bedrock Runtime client and related modules. Ensure these are installed in your environment. ```python from aws_sdk_bedrock_runtime.client import BedrockRuntimeClient, ConverseInput from aws_sdk_bedrock_runtime.models import Message, ContentBlockText, StopReason from aws_sdk_bedrock_runtime.config import Config from smithy_aws_core.credentials_resolvers.environment import EnvironmentCredentialsResolver import asyncio ``` -------------------------------- ### Constructing User Messages for Bedrock Runtime Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/simple-app.md Creates a list of Message objects, where each Message contains content blocks. This example formats a single user message with text content. ```python def create_messages(): message_1 = Message( role="user", content=[ ContentBlockText( value="Create a list of 3 of the best songs from the 1980s." ) ] ) return [message_1] ``` -------------------------------- ### Get AWS Region from Environment Variable for Bedrock Runtime Client Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/region.md Retrieve the AWS Region from the `AWS_REGION` environment variable, falling back to a default value if the variable is not set. This approach is useful for dynamic region configuration. ```python import os ... region = os.getenv('AWS_REGION', default="us-east-1") service_client = BedrockRuntimeClient( config=Config( aws_credentials_identity_resolver=EnvironmentCredentialsResolver(), region = region, ) ) ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-sagemaker-runtime-http2/docs/README.md Use this command to serve the documentation locally, allowing for live preview during development. ```bash make docs-serve ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-lex-runtime-v2/docs/README.md Serves the generated documentation locally for preview. ```bash make docs-serve ``` -------------------------------- ### Build Static HTML Documentation Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-sagemaker-runtime-http2/docs/README.md Execute this command to build the static HTML files for the client documentation. ```bash make docs ``` -------------------------------- ### Build Static HTML Documentation Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-connecthealth/docs/README.md Builds the static HTML files for the documentation. This is typically used for deploying the documentation to a web server. ```bash # OR build static HTML documentation make docs ``` -------------------------------- ### Creating an Amazon Bedrock Runtime Client Instance Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/simple-app.md Initializes the BedrockRuntimeClient with a specific AWS region and configures it to use environment variables for credentials. This client is used to interact with Bedrock services. ```python def get_service_client(): service_client = BedrockRuntimeClient( config=Config( aws_credentials_identity_resolver=EnvironmentCredentialsResolver(), region = "us-east-1", ) ) return service_client ``` -------------------------------- ### Run Asynchronous Application Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/simple-app.md An asynchronous function that initializes a service client, creates messages, sends them, and outputs the reply. Includes entry point for running the application. ```python async def app_run(): service_client = get_service_client() messages = create_messages() output = await send_messages(service_client, messages) if output: output_reply(output) if __name__ == "__main__": asyncio.run(app_run()) ``` -------------------------------- ### Clean Documentation Artifacts Source: https://github.com/awslabs/aws-sdk-python/blob/develop/clients/aws-sdk-sagemaker-runtime-http2/docs/README.md Run this command to remove generated documentation artifacts and clean up the build directory. ```bash make docs-clean ``` -------------------------------- ### Use EnvironmentCredentialsResolver with BedrockRuntimeClient Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/credential-providers.md Configure the BedrockRuntimeClient to use EnvironmentCredentialsResolver by setting the `aws_credentials_identity_resolver` property in the `Config` object. This resolver fetches credentials from environment variables. ```python service_client = BedrockRuntimeClient( config=Config( aws_credentials_identity_resolver=EnvironmentCredentialsResolver(), region = "us-east-1", ) ) ``` -------------------------------- ### Sending Messages and Handling Bedrock Runtime Response Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/simple-app.md Sends messages to the specified Bedrock model using the converse API and handles potential responses, including checking stop reasons like guardrail interventions or content filtering. This function is asynchronous. ```python async def send_messages(service_client, messages): try: response = await service_client.converse( ConverseInput( model_id='amazon.titan-text-express-v1', messages=messages ) ) except: print("An error occurred while conversing!") return # Get the `ConverseOutput` object from the `ConverseOperationOutput`. This # is a `ConverseOutputMessage` unless an unexpected response arrives. output = response.output # Check the reason why the operation ended. stop_reason = response.stop_reason match stop_reason: case StopReason.GUARDRAIL_INTERVENED: print("A guardrail intervened in the response.") case StopReason.CONTENT_FILTERED: print("The content was filtered.") case StopReason.MAX_TOKENS: print("You ran out of tokens.") case _: pass return output ``` -------------------------------- ### Output Reply from ConverseOutputMessage Source: https://github.com/awslabs/aws-sdk-python/blob/develop/dev-guide/simple-app.md Extracts and prints text content from a ConverseOutputMessage object. Handles ContentBlockText types and flags unexpected types. ```python def output_reply(output): # Get the `Message` from the `ConverseOutputMessage` object. output_msg = output.value # Get the content of the message from its `content` property. This is # an array of `ContentBlock` objects, each representing a piece of the # response. msg_contents = output_msg.content # Output the returned text by printing the value of each # `ContentBlockText` object found in the `msg_contents` array. for msg_block in msg_contents: if isinstance(msg_block, ContentBlockText): print(msg_block.value) else: print("Unexpected type of response.") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.