|
| 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 Main { |
| 21 | + static func main() async throws { |
| 22 | + do { |
| 23 | + try await Main.converseStream() |
| 24 | + } catch { |
| 25 | + print("Error:\n\(error)") |
| 26 | + } |
| 27 | + } |
| 28 | + static func converseStream() async throws { |
| 29 | + var logger = Logger(label: "Converse") |
| 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 = .nova_lite |
| 42 | + |
| 43 | + guard model.hasConverseModality() else { |
| 44 | + throw MyError.incorrectModality("\(model.name) does not support converse") |
| 45 | + } |
| 46 | + |
| 47 | + // create a request |
| 48 | + let builder = try ConverseRequestBuilder(with: model) |
| 49 | + .withPrompt("Tell me about rainbows") |
| 50 | + |
| 51 | + // send the request |
| 52 | + let reply = try await bedrock.converseStream(with: builder) |
| 53 | + |
| 54 | + // the reply gives access to two streams. |
| 55 | + // 1. `stream` is a high-level stream that provides elements of the conversation : |
| 56 | + // - messageStart: this is the start of a message, it contains the role (assistant or user) |
| 57 | + // - text: this is a delta of the text content, it contains the partial text |
| 58 | + // - reasoning: this is a delta of the reasoning content, it contains the partial reasoning text |
| 59 | + // - toolUse: this is a buffered tool use response, it contains the tool name and id, and the input parameters |
| 60 | + // - message complete: this includes the complete Message, ready to be added to the history and used for future requests |
| 61 | + // - metaData: this is the metadata about the response, it contains statitics about the response, such as the number of tokens used and the latency |
| 62 | + // |
| 63 | + // 2. `sdkStream` is the low-level stream provided by the AWS SDK. Use it when you need low level access to the stream, |
| 64 | + // such as when you want to handle the stream in a custom way or when you need to access the raw data. |
| 65 | + // see : https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference-call.html#conversation-inference-call-response-converse-stream |
| 66 | + for try await element in reply.stream { |
| 67 | + // process the stream elements |
| 68 | + switch element { |
| 69 | + case .messageStart(let role): |
| 70 | + logger.info("Message started with role: \(role)") |
| 71 | + case .text(_, let text): |
| 72 | + print(text, terminator: "") |
| 73 | + case .reasoning(let index, let reasoning): |
| 74 | + logger.info("Reasoning delta: \(reasoning)", metadata: ["index": "\(index)"]) |
| 75 | + case .toolUse(let index, let toolUse): |
| 76 | + logger.info( |
| 77 | + "Tool use: \(toolUse.name) with id: \(toolUse.id) and input: \(toolUse.input)", |
| 78 | + metadata: ["index": "\(index)"] |
| 79 | + ) |
| 80 | + case .messageComplete(_): |
| 81 | + print("\n") |
| 82 | + case .metaData(let metaData): |
| 83 | + logger.info("Metadata: \(metaData)") |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + enum MyError: Error { |
| 89 | + case incorrectModality(String) |
| 90 | + } |
| 91 | +} |
0 commit comments