Skip to content

Commit 3668817

Browse files
authored
Python:Hello Bedrock example for Amazon Bedrock (#7207)
1 parent 1e9ab4b commit 3668817

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

.doc_gen/metadata/bedrock_metadata.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ bedrock_Hello:
2929
- description:
3030
snippet_files:
3131
- javascriptv3/example_code/bedrock/hello.js
32+
Python:
33+
versions:
34+
- sdk_version: 3
35+
github: python/example_code/bedrock
36+
sdkguide:
37+
excerpts:
38+
- description:
39+
snippet_tags:
40+
- bedrock.example_code.hello_bedrock.complete
3241
services:
3342
bedrock: {ListFoundationModels}
3443

python/example_code/bedrock/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ python -m pip install -r requirements.txt
3535
> ⚠ 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).
3636
<!--custom.prerequisites.end-->
3737
38+
### Get started
39+
40+
- [Hello Amazon Bedrock](hello_bedrock.py#L5) (`ListFoundationModels`)
41+
42+
3843
### Single actions
3944

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

73+
#### Hello Amazon Bedrock
74+
75+
This example shows you how to get started using Amazon Bedrock.
76+
77+
```
78+
python hello_bedrock.py
79+
```
6880

6981

7082
### Tests
@@ -93,4 +105,4 @@ in the `python` folder.
93105

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

96-
SPDX-License-Identifier: Apache-2.0
108+
SPDX-License-Identifier: Apache-2.0
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
5+
# snippet-start:[bedrock.example_code.hello_bedrock.complete]
6+
7+
"""
8+
Lists the available Amazon Bedrock models.
9+
"""
10+
import logging
11+
import json
12+
import boto3
13+
14+
15+
from botocore.exceptions import ClientError
16+
17+
18+
logging.basicConfig(level=logging.INFO)
19+
logger = logging.getLogger(__name__)
20+
21+
22+
def list_foundation_models(bedrock_client):
23+
"""
24+
Gets a list of available Amazon Bedrock foundation models.
25+
26+
:return: The list of available bedrock foundation models.
27+
"""
28+
29+
try:
30+
response = bedrock_client.list_foundation_models()
31+
models = response["modelSummaries"]
32+
logger.info("Got %s foundation models.", len(models))
33+
return models
34+
35+
except ClientError:
36+
logger.error("Couldn't list foundation models.")
37+
raise
38+
39+
40+
def main():
41+
"""Entry point for the example. Uses the AWS SDK for Python (Boto3)
42+
to create an Amazon Bedrock client. Then lists the available Bedrock models
43+
in the region set in the callers profile and credentials.
44+
"""
45+
46+
bedrock_client = boto3.client(service_name="bedrock")
47+
48+
fm_models = list_foundation_models(bedrock_client)
49+
for model in fm_models:
50+
print(f"Model: {model['modelName']}")
51+
print(json.dumps(model, indent=2))
52+
print("---------------------------\n")
53+
54+
logger.info("Done.")
55+
56+
57+
if __name__ == "__main__":
58+
main()
59+
60+
# snippet-end:[bedrock.example_code.hello_bedrock.complete]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import pytest
5+
import subprocess
6+
import sys
7+
8+
files_under_test = [
9+
# Text models
10+
"hello_bedrock.py"
11+
]
12+
13+
14+
@pytest.mark.integ
15+
@pytest.mark.parametrize("file", files_under_test)
16+
def test_hello_bedrock(file):
17+
result = subprocess.run(
18+
[sys.executable, file],
19+
capture_output=True,
20+
text=True,
21+
)
22+
assert result.stdout != ""
23+
assert result.returncode == 0

0 commit comments

Comments
 (0)