AWS bedrock documentation change
Summary
Removed Python SDK code examples for InvokeModel and Converse operations, including error handling and runtime client usage
Security assessment
The removed content was example code for API usage without any security-specific configurations or warnings. No evidence of vulnerability fixes or security enhancements in the diff. Changes appear to be routine documentation cleanup/streamlining.
Diff
diff --git a/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_Hello_section.md b/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_Hello_section.md index 750c6e5b3..af71d7e58 100644 --- a//bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_Hello_section.md +++ b//bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_Hello_section.md @@ -218,181 +217,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru -Python - - -**SDK for Python (Boto3)** - - -###### Note - -There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/bedrock-runtime#code-examples). - -Send a prompt to a model with the InvokeModel operation. - - - """ - Uses the Amazon Bedrock runtime client InvokeModel operation to send a prompt to a model. - """ - import logging - import json - import boto3 - - - from botocore.exceptions import ClientError - - - logging.basicConfig(level=logging.INFO) - logger = logging.getLogger(__name__) - - - def invoke_model(brt, model_id, prompt): - """ - Invokes the specified model with the supplied prompt. - param brt: A bedrock runtime boto3 client - param model_id: The model ID for the model that you want to use. - param prompt: The prompt that you want to send to the model. - - :return: The text response from the model. - """ - - # Format the request payload using the model's native structure. - native_request = { - "inputText": prompt, - "textGenerationConfig": { - "maxTokenCount": 512, - "temperature": 0.5, - "topP": 0.9 - } - } - - # Convert the native request to JSON. - request = json.dumps(native_request) - - try: - # Invoke the model with the request. - response = brt.invoke_model(modelId=model_id, body=request) - - # Decode the response body. - model_response = json.loads(response["body"].read()) - - # Extract and print the response text. - response_text = model_response["results"][0]["outputText"] - return response_text - - except (ClientError, Exception) as e: - print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") - raise - - - def main(): - """Entry point for the example. Uses the AWS SDK for Python (Boto3) - to create an Amazon Bedrock runtime client. Then sends a prompt to a model - in the region set in the callers profile and credentials. - """ - - # Create an Amazon Bedrock Runtime client. - brt = boto3.client("bedrock-runtime") - - # Set the model ID, e.g., Amazon Titan Text G1 - Express. - model_id = "amazon.titan-text-express-v1" - - # Define the prompt for the model. - prompt = "Describe the purpose of a 'hello world' program in one line." - - # Send the prompt to the model. - response = invoke_model(brt, model_id, prompt) - - print(f"Response: {response}") - - logger.info("Done.") - - - if __name__ == "__main__": - main() - - - - -Send a user message to a model with the Converse operation. - - - """ - Uses the Amazon Bedrock runtime client Converse operation to send a user message to a model. - """ - import logging - import boto3 - - from botocore.exceptions import ClientError - - - logging.basicConfig(level=logging.INFO) - logger = logging.getLogger(__name__) - - - def converse(brt, model_id, user_message): - """ - Uses the Converse operation to send a user message to the supplied model. - param brt: A bedrock runtime boto3 client - param model_id: The model ID for the model that you want to use. - param user message: The user message that you want to send to the model. - - :return: The text response from the model. - """ - - # Format the request payload using the model's native structure. - conversation = [ - { - "role": "user", - "content": [{"text": user_message}], - } - ] - - try: - # Send the message to the model, using a basic inference configuration. - response = brt.converse( - modelId=model_id, - messages=conversation, - inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, - ) - - # Extract and print the response text. - response_text = response["output"]["message"]["content"][0]["text"] - return response_text - - except (ClientError, Exception) as e: - print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") - raise - - - def main(): - """Entry point for the example. Uses the AWS SDK for Python (Boto3) - to create an Amazon Bedrock runtime client. Then sends a user message to a model - in the region set in the callers profile and credentials. - """ - - # Create an Amazon Bedrock Runtime client. - brt = boto3.client("bedrock-runtime") - - # Set the model ID, e.g., Amazon Titan Text G1 - Express. - model_id = "amazon.titan-text-express-v1" - - # Define the message for the model. - message = "Describe the purpose of a 'hello world' program in one line." - - # Send the message to the model. - response = converse(brt, model_id, message) - - print(f"Response: {response}") - - logger.info("Done.") - - - if __name__ == "__main__": - main() - - - - - * For API details, see [InvokeModel](https://docs.aws.amazon.com/goto/boto3/bedrock-runtime-2023-09-30/InvokeModel) in _AWS SDK for Python (Boto3) API Reference_. - - - -