-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathentry.swift
More file actions
148 lines (126 loc) · 4.66 KB
/
entry.swift
File metadata and controls
148 lines (126 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// An example demonstrating how to fetch a list of foundation models available
// using Amazon Bedrock.
// snippet-start:[swift.bedrock.hello]
import ArgumentParser
import AWSClientRuntime
import Foundation
// snippet-start:[swift.bedrock.import]
import AWSBedrock
// snippet-end:[swift.bedrock.import]
struct ExampleCommand: ParsableCommand {
static var configuration = CommandConfiguration(
commandName: "ListFoundationModels",
abstract: """
This example demonstrates how to retrieve a list of the available
foundation models from Amazon Bedrock.
""",
discussion: """
"""
)
/// Construct a string listing the specified modalities.
///
/// - Parameter modalities: An array of the modalities to list.
///
/// - Returns: A string with a human-readable list of modalities.
func buildModalityList(modalities: [BedrockClientTypes.ModelModality]?) -> String {
var first = true
var str = ""
if modalities == nil {
return "<none>"
}
for modality in modalities! {
if !first {
str += ", "
}
first = false
str += modality.rawValue
}
return str
}
/// Construct a string listing the specified customizations.
///
/// - Parameter customizations: An array of the customizations to list.
///
/// - Returns: A string listing the customizations.
func buildCustomizationList(customizations: [BedrockClientTypes.ModelCustomization]?) -> String {
var first = true
var str = ""
if customizations == nil {
return "<none>"
}
for customization in customizations! {
if !first {
str += ", "
}
first = false
str += customization.rawValue
}
return str
}
/// Construct a string listing the specified inferences.
///
/// - Parameter inferences: An array of inferences to list.
///
/// - Returns: A string listing the specified inferences.
func buildInferenceList(inferences: [BedrockClientTypes.InferenceType]?) -> String {
var first = true
var str = ""
if inferences == nil {
return "<none>"
}
for inference in inferences! {
if !first {
str += ", "
}
first = false
str += inference.rawValue
}
return str
}
/// Called by ``main()`` to run the bulk of the example.
func runAsync() async throws {
// snippet-start:[swift.bedrock.ListFoundationModels]
// Always use the Region "us-east-1" to have access to the most models.
let config = try await BedrockClient.BedrockClientConfiguration(region: "us-east-1")
let bedrockClient = BedrockClient(config: config)
let output = try await bedrockClient.listFoundationModels(
input: ListFoundationModelsInput()
)
guard let summaries = output.modelSummaries else {
print("No models returned.")
return
}
// Output a list of the models with their details.
for summary in summaries {
print("==========================================")
print(" Model ID: \(summary.modelId ?? "<unknown>")")
print("------------------------------------------")
print(" Name: \(summary.modelName ?? "<unknown>")")
print(" Provider: \(summary.providerName ?? "<unknown>")")
print(" Input modalities: \(buildModalityList(modalities: summary.inputModalities))")
print(" Output modalities: \(buildModalityList(modalities: summary.outputModalities))")
print(" Supported customizations: \(buildCustomizationList(customizations: summary.customizationsSupported ))")
print(" Supported inference types: \(buildInferenceList(inferences: summary.inferenceTypesSupported))")
print("------------------------------------------\n")
}
// snippet-end:[swift.bedrock.ListFoundationModels]
print("\(summaries.count) models available.")
}
}
/// The program's asynchronous entry point.
@main
struct Main {
static func main() async {
let args = Array(CommandLine.arguments.dropFirst())
do {
let command = try ExampleCommand.parse(args)
try await command.runAsync()
} catch {
ExampleCommand.exit(withError: error)
}
}
}
// snippet-end:[swift.bedrock.hello]