From fbc3302b18b5647c5b7afaebd054652732a5f590 Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Wed, 9 Apr 2025 17:51:58 +0000 Subject: [PATCH 1/5] Add example for Bedrock's ListFoundationModels --- .../ListFoundationModels/Package.swift | 40 +++++ .../ListFoundationModels/Sources/entry.swift | 145 ++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 swift/example_code/bedrock/ListFoundationModels/Package.swift create mode 100644 swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift diff --git a/swift/example_code/bedrock/ListFoundationModels/Package.swift b/swift/example_code/bedrock/ListFoundationModels/Package.swift new file mode 100644 index 00000000000..b12f406bcbb --- /dev/null +++ b/swift/example_code/bedrock/ListFoundationModels/Package.swift @@ -0,0 +1,40 @@ +// swift-tools-version: 5.9 +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// The swift-tools-version declares the minimum version of Swift required to +// build this package. + +import PackageDescription + +let package = Package( + name: "ListFoundationModels", + // Let Xcode know the minimum Apple platforms supported. + platforms: [ + .macOS(.v13), + .iOS(.v15) + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + .package( + url: "https://github.com/awslabs/aws-sdk-swift", + from: "1.0.0"), + .package( + url: "https://github.com/apple/swift-argument-parser.git", + branch: "main" + ) + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products + // from dependencies. + .executableTarget( + name: "ListFoundationModels", + dependencies: [ + .product(name: "AWSBedrock", package: "aws-sdk-swift"), + .product(name: "ArgumentParser", package: "swift-argument-parser") + ], + path: "Sources") + + ] +) diff --git a/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift new file mode 100644 index 00000000000..7573d53cbbe --- /dev/null +++ b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift @@ -0,0 +1,145 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// An example demonstrating how to fetch a list of foundation models available +// using Amazon Bedrock. + +import ArgumentParser +import AWSClientRuntime +import Foundation + +// snippet-start:[bedrock.swift.import] +import AWSBedrock +// snippet-end:[bedrock.swift.import] + +struct ExampleCommand: ParsableCommand { + static var configuration = CommandConfiguration( + commandName: "ListFoundationModels", + abstract: """ + This example demonstrates how to retrieve a list of the available + foundation models from Amazon Bedrock. + """, + discussion: """ + """ + ) + + /// Construct a string listing the specified modalities. + /// + /// - Parameter modalities: An array of the modalities to list. + /// + /// - Returns: A string with a human-readable list of modalities. + func buildModalityList(modalities: [BedrockClientTypes.ModelModality]?) -> String { + var first = true + var str = "" + + if modalities == nil { + return "" + } + + for modality in modalities! { + if !first { + str += ", " + } + first = false + str += modality.rawValue + } + + return str + } + + /// Construct a string listing the specified customizations. + /// + /// - Parameter customizations: An array of the customizations to list. + /// + /// - Returns: A string listing the customizations. + func buildCustomizationList(customizations: [BedrockClientTypes.ModelCustomization]?) -> String { + var first = true + var str = "" + + if customizations == nil { + return "" + } + + for customization in customizations! { + if !first { + str += ", " + } + first = false + str += customization.rawValue + } + + return str + } + + /// Construct a string listing the specified inferences. + /// + /// - Parameter inferences: An array of inferences to list. + /// + /// - Returns: A string listing the specified inferences. + func buildInferenceList(inferences: [BedrockClientTypes.InferenceType]?) -> String { + var first = true + var str = "" + + if inferences == nil { + return "" + } + + for inference in inferences! { + if !first { + str += ", " + } + first = false + str += inference.rawValue + } + + return str + } + + /// Called by ``main()`` to run the bulk of the example. + func runAsync() async throws { + // snippet-start:[swift.bedrock.ListFoundationModels] + // Always use the Region "us-east-1" to have access to the most models. + let config = try await BedrockClient.BedrockClientConfiguration(region: "us-east-1") + let bedrockClient = BedrockClient(config: config) + + let output = try await bedrockClient.listFoundationModels( + input: ListFoundationModelsInput() + ) + + guard let summaries = output.modelSummaries else { + print("No models returned.") + return + } + // snippet-end:[swift.bedrock.ListFoundationModels] + + for summary in summaries { + print("==========================================") + print(" Model ID: \(summary.modelId ?? "")") + print("------------------------------------------") + print(" Name: \(summary.modelName ?? "")") + print(" Provider: \(summary.providerName ?? "")") + print(" Input modalities: \(buildModalityList(modalities: summary.inputModalities))") + print(" Output modalities: \(buildModalityList(modalities: summary.outputModalities))") + print(" Supported customizations: \(buildCustomizationList(customizations: summary.customizationsSupported ))") + print(" Supported inference types: \(buildInferenceList(inferences: summary.inferenceTypesSupported))") + print("------------------------------------------\n") + } + + print("\(summaries.count) models available.") + } +} + +/// The program's asynchronous entry point. +@main +struct Main { + static func main() async { + let args = Array(CommandLine.arguments.dropFirst()) + + do { + let command = try ExampleCommand.parse(args) + try await command.runAsync() + } catch { + ExampleCommand.exit(withError: error) + } + } +} From a87bdcd95717a8daca5e30be48782d663b9a095c Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Thu, 10 Apr 2025 14:11:51 +0000 Subject: [PATCH 2/5] Bedrock ListFoundationModels example --- .../bedrock/ListFoundationModels/Sources/entry.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift index 7573d53cbbe..69c8551b8fd 100644 --- a/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift +++ b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift @@ -110,8 +110,8 @@ struct ExampleCommand: ParsableCommand { print("No models returned.") return } - // snippet-end:[swift.bedrock.ListFoundationModels] + // Output a list of the models with their details. for summary in summaries { print("==========================================") print(" Model ID: \(summary.modelId ?? "")") @@ -124,6 +124,7 @@ struct ExampleCommand: ParsableCommand { print(" Supported inference types: \(buildInferenceList(inferences: summary.inferenceTypesSupported))") print("------------------------------------------\n") } + // snippet-end:[swift.bedrock.ListFoundationModels] print("\(summaries.count) models available.") } From 3d816c994caa737bf2e4e5fe186cd91b7f0d0eae Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Thu, 10 Apr 2025 14:13:32 +0000 Subject: [PATCH 3/5] Rename SoS metadata tag --- .../bedrock/ListFoundationModels/Sources/entry.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift index 69c8551b8fd..6da3dba62a1 100644 --- a/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift +++ b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift @@ -8,9 +8,9 @@ import ArgumentParser import AWSClientRuntime import Foundation -// snippet-start:[bedrock.swift.import] +// snippet-start:[swift.bedrock.import] import AWSBedrock -// snippet-end:[bedrock.swift.import] +// snippet-end:[swift.bedrock.import] struct ExampleCommand: ParsableCommand { static var configuration = CommandConfiguration( From 5f651b53ee17223c6dde3df71882f93d7250fe94 Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Thu, 10 Apr 2025 15:49:55 +0000 Subject: [PATCH 4/5] Add metadata for Bedrock. Build/rebuild readmes --- .doc_gen/metadata/bedrock_metadata.yaml | 17 +++ .../ListFoundationModels/Sources/entry.swift | 2 + swift/example_code/bedrock/README.md | 98 ++++++++++++++ swift/example_code/dynamodb/README.md | 18 +-- swift/example_code/dynamodb/README.old.md | 124 ++++++++++++++++++ 5 files changed, 250 insertions(+), 9 deletions(-) create mode 100644 swift/example_code/bedrock/README.md create mode 100644 swift/example_code/dynamodb/README.old.md diff --git a/.doc_gen/metadata/bedrock_metadata.yaml b/.doc_gen/metadata/bedrock_metadata.yaml index 851bd1ee152..6c95de1e8e5 100644 --- a/.doc_gen/metadata/bedrock_metadata.yaml +++ b/.doc_gen/metadata/bedrock_metadata.yaml @@ -38,6 +38,14 @@ bedrock_Hello: - description: snippet_tags: - bedrock.example_code.hello_bedrock.complete + Swift: + versions: + - sdk_version: 1 + github: swift/example_code/bedrock + excerpts: + - description: + snippet_tags: + - swift.bedrock.hello services: bedrock: {ListFoundationModels} @@ -135,5 +143,14 @@ bedrock_ListFoundationModels: - description: List the available Bedrock foundation models. snippet_tags: - Bedrock.dotnetv3.BedrockActions.ListFoundationModels + Swift: + versions: + - sdk_version: 1 + github: swift/example_code/bedrock + excerpts: + - description: + snippet_tags: + - swift.bedrock.import + - swift.bedrock.ListFoundationModels services: bedrock: {ListFoundationModels} diff --git a/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift index 6da3dba62a1..c6ec84ed2ae 100644 --- a/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift +++ b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift @@ -4,6 +4,7 @@ // An example demonstrating how to fetch a list of foundation models available // using Amazon Bedrock. +// snippet-start:[swift.bedrock.hello] import ArgumentParser import AWSClientRuntime import Foundation @@ -144,3 +145,4 @@ struct Main { } } } +// snippet-end:[swift.bedrock.hello] diff --git a/swift/example_code/bedrock/README.md b/swift/example_code/bedrock/README.md new file mode 100644 index 00000000000..08b71d669d1 --- /dev/null +++ b/swift/example_code/bedrock/README.md @@ -0,0 +1,98 @@ +# Amazon Bedrock code examples for the SDK for Swift + +## Overview + +Shows how to use the AWS SDK for Swift to work with Amazon Bedrock. + + + + +_Amazon Bedrock enables you to build and scale generative AI applications with foundation models._ + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder. + + + + + +### Get started + +- [Hello Amazon Bedrock](ListFoundationModels/Sources/entry.swift#L7) (`ListFoundationModels`) + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +- [ListFoundationModels](ListFoundationModels/Sources/entry.swift#L101) + + + + + +## Run the examples + +### Instructions + +To build any of these examples from a terminal window, navigate into its +directory, then use the following command: + +``` +$ swift build +``` + +To build one of these examples in Xcode, navigate to the example's directory +(such as the `ListUsers` directory, to build that example). Then type `xed.` +to open the example directory in Xcode. You can then use standard Xcode build +and run commands. + + + + +#### Hello Amazon Bedrock + +This example shows you how to get started using Amazon Bedrock. + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `swift` folder. + + + + + + +## Additional resources + +- [Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +- [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +- [SDK for Swift Amazon Bedrock reference](https://sdk.amazonaws.com/swift/api/awsbedrock/latest/documentation/awsbedrock) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 diff --git a/swift/example_code/dynamodb/README.md b/swift/example_code/dynamodb/README.md index 8263323f766..93fceebdaa2 100644 --- a/swift/example_code/dynamodb/README.md +++ b/swift/example_code/dynamodb/README.md @@ -41,16 +41,16 @@ Code examples that show you how to perform the essential operations within a ser Code excerpts that show you how to call individual service functions. - [BatchGetItem](BatchGetItem/Sources/MovieDatabase.swift#L304) -- [BatchWriteItem](basics/MovieList/MovieTable.swift#L206) +- [BatchWriteItem](basics/MovieList/MovieTable.swift#L203) - [CreateTable](basics/MovieList/MovieTable.swift#L62) -- [DeleteItem](basics/MovieList/MovieTable.swift#L545) -- [DeleteTable](basics/MovieList/MovieTable.swift#L153) -- [GetItem](basics/MovieList/MovieTable.swift#L324) +- [DeleteItem](basics/MovieList/MovieTable.swift#L542) +- [DeleteTable](basics/MovieList/MovieTable.swift#L150) +- [GetItem](basics/MovieList/MovieTable.swift#L321) - [ListTables](ListTables/Sources/DatabaseManager.swift#L107) -- [PutItem](basics/MovieList/MovieTable.swift#L269) -- [Query](basics/MovieList/MovieTable.swift#L363) -- [Scan](basics/MovieList/MovieTable.swift#L415) -- [UpdateItem](basics/MovieList/MovieTable.swift#L480) +- [PutItem](basics/MovieList/MovieTable.swift#L266) +- [Query](basics/MovieList/MovieTable.swift#L360) +- [Scan](basics/MovieList/MovieTable.swift#L412) +- [UpdateItem](basics/MovieList/MovieTable.swift#L477) @@ -121,4 +121,4 @@ in the `swift` 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/swift/example_code/dynamodb/README.old.md b/swift/example_code/dynamodb/README.old.md new file mode 100644 index 00000000000..8263323f766 --- /dev/null +++ b/swift/example_code/dynamodb/README.old.md @@ -0,0 +1,124 @@ +# DynamoDB code examples for the SDK for Swift + +## Overview + +Shows how to use the AWS SDK for Swift to work with Amazon DynamoDB. + + + + +_DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability._ + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder. + + + + + +### Basics + +Code examples that show you how to perform the essential operations within a service. + +- [Learn the basics](basics/MovieList/MovieTable.swift) + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +- [BatchGetItem](BatchGetItem/Sources/MovieDatabase.swift#L304) +- [BatchWriteItem](basics/MovieList/MovieTable.swift#L206) +- [CreateTable](basics/MovieList/MovieTable.swift#L62) +- [DeleteItem](basics/MovieList/MovieTable.swift#L545) +- [DeleteTable](basics/MovieList/MovieTable.swift#L153) +- [GetItem](basics/MovieList/MovieTable.swift#L324) +- [ListTables](ListTables/Sources/DatabaseManager.swift#L107) +- [PutItem](basics/MovieList/MovieTable.swift#L269) +- [Query](basics/MovieList/MovieTable.swift#L363) +- [Scan](basics/MovieList/MovieTable.swift#L415) +- [UpdateItem](basics/MovieList/MovieTable.swift#L480) + + + + + +## Run the examples + +### Instructions + +To build any of these examples from a terminal window, navigate into its +directory, then use the following command: + +``` +$ swift build +``` + +To build one of these examples in Xcode, navigate to the example's directory +(such as the `ListUsers` directory, to build that example). Then type `xed.` +to open the example directory in Xcode. You can then use standard Xcode build +and run commands. + + + + + +#### Learn the basics + +This example shows you how to do the following: + +- Create a table that can hold movie data. +- Put, get, and update a single movie in the table. +- Write movie data to the table from a sample JSON file. +- Query for movies that were released in a given year. +- Scan for movies that were released in a range of years. +- Delete a movie from the table, then delete the table. + + + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `swift` folder. + + + + + + +## Additional resources + +- [DynamoDB Developer Guide](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) +- [DynamoDB API Reference](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/Welcome.html) +- [SDK for Swift DynamoDB reference](https://sdk.amazonaws.com/swift/api/awsdynamodb/latest/documentation/awsdynamodb) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file From 2b4d6e52df35e257b4ef9b99484fefe946b00e9d Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Thu, 10 Apr 2025 18:41:50 +0000 Subject: [PATCH 5/5] Remove a file added by accident --- swift/example_code/dynamodb/README.old.md | 124 ---------------------- 1 file changed, 124 deletions(-) delete mode 100644 swift/example_code/dynamodb/README.old.md diff --git a/swift/example_code/dynamodb/README.old.md b/swift/example_code/dynamodb/README.old.md deleted file mode 100644 index 8263323f766..00000000000 --- a/swift/example_code/dynamodb/README.old.md +++ /dev/null @@ -1,124 +0,0 @@ -# DynamoDB code examples for the SDK for Swift - -## Overview - -Shows how to use the AWS SDK for Swift to work with Amazon DynamoDB. - - - - -_DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability._ - -## ⚠ Important - -* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). -* Running the tests might result in charges to your AWS account. -* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). -* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). - - - - -## Code examples - -### Prerequisites - -For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder. - - - - - -### Basics - -Code examples that show you how to perform the essential operations within a service. - -- [Learn the basics](basics/MovieList/MovieTable.swift) - - -### Single actions - -Code excerpts that show you how to call individual service functions. - -- [BatchGetItem](BatchGetItem/Sources/MovieDatabase.swift#L304) -- [BatchWriteItem](basics/MovieList/MovieTable.swift#L206) -- [CreateTable](basics/MovieList/MovieTable.swift#L62) -- [DeleteItem](basics/MovieList/MovieTable.swift#L545) -- [DeleteTable](basics/MovieList/MovieTable.swift#L153) -- [GetItem](basics/MovieList/MovieTable.swift#L324) -- [ListTables](ListTables/Sources/DatabaseManager.swift#L107) -- [PutItem](basics/MovieList/MovieTable.swift#L269) -- [Query](basics/MovieList/MovieTable.swift#L363) -- [Scan](basics/MovieList/MovieTable.swift#L415) -- [UpdateItem](basics/MovieList/MovieTable.swift#L480) - - - - - -## Run the examples - -### Instructions - -To build any of these examples from a terminal window, navigate into its -directory, then use the following command: - -``` -$ swift build -``` - -To build one of these examples in Xcode, navigate to the example's directory -(such as the `ListUsers` directory, to build that example). Then type `xed.` -to open the example directory in Xcode. You can then use standard Xcode build -and run commands. - - - - - -#### Learn the basics - -This example shows you how to do the following: - -- Create a table that can hold movie data. -- Put, get, and update a single movie in the table. -- Write movie data to the table from a sample JSON file. -- Query for movies that were released in a given year. -- Scan for movies that were released in a range of years. -- Delete a movie from the table, then delete the table. - - - - - - - - - -### Tests - -⚠ Running tests might result in charges to your AWS account. - - -To find instructions for running these tests, see the [README](../../README.md#Tests) -in the `swift` folder. - - - - - - -## Additional resources - -- [DynamoDB Developer Guide](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) -- [DynamoDB API Reference](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/Welcome.html) -- [SDK for Swift DynamoDB reference](https://sdk.amazonaws.com/swift/api/awsdynamodb/latest/documentation/awsdynamodb) - - - - ---- - -Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - -SPDX-License-Identifier: Apache-2.0 \ No newline at end of file