Skip to content

Commit f926952

Browse files
monadierickxbrmur
authored andcommitted
Amazon Nova converse text
1 parent 5ab1226 commit f926952

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ bedrock-runtime_Converse_AmazonNovaText:
132132
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
133133
snippet_tags:
134134
- python.example_code.bedrock-runtime.Converse_AmazonNovaText
135+
Swift:
136+
versions:
137+
- sdk_version: 3
138+
github: swift/example_code/bedrock-runtime
139+
excerpts:
140+
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
141+
snippet_tags:
142+
- swift.example_code.bedrock-runtime.Converse_AmazonNovaText
135143
services:
136144
bedrock-runtime: {Converse}
137145

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// swift-tools-version: 6.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "AmazonNovaText",
8+
// Let Xcode know the minimum Apple platforms supported.
9+
platforms: [
10+
.macOS(.v13),
11+
.iOS(.v15)
12+
],
13+
dependencies: [
14+
// Dependencies declare other packages that this package depends on.
15+
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
16+
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.61")
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package, defining a module or a test suite.
20+
// Targets can depend on other targets in this package and products from dependencies.
21+
.executableTarget(
22+
name: "Converse",
23+
dependencies: [
24+
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
25+
.product(name: "ArgumentParser", package: "swift-argument-parser")
26+
],
27+
path: "Sources/Converse"
28+
)
29+
]
30+
)
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+
// An example demonstrating how to use converse with Amazon Nova Micro
5+
6+
// snippet-start:[swift.example_code.bedrock-runtime.Converse_AmazonNovaText]
7+
import ArgumentParser
8+
import AWSClientRuntime
9+
import Foundation
10+
import AWSBedrockRuntime
11+
12+
func converse(_ textPrompt: String) async throws -> String {
13+
14+
// Create a Bedrock Runtime client in the AWS Region you want to use.
15+
let config = try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration(region: "us-east-1")
16+
let client = BedrockRuntimeClient(config: config)
17+
18+
// Set the model ID.
19+
let modelId = "amazon.nova-micro-v1:0"
20+
21+
// Start a conversation with the user message.
22+
let message = BedrockRuntimeClientTypes.Message(
23+
content: [.text(textPrompt)],
24+
role: .user
25+
)
26+
27+
// Optionally use inference parameters
28+
let inferenceConfig = BedrockRuntimeClientTypes.InferenceConfiguration(
29+
maxTokens: 512,
30+
stopSequences: ["END"],
31+
temperature: 0.5,
32+
topp: 0.9
33+
)
34+
35+
// Create the ConverseInput to send to the model
36+
let input = ConverseInput(inferenceConfig: inferenceConfig, messages: [message], modelId: modelId)
37+
38+
// Send the ConverseInput to the model
39+
let response = try await client.converse(input: input)
40+
41+
// Extract and return the response text.
42+
if case let .message(msg) = response.output {
43+
if case let .text(textResponse) = msg.content![0] {
44+
return textResponse
45+
} else {
46+
return "No text response found in message content"
47+
}
48+
} else {
49+
return "No message found in converse output"
50+
}
51+
}
52+
53+
do {
54+
let reply = try await converse("Describe the purpose of a 'hello world' program in one line.")
55+
print(reply)
56+
} catch {
57+
print("An error occured: \(error)")
58+
}
59+
60+
// snippet-end:[swift.example_code.bedrock-runtime.Converse_AmazonNovaText]

0 commit comments

Comments
 (0)