-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Python:Hello Bedrock example for Amazon Bedrock #7207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24f40b5
work on python hello bedrock example
AWSChris 65ae500
fixed metadata
AWSChris 11462df
removed AWS reference
AWSChris 81e943e
reformatted string
AWSChris 7bbb1a7
updated readme
AWSChris f93e0b0
updated readme
AWSChris 2ba04d9
updated readme
AWSChris 9fb3e7d
updated readme
AWSChris 7944fe8
ran writeme script to update bedrock readme
AWSChris 17f5a18
removed region declaration
AWSChris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # 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. Change region to the region | ||
| that you want to use.""" | ||
|
|
||
| region = "us-east-1" | ||
|
|
||
| bedrock_client = boto3.client(service_name="bedrock", region_name=region) | ||
|
|
||
| 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] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.