|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Bedrock Library open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Amazon.com, Inc. or its affiliates |
| 6 | +// and the Swift Bedrock Library project authors |
| 7 | +// Licensed under Apache License v2.0 |
| 8 | +// |
| 9 | +// See LICENSE.txt for license information |
| 10 | +// See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors |
| 11 | +// |
| 12 | +// SPDX-License-Identifier: Apache-2.0 |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +import BedrockService |
| 17 | +import Logging |
| 18 | + |
| 19 | +@main |
| 20 | +struct TextChat { |
| 21 | + static func main() async throws { |
| 22 | + do { |
| 23 | + try await TextChat.run() |
| 24 | + } catch { |
| 25 | + print("Error:\n\(error)") |
| 26 | + } |
| 27 | + } |
| 28 | + static func run() async throws { |
| 29 | + var logger = Logger(label: "TextChat") |
| 30 | + logger.logLevel = .debug |
| 31 | + |
| 32 | + let bedrock = try await BedrockService( |
| 33 | + region: .useast1, |
| 34 | + logger: logger |
| 35 | + // uncomment if you use SSO with AWS Identity Center |
| 36 | + // authentication: .sso |
| 37 | + ) |
| 38 | + |
| 39 | + // select a model that supports the converse modality |
| 40 | + // models must be enabled in your AWS account |
| 41 | + let model: BedrockModel = .claudev3_7_sonnet |
| 42 | + |
| 43 | + guard model.hasConverseModality() else { |
| 44 | + throw MyError.incorrectModality("\(model.name) does not support converse") |
| 45 | + } |
| 46 | + |
| 47 | + // a reusable var to build the requests |
| 48 | + var request: ConverseRequestBuilder? = nil |
| 49 | + |
| 50 | + // we keep track of the history of the conversation |
| 51 | + var history: [Message] = [] |
| 52 | + |
| 53 | + // while the user doesn't type "exit" or "quit" |
| 54 | + while true { |
| 55 | + |
| 56 | + print("\nYou: ", terminator: "") |
| 57 | + let prompt: String = readLine() ?? "" |
| 58 | + guard prompt.isEmpty == false else { continue } |
| 59 | + if ["exit", "quit"].contains(prompt.lowercased()) { |
| 60 | + break |
| 61 | + } |
| 62 | + |
| 63 | + print("\nAssistant: ", terminator: "") |
| 64 | + |
| 65 | + if request == nil { |
| 66 | + // create a new request |
| 67 | + request = try ConverseRequestBuilder(with: model) |
| 68 | + .withPrompt(prompt) |
| 69 | + } else { |
| 70 | + // append the new prompt to the existing request |
| 71 | + // ConverseRequestBuilder is stateles, it doesn't keep track of the history |
| 72 | + request = try ConverseRequestBuilder(from: request!) |
| 73 | + .withHistory(history) |
| 74 | + .withPrompt(prompt) |
| 75 | + } |
| 76 | + |
| 77 | + // keep track of the history of the conversation |
| 78 | + history.append(Message(prompt)) |
| 79 | + |
| 80 | + // send the request |
| 81 | + let reply = try await bedrock.converseStream(with: request!) |
| 82 | + |
| 83 | + for try await element in reply.stream { |
| 84 | + // process the stream elements |
| 85 | + switch element { |
| 86 | + case .text(_, let text): |
| 87 | + print(text, terminator: "") |
| 88 | + case .messageComplete(let message): |
| 89 | + print("\n") |
| 90 | + history.append(message) |
| 91 | + default: |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + enum MyError: Error { |
| 99 | + case incorrectModality(String) |
| 100 | + } |
| 101 | +} |
0 commit comments