Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .doc_gen/metadata/bedrock_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
14 changes: 13 additions & 1 deletion python/example_code/bedrock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
<!--custom.prerequisites.end-->

### Get started

- [Hello Amazon Bedrock](hello_bedrock.py#L5) (`ListFoundationModels`)


### Single actions

Code excerpts that show you how to call individual service functions.
Expand Down Expand Up @@ -65,6 +70,13 @@ python bedrock_wrapper.py
```
<!--custom.instructions.end-->

#### Hello Amazon Bedrock

This example shows you how to get started using Amazon Bedrock.

```
python hello_bedrock.py
```


### Tests
Expand Down Expand Up @@ -93,4 +105,4 @@ in the `python` folder.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
60 changes: 60 additions & 0 deletions python/example_code/bedrock/hello_bedrock.py
Original file line number Diff line number Diff line change
@@ -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]
23 changes: 23 additions & 0 deletions python/example_code/bedrock/test/test_hello_bedrock.py
Original file line number Diff line number Diff line change
@@ -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
Loading