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/invoke-model/.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
30 changes: 30 additions & 0 deletions Examples/invoke-model/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "InvokeModel",
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
products: [
.executable(name: "InvokeModel", targets: ["InvokeModel"])
],
dependencies: [
// for production use, uncomment the following line
// .package(url: "https://github.com/build-on-aws/swift-bedrock-library.git", branch: "main"),

// for local development, use the following line
.package(name: "swift-bedrock-library", path: "../.."),

.package(url: "https://github.com/apple/swift-log.git", from: "1.5.0"),
],
targets: [
.executableTarget(
name: "InvokeModel",
dependencies: [
.product(name: "BedrockService", package: "swift-bedrock-library"),
.product(name: "Logging", package: "swift-log"),
]
)
]
)
45 changes: 45 additions & 0 deletions Examples/invoke-model/Sources/InvokeModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// 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 InvokeModel {
static func main() async throws {
do {
try await InvokeModel.run()
} catch {
print("Error:\n\(error)")
}
}
static func run() async throws {
var logger = Logger(label: "InvokeModel")
logger.logLevel = .debug

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

let model: BedrockModel = .claude_opus_v4_5

let response = try await bedrock.completeText("who are you?", with: model)

print(response.completion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
//
//===----------------------------------------------------------------------===//

// https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference.html

public protocol CrossRegionInferenceModality: Sendable {}
extension CrossRegionInferenceModality {
public func crossRegionPrefix(forRegion region: Region) -> String {
Expand All @@ -22,3 +24,8 @@ extension CrossRegionInferenceModality {
return ""
}
}

public protocol GlobalCrossRegionInferenceModality: Sendable {}
extension GlobalCrossRegionInferenceModality {
public func crossRegionPrefix() -> String { "global." }
}
4 changes: 2 additions & 2 deletions Sources/BedrockService/Models/Anthropic/Anthropic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ struct AnthropicText: TextModality, ConverseModality, ConverseStreamingModality,
return AnthropicRequestBody(
prompt: prompt,
maxTokens: maxTokens,
temperature: temperature ?? parameters.temperature.defaultValue,
topP: topP ?? parameters.topP.defaultValue,
temperature: temperature,
topP: topP,
topK: topK ?? parameters.topK.defaultValue,
stopSequences: stopSequences ?? parameters.stopSequences.defaultValue
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ typealias ClaudeV3_5Sonnet = AnthropicText
typealias ClaudeV3_7Sonnet = AnthropicText
typealias Claude_Sonnet_v4 = AnthropicText
typealias Claude_Opus_v4 = AnthropicText
typealias Claude_Sonnet_v4_5 = AnthropicText

// text
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html
Expand Down Expand Up @@ -236,4 +235,20 @@ extension BedrockModel {
maxReasoningTokens: Parameter(.maxReasoningTokens, minValue: 1_024, maxValue: 8_191, defaultValue: 4_096)
)
)
public static let claude_opus_v4_5: BedrockModel = BedrockModel(
id: "anthropic.claude-opus-4-5-20251101-v1:0",
name: "Claude Opus v4.5",
modality: Claude_Opus_v4_5(
parameters: TextGenerationParameters(
temperature: Parameter(.temperature, minValue: 0, maxValue: 1, defaultValue: 1),
maxTokens: Parameter(.maxTokens, minValue: 1, maxValue: 32_000, defaultValue: 8_192),
topP: Parameter(.topP, minValue: 0, maxValue: 1, defaultValue: 0.999),
topK: Parameter(.topK, minValue: 0, maxValue: 500, defaultValue: 0),
stopSequences: StopSequenceParams(maxSequences: 8191, defaultValue: []),
maxPromptSize: 200_000
),
features: [.textGeneration, .systemPrompts, .document, .vision, .toolUse, .reasoning],
maxReasoningTokens: Parameter(.maxReasoningTokens, minValue: 1_024, maxValue: 8_191, defaultValue: 4_096)
)
)
}
114 changes: 114 additions & 0 deletions Sources/BedrockService/Models/Anthropic/AnthropicGlobalModels.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

// Wrapper structs for v4.5 models to support GlobalCrossRegionInferenceModality
struct Claude_Sonnet_v4_5: TextModality, ConverseModality, ConverseStreamingModality, GlobalCrossRegionInferenceModality
{
private let anthropicText: AnthropicText

let converseParameters: ConverseParameters
let converseFeatures: [ConverseFeature]

init(parameters: TextGenerationParameters, features: [ConverseFeature], maxReasoningTokens: Parameter<Int>) {
self.anthropicText = AnthropicText(
parameters: parameters,
features: features,
maxReasoningTokens: maxReasoningTokens
)
self.converseParameters = anthropicText.converseParameters
self.converseFeatures = anthropicText.converseFeatures
}

func getName() -> String { anthropicText.getName() }
func getParameters() -> TextGenerationParameters { anthropicText.getParameters() }
func getConverseParameters() -> ConverseParameters { anthropicText.getConverseParameters() }
func getConverseFeatures() -> [ConverseFeature] { anthropicText.getConverseFeatures() }

func getTextRequestBody(
prompt: String,
maxTokens: Int?,
temperature: Double?,
topP: Double?,
topK: Int?,
stopSequences: [String]?,
serviceTier: ServiceTier
) throws -> BedrockBodyCodable {
try anthropicText.getTextRequestBody(
prompt: prompt,
maxTokens: maxTokens,
temperature: temperature,
topP: topP,
topK: topK,
stopSequences: stopSequences,
serviceTier: serviceTier
)
}

func getTextResponseBody(from data: Data) throws -> ContainsTextCompletion {
try anthropicText.getTextResponseBody(from: data)
}
}

struct Claude_Opus_v4_5: TextModality, ConverseModality, ConverseStreamingModality, GlobalCrossRegionInferenceModality {
private let anthropicText: AnthropicText

let converseParameters: ConverseParameters
let converseFeatures: [ConverseFeature]

init(parameters: TextGenerationParameters, features: [ConverseFeature], maxReasoningTokens: Parameter<Int>) {
self.anthropicText = AnthropicText(
parameters: parameters,
features: features,
maxReasoningTokens: maxReasoningTokens
)
self.converseParameters = anthropicText.converseParameters
self.converseFeatures = anthropicText.converseFeatures
}

func getName() -> String { anthropicText.getName() }
func getParameters() -> TextGenerationParameters { anthropicText.getParameters() }
func getConverseParameters() -> ConverseParameters { anthropicText.getConverseParameters() }
func getConverseFeatures() -> [ConverseFeature] { anthropicText.getConverseFeatures() }

func getTextRequestBody(
prompt: String,
maxTokens: Int?,
temperature: Double?,
topP: Double?,
topK: Int?,
stopSequences: [String]?,
serviceTier: ServiceTier
) throws -> BedrockBodyCodable {
try anthropicText.getTextRequestBody(
prompt: prompt,
maxTokens: maxTokens,
temperature: temperature,
topP: topP,
topK: topK,
stopSequences: stopSequences,
serviceTier: serviceTier
)
}

func getTextResponseBody(from data: Data) throws -> ContainsTextCompletion {
try anthropicText.getTextResponseBody(from: data)
}
}
22 changes: 16 additions & 6 deletions Sources/BedrockService/Models/BedrockModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
self = BedrockModel.claude_opus_v4
case BedrockModel.claude_sonnet_v4_5.id:
self = BedrockModel.claude_sonnet_v4_5
case BedrockModel.claude_opus_v4_5.id:
self = BedrockModel.claude_opus_v4_5
// titan
case BedrockModel.titan_text_g1_premier.id:
self = BedrockModel.titan_text_g1_premier
Expand Down Expand Up @@ -132,13 +134,21 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {

// MARK: Cross region inference
public func getModelIdWithCrossRegionInferencePrefix(region: Region) -> String {
// If the model does not support cross region inference, return the model ID as is
guard let crossRegionInferenceModality = modality as? CrossRegionInferenceModality else {
return id

// If the model support global cross region inference, return the global ID first
if let globalCrossRegionInferenceModality = modality as? GlobalCrossRegionInferenceModality {
let prefix = globalCrossRegionInferenceModality.crossRegionPrefix()
return "\(prefix)\(id)"
}

// If the model support cross region inference, return the regional ID
if let crossRegionInferenceModality = modality as? CrossRegionInferenceModality {
let prefix = crossRegionInferenceModality.crossRegionPrefix(forRegion: region)
return "\(prefix)\(id)"
}
// If the model supports cross region inference, return the model ID with the appropriate prefix
let prefix = crossRegionInferenceModality.crossRegionPrefix(forRegion: region)
return "\(prefix)\(id)"

// otherwise, returns the Id
return id
}

// MARK: Modality checks
Expand Down