Skip to content

Commit fbc3302

Browse files
committed
Add example for Bedrock's ListFoundationModels
1 parent 5cc45c3 commit fbc3302

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// The swift-tools-version declares the minimum version of Swift required to
6+
// build this package.
7+
8+
import PackageDescription
9+
10+
let package = Package(
11+
name: "ListFoundationModels",
12+
// Let Xcode know the minimum Apple platforms supported.
13+
platforms: [
14+
.macOS(.v13),
15+
.iOS(.v15)
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
.package(
20+
url: "https://github.com/awslabs/aws-sdk-swift",
21+
from: "1.0.0"),
22+
.package(
23+
url: "https://github.com/apple/swift-argument-parser.git",
24+
branch: "main"
25+
)
26+
],
27+
targets: [
28+
// Targets are the basic building blocks of a package, defining a module or a test suite.
29+
// Targets can depend on other targets in this package and products
30+
// from dependencies.
31+
.executableTarget(
32+
name: "ListFoundationModels",
33+
dependencies: [
34+
.product(name: "AWSBedrock", package: "aws-sdk-swift"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser")
36+
],
37+
path: "Sources")
38+
39+
]
40+
)
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// An example demonstrating how to fetch a list of foundation models available
5+
// using Amazon Bedrock.
6+
7+
import ArgumentParser
8+
import AWSClientRuntime
9+
import Foundation
10+
11+
// snippet-start:[bedrock.swift.import]
12+
import AWSBedrock
13+
// snippet-end:[bedrock.swift.import]
14+
15+
struct ExampleCommand: ParsableCommand {
16+
static var configuration = CommandConfiguration(
17+
commandName: "ListFoundationModels",
18+
abstract: """
19+
This example demonstrates how to retrieve a list of the available
20+
foundation models from Amazon Bedrock.
21+
""",
22+
discussion: """
23+
"""
24+
)
25+
26+
/// Construct a string listing the specified modalities.
27+
///
28+
/// - Parameter modalities: An array of the modalities to list.
29+
///
30+
/// - Returns: A string with a human-readable list of modalities.
31+
func buildModalityList(modalities: [BedrockClientTypes.ModelModality]?) -> String {
32+
var first = true
33+
var str = ""
34+
35+
if modalities == nil {
36+
return "<none>"
37+
}
38+
39+
for modality in modalities! {
40+
if !first {
41+
str += ", "
42+
}
43+
first = false
44+
str += modality.rawValue
45+
}
46+
47+
return str
48+
}
49+
50+
/// Construct a string listing the specified customizations.
51+
///
52+
/// - Parameter customizations: An array of the customizations to list.
53+
///
54+
/// - Returns: A string listing the customizations.
55+
func buildCustomizationList(customizations: [BedrockClientTypes.ModelCustomization]?) -> String {
56+
var first = true
57+
var str = ""
58+
59+
if customizations == nil {
60+
return "<none>"
61+
}
62+
63+
for customization in customizations! {
64+
if !first {
65+
str += ", "
66+
}
67+
first = false
68+
str += customization.rawValue
69+
}
70+
71+
return str
72+
}
73+
74+
/// Construct a string listing the specified inferences.
75+
///
76+
/// - Parameter inferences: An array of inferences to list.
77+
///
78+
/// - Returns: A string listing the specified inferences.
79+
func buildInferenceList(inferences: [BedrockClientTypes.InferenceType]?) -> String {
80+
var first = true
81+
var str = ""
82+
83+
if inferences == nil {
84+
return "<none>"
85+
}
86+
87+
for inference in inferences! {
88+
if !first {
89+
str += ", "
90+
}
91+
first = false
92+
str += inference.rawValue
93+
}
94+
95+
return str
96+
}
97+
98+
/// Called by ``main()`` to run the bulk of the example.
99+
func runAsync() async throws {
100+
// snippet-start:[swift.bedrock.ListFoundationModels]
101+
// Always use the Region "us-east-1" to have access to the most models.
102+
let config = try await BedrockClient.BedrockClientConfiguration(region: "us-east-1")
103+
let bedrockClient = BedrockClient(config: config)
104+
105+
let output = try await bedrockClient.listFoundationModels(
106+
input: ListFoundationModelsInput()
107+
)
108+
109+
guard let summaries = output.modelSummaries else {
110+
print("No models returned.")
111+
return
112+
}
113+
// snippet-end:[swift.bedrock.ListFoundationModels]
114+
115+
for summary in summaries {
116+
print("==========================================")
117+
print(" Model ID: \(summary.modelId ?? "<unknown>")")
118+
print("------------------------------------------")
119+
print(" Name: \(summary.modelName ?? "<unknown>")")
120+
print(" Provider: \(summary.providerName ?? "<unknown>")")
121+
print(" Input modalities: \(buildModalityList(modalities: summary.inputModalities))")
122+
print(" Output modalities: \(buildModalityList(modalities: summary.outputModalities))")
123+
print(" Supported customizations: \(buildCustomizationList(customizations: summary.customizationsSupported ))")
124+
print(" Supported inference types: \(buildInferenceList(inferences: summary.inferenceTypesSupported))")
125+
print("------------------------------------------\n")
126+
}
127+
128+
print("\(summaries.count) models available.")
129+
}
130+
}
131+
132+
/// The program's asynchronous entry point.
133+
@main
134+
struct Main {
135+
static func main() async {
136+
let args = Array(CommandLine.arguments.dropFirst())
137+
138+
do {
139+
let command = try ExampleCommand.parse(args)
140+
try await command.runAsync()
141+
} catch {
142+
ExampleCommand.exit(withError: error)
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)