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