Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Examples/converse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
27 changes: 27 additions & 0 deletions Examples/converse/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Converse",
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
products: [
.executable(name: "Converse", targets: ["Converse"])
],
dependencies: [
.package(url: "https://github.com/build-on-aws/swift-bedrock-library.git", branch: "main"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.0"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "Converse",
dependencies: [
.product(name: "BedrockService", package: "swift-bedrock-library"),
.product(name: "Logging", package: "swift-log"),
]
)
]
)
70 changes: 70 additions & 0 deletions Examples/converse/Sources/Converse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Bedrock Library open source project
//
// Copyright (c) 2025 Amazon.com, Inc. or its affiliates
// and the Swift Bedrock Library project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import BedrockService
import Logging

@main
struct Main {
static func main() async throws {
do {
try await Main.converse()
} catch {
print("Error:\n\(error)")
}
}
static func converse() async throws {
var logger = Logger(label: "Converse")
logger.logLevel = .debug

let bedrock = try await BedrockService(
region: .useast1,
logger: logger
// uncomment if you use SSO with AWS Identity Center
// authentication: .sso
)

// select a model that supports the converse modality
// models must be enabled in your AWS account
let model: BedrockModel = .nova_lite

guard model.hasConverseModality() else {
throw MyError.incorrectModality("\(model.name) does not support converse")
}

// create a request
var builder = try ConverseRequestBuilder(with: model)
.withPrompt("Tell me about rainbows")

// send the request
var reply = try await bedrock.converse(with: builder)

print("Assistant: \(reply)")

// create the next request
// you can use the previous reply to continue the conversation
builder = try ConverseRequestBuilder(from: builder, with: reply)
.withPrompt("Do you think birds can see them too?")

// send the next request
reply = try await bedrock.converse(with: builder)

print("Assistant: \(reply)")
}

enum MyError: Error {
case incorrectModality(String)
}
}
6 changes: 2 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ let package = Package(
.product(name: "Smithy", package: "smithy-swift"),
.product(name: "Logging", package: "swift-log"),
.product(name: "AwsCommonRuntimeKit", package: "aws-crt-swift"),
],
path: "Sources/BedrockService"
]
),
.testTarget(
name: "BedrockServiceTests",
dependencies: [
.target(name: "BedrockService")
],
path: "Tests/BedrockServiceTests"
]
),
]
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.