Skip to content

Commit 4b33006

Browse files
authored
add converse example (#18)
* add converse example * swift format
1 parent 7d13aa3 commit 4b33006

File tree

111 files changed

+107
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+107
-4
lines changed

Examples/converse/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Examples/converse/Package.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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: "Converse",
8+
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
9+
products: [
10+
.executable(name: "Converse", targets: ["Converse"])
11+
],
12+
dependencies: [
13+
.package(url: "https://github.com/build-on-aws/swift-bedrock-library.git", branch: "main"),
14+
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.0"),
15+
],
16+
targets: [
17+
// Targets are the basic building blocks of a package, defining a module or a test suite.
18+
// Targets can depend on other targets in this package and products from dependencies.
19+
.executableTarget(
20+
name: "Converse",
21+
dependencies: [
22+
.product(name: "BedrockService", package: "swift-bedrock-library"),
23+
.product(name: "Logging", package: "swift-log"),
24+
]
25+
)
26+
]
27+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.converse()
24+
} catch {
25+
print("Error:\n\(error)")
26+
}
27+
}
28+
static func converse() 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+
var builder = try ConverseRequestBuilder(with: model)
49+
.withPrompt("Tell me about rainbows")
50+
51+
// send the request
52+
var reply = try await bedrock.converse(with: builder)
53+
54+
print("Assistant: \(reply)")
55+
56+
// create the next request
57+
// you can use the previous reply to continue the conversation
58+
builder = try ConverseRequestBuilder(from: builder, with: reply)
59+
.withPrompt("Do you think birds can see them too?")
60+
61+
// send the next request
62+
reply = try await bedrock.converse(with: builder)
63+
64+
print("Assistant: \(reply)")
65+
}
66+
67+
enum MyError: Error {
68+
case incorrectModality(String)
69+
}
70+
}

Package.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ let package = Package(
2626
.product(name: "Smithy", package: "smithy-swift"),
2727
.product(name: "Logging", package: "swift-log"),
2828
.product(name: "AwsCommonRuntimeKit", package: "aws-crt-swift"),
29-
],
30-
path: "Sources/BedrockService"
29+
]
3130
),
3231
.testTarget(
3332
name: "BedrockServiceTests",
3433
dependencies: [
3534
.target(name: "BedrockService")
36-
],
37-
path: "Tests/BedrockServiceTests"
35+
]
3836
),
3937
]
4038
)
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)