diff --git a/.doc_gen/metadata/bedrock-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-runtime_metadata.yaml index 9603630e1c5..90df1b274af 100644 --- a/.doc_gen/metadata/bedrock-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-runtime_metadata.yaml @@ -132,6 +132,14 @@ bedrock-runtime_Converse_AmazonNovaText: - description: Send a text message to Amazon Nova, using Bedrock's Converse API. snippet_tags: - python.example_code.bedrock-runtime.Converse_AmazonNovaText + Swift: + versions: + - sdk_version: 1 + github: swift/example_code/bedrock-runtime + excerpts: + - description: Send a text message to Amazon Nova, using Bedrock's Converse API. + snippet_tags: + - swift.example_code.bedrock-runtime.Converse_AmazonNovaText services: bedrock-runtime: {Converse} @@ -501,6 +509,14 @@ bedrock-runtime_ConverseStream_AmazonNovaText: - description: Send a text message to Amazon Nova, using Bedrock's Converse API and process the response stream in real-time. snippet_tags: - python.example_code.bedrock-runtime.ConverseStream_AmazonNovaText + Swift: + versions: + - sdk_version: 1 + github: swift/example_code/bedrock-runtime + excerpts: + - description: Send a text message to Amazon Nova, using Bedrock's Converse API and process the response stream in real-time. + snippet_tags: + - swift.example_code.bedrock-runtime.ConverseStream_AmazonNovaText services: bedrock-runtime: {ConverseStream} diff --git a/swift/example_code/bedrock-runtime/README.md b/swift/example_code/bedrock-runtime/README.md new file mode 100644 index 00000000000..8e9c28113b0 --- /dev/null +++ b/swift/example_code/bedrock-runtime/README.md @@ -0,0 +1,87 @@ +# Amazon Bedrock Runtime code examples for the SDK for Swift + +## Overview + +Shows how to use the AWS SDK for Swift to work with Amazon Bedrock Runtime. + + + + +_Amazon Bedrock Runtime is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon._ + +## ⚠ 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. + + + + +### Amazon Nova + +- [Converse](models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift#L4) +- [ConverseStream](models/amazon-nova/amazon-nova-text/Sources/ConverseStream/main.swift#L4) + + + + + +## 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. + + + + + + +### 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 Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +- [Amazon Bedrock Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +- [SDK for Swift Amazon Bedrock Runtime reference](https://sdk.amazonaws.com/swift/api/awsbedrockruntime/latest/documentation/awsbedrockruntime) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 diff --git a/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Package.swift b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Package.swift new file mode 100644 index 00000000000..cb9ecb939d7 --- /dev/null +++ b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Package.swift @@ -0,0 +1,38 @@ +// swift-tools-version: 6.1 +// 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: "AmazonNovaText", + // 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.2.61") + ], + 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: "Converse", + dependencies: [ + .product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"), + ], + path: "Sources/Converse" + ), + .executableTarget( + name: "ConverseStream", + dependencies: [ + .product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"), + ], + path: "Sources/ConverseStream" + ) + ] +) diff --git a/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift new file mode 100644 index 00000000000..2153263bcff --- /dev/null +++ b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift @@ -0,0 +1,65 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// snippet-start:[swift.example_code.bedrock-runtime.Converse_AmazonNovaText] +// An example demonstrating how to use the Conversation API to send +// a text message to Amazon Nova. + +import AWSBedrockRuntime + +func converse(_ textPrompt: String) async throws -> String { + + // Create a Bedrock Runtime client in the AWS Region you want to use. + let config = + try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration( + region: "us-east-1" + ) + let client = BedrockRuntimeClient(config: config) + + // Set the model ID. + let modelId = "amazon.nova-micro-v1:0" + + // Start a conversation with the user message. + let message = BedrockRuntimeClientTypes.Message( + content: [.text(textPrompt)], + role: .user + ) + + // Optionally use inference parameters + let inferenceConfig = + BedrockRuntimeClientTypes.InferenceConfiguration( + maxTokens: 512, + stopSequences: ["END"], + temperature: 0.5, + topp: 0.9 + ) + + // Create the ConverseInput to send to the model + let input = ConverseInput( + inferenceConfig: inferenceConfig, messages: [message], modelId: modelId) + + // Send the ConverseInput to the model + let response = try await client.converse(input: input) + + // Extract and return the response text. + if case let .message(msg) = response.output { + if case let .text(textResponse) = msg.content![0] { + return textResponse + } else { + return "No text response found in message content" + } + } else { + return "No message found in converse output" + } +} + +// snippet-end:[swift.example_code.bedrock-runtime.Converse_AmazonNovaText] + +do { + let reply = try await converse( + "Describe the purpose of a 'hello world' program in one line." + ) + print(reply) +} catch { + print("An error occurred: \(error)") +} diff --git a/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Sources/ConverseStream/main.swift b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Sources/ConverseStream/main.swift new file mode 100644 index 00000000000..7aaa499c0d5 --- /dev/null +++ b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Sources/ConverseStream/main.swift @@ -0,0 +1,75 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// snippet-start:[swift.example_code.bedrock-runtime.ConverseStream_AmazonNovaText] +// An example demonstrating how to use the Conversation API to send a text message +// to Amazon Nova and print the response stream + +import AWSBedrockRuntime + +func printConverseStream(_ textPrompt: String) async throws { + + // Create a Bedrock Runtime client in the AWS Region you want to use. + let config = + try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration( + region: "us-east-1" + ) + let client = BedrockRuntimeClient(config: config) + + // Set the model ID. + let modelId = "amazon.nova-lite-v1:0" + + // Start a conversation with the user message. + let message = BedrockRuntimeClientTypes.Message( + content: [.text(textPrompt)], + role: .user + ) + + // Optionally use inference parameters. + let inferenceConfig = + BedrockRuntimeClientTypes.InferenceConfiguration( + maxTokens: 512, + stopSequences: ["END"], + temperature: 0.5, + topp: 0.9 + ) + + // Create the ConverseStreamInput to send to the model. + let input = ConverseStreamInput( + inferenceConfig: inferenceConfig, messages: [message], modelId: modelId) + + // Send the ConverseStreamInput to the model. + let response = try await client.converseStream(input: input) + + // Extract the streaming response. + guard let stream = response.stream else { + print("No stream available") + return + } + + // Extract and print the streamed response text in real-time. + for try await event in stream { + switch event { + case .messagestart(_): + print("\nNova Lite:") + + case .contentblockdelta(let deltaEvent): + if case .text(let text) = deltaEvent.delta { + print(text, terminator: "") + } + + default: + break + } + } +} + +// snippet-end:[swift.example_code.bedrock-runtime.ConverseStream_AmazonNovaText] + +do { + try await printConverseStream( + "Describe the purpose of a 'hello world' program in two paragraphs." + ) +} catch { + print("An error occurred: \(error)") +}