diff --git a/.doc_gen/metadata/bedrock_metadata.yaml b/.doc_gen/metadata/bedrock_metadata.yaml index 519feeefb62..851bd1ee152 100644 --- a/.doc_gen/metadata/bedrock_metadata.yaml +++ b/.doc_gen/metadata/bedrock_metadata.yaml @@ -29,6 +29,15 @@ bedrock_Hello: - description: snippet_files: - javascriptv3/example_code/bedrock/hello.js + Python: + versions: + - sdk_version: 3 + github: python/example_code/bedrock + sdkguide: + excerpts: + - description: + snippet_tags: + - bedrock.example_code.hello_bedrock.complete services: bedrock: {ListFoundationModels} diff --git a/python/example_code/bedrock/README.md b/python/example_code/bedrock/README.md index 48303e29c17..5734b77682e 100644 --- a/python/example_code/bedrock/README.md +++ b/python/example_code/bedrock/README.md @@ -35,6 +35,11 @@ python -m pip install -r requirements.txt > ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). +### Get started + +- [Hello Amazon Bedrock](hello_bedrock.py#L5) (`ListFoundationModels`) + + ### Single actions Code excerpts that show you how to call individual service functions. @@ -65,6 +70,13 @@ python bedrock_wrapper.py ``` +#### Hello Amazon Bedrock + +This example shows you how to get started using Amazon Bedrock. + +``` +python hello_bedrock.py +``` ### Tests @@ -93,4 +105,4 @@ in the `python` folder. Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -SPDX-License-Identifier: Apache-2.0 \ No newline at end of file +SPDX-License-Identifier: Apache-2.0 diff --git a/python/example_code/bedrock/hello_bedrock.py b/python/example_code/bedrock/hello_bedrock.py new file mode 100644 index 00000000000..839cb572b63 --- /dev/null +++ b/python/example_code/bedrock/hello_bedrock.py @@ -0,0 +1,60 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + + +# snippet-start:[bedrock.example_code.hello_bedrock.complete] + +""" +Lists the available Amazon Bedrock models. +""" +import logging +import json +import boto3 + + +from botocore.exceptions import ClientError + + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + +def list_foundation_models(bedrock_client): + """ + Gets a list of available Amazon Bedrock foundation models. + + :return: The list of available bedrock foundation models. + """ + + try: + response = bedrock_client.list_foundation_models() + models = response["modelSummaries"] + logger.info("Got %s foundation models.", len(models)) + return models + + except ClientError: + logger.error("Couldn't list foundation models.") + raise + + +def main(): + """Entry point for the example. Uses the AWS SDK for Python (Boto3) + to create an Amazon Bedrock client. Then lists the available Bedrock models + in the region set in the callers profile and credentials. + """ + + bedrock_client = boto3.client(service_name="bedrock") + + fm_models = list_foundation_models(bedrock_client) + for model in fm_models: + print(f"Model: {model['modelName']}") + print(json.dumps(model, indent=2)) + print("---------------------------\n") + + logger.info("Done.") + + +if __name__ == "__main__": + main() + + # snippet-end:[bedrock.example_code.hello_bedrock.complete] diff --git a/python/example_code/bedrock/test/test_hello_bedrock.py b/python/example_code/bedrock/test/test_hello_bedrock.py new file mode 100644 index 00000000000..9a3742568bb --- /dev/null +++ b/python/example_code/bedrock/test/test_hello_bedrock.py @@ -0,0 +1,23 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +import subprocess +import sys + +files_under_test = [ + # Text models + "hello_bedrock.py" +] + + +@pytest.mark.integ +@pytest.mark.parametrize("file", files_under_test) +def test_hello_bedrock(file): + result = subprocess.run( + [sys.executable, file], + capture_output=True, + text=True, + ) + assert result.stdout != "" + assert result.returncode == 0