AWS code-library documentation change
Summary
Removed Python code examples demonstrating InvokeModel and Converse operations for Amazon Bedrock runtime
Security assessment
The removed content was example code for model interaction without security-specific context. No security vulnerabilities or mitigations are mentioned. The change appears to be routine documentation cleanup rather than security-related.
Diff
diff --git a/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Hello_section.md b/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Hello_section.md index 89ac2c452..8a12e57c5 100644 --- a//code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Hello_section.md +++ b//code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Hello_section.md @@ -220,181 +219,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_. - - - -