forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
65 lines (54 loc) · 1.97 KB
/
main.swift
File metadata and controls
65 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)")
}