Skip to content

Commit 14f3b68

Browse files
monadierickxbrmur
authored andcommitted
ConverseStream
1 parent 5f81b37 commit 14f3b68

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Package.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ let package = Package(
2828
.product(name: "ArgumentParser", package: "swift-argument-parser")
2929
],
3030
path: "Sources/Converse"
31+
),
32+
.executableTarget(
33+
name: "ConverseStream",
34+
dependencies: [
35+
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
36+
.product(name: "ArgumentParser", package: "swift-argument-parser")
37+
],
38+
path: "Sources/ConverseStream"
3139
)
3240
]
3341
)

swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func converse(_ textPrompt: String) async throws -> String {
2020

2121
// Start a conversation with the user message.
2222
let message = BedrockRuntimeClientTypes.Message(
23-
content: [.text(textPrompt)],
24-
role: .user
25-
)
26-
23+
content: [.text(textPrompt)],
24+
role: .user
25+
)
26+
2727
// Optionally use inference parameters
2828
let inferenceConfig = BedrockRuntimeClientTypes.InferenceConfiguration(
2929
maxTokens: 512,
@@ -34,7 +34,7 @@ func converse(_ textPrompt: String) async throws -> String {
3434

3535
// Create the ConverseInput to send to the model
3636
let input = ConverseInput(inferenceConfig: inferenceConfig, messages: [message], modelId: modelId)
37-
37+
3838
// Send the ConverseInput to the model
3939
let response = try await client.converse(input: input)
4040

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// snippet-start:[swift.example_code.bedrock-runtime.ConverseStream_AmazonNovaText]
5+
// An example demonstrating how to use the Conversation API to send a text message to Amazon Nova and print the response stream
6+
7+
import ArgumentParser
8+
import AWSClientRuntime
9+
import Foundation
10+
import AWSBedrockRuntime
11+
12+
func printConverseStream(_ textPrompt: String) async throws {
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-lite-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 ConverseStreamInput to send to the model.
36+
let input = ConverseStreamInput(inferenceConfig: inferenceConfig, messages: [message], modelId: modelId)
37+
38+
// Send the ConverseStreamInput to the model.
39+
let response = try await client.converseStream(input: input)
40+
41+
// Extract the streaming response.
42+
guard let stream = response.stream else {
43+
print("No stream available")
44+
return
45+
}
46+
47+
// Extract and print the streamed response text in real-time.
48+
for try await event in stream {
49+
switch event {
50+
case .messagestart(_):
51+
print("\nNova Lite:")
52+
53+
case .contentblockdelta(let deltaEvent):
54+
if case .text(let text) = deltaEvent.delta {
55+
print(text, terminator: "")
56+
}
57+
58+
default:
59+
break
60+
}
61+
}
62+
}
63+
64+
do {
65+
try await printConverseStream("Describe the purpose of a 'hello world' program in two paragraphs.")
66+
} catch {
67+
print("An error occured: \(error)")
68+
}
69+
70+
// snippet-end:[swift.example_code.bedrock-runtime.ConverseStream_AmazonNovaText]

0 commit comments

Comments
 (0)