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/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..c6ec84ed2ae --- /dev/null +++ b/swift/example_code/bedrock/ListFoundationModels/Sources/entry.swift @@ -0,0 +1,148 @@ +// 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. + +// snippet-start:[swift.bedrock.hello] +import ArgumentParser +import AWSClientRuntime +import Foundation + +// snippet-start:[swift.bedrock.import] +import AWSBedrock +// snippet-end:[swift.bedrock.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 + } + + // Output a list of the models with their details. + 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") + } + // snippet-end:[swift.bedrock.ListFoundationModels] + + 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) + } + } +} +// 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