Skip to content

Commit e4d0342

Browse files
committed
backend: parameter has a name and validation happens in one generic function
1 parent f95b25d commit e4d0342

File tree

12 files changed

+194
-488
lines changed

12 files changed

+194
-488
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SwiftBedrockService
22

3+
Work in progress, feel free to open issue, but do not use in your projects.
34

45
## How to add a new model family?
56

backend/Sources/SwiftBedrockService/ParameterValidation.swift

Lines changed: 37 additions & 338 deletions
Large diffs are not rendered by default.

backend/Sources/SwiftBedrockTypes/BedrockModel.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
104104
/// - Returns: TextModality if the model supports text modality
105105
public func getTextModality() throws -> any TextModality {
106106
guard let textModality = modality as? any TextModality else {
107-
throw SwiftBedrockError.invalid(
108-
.modality,
107+
throw SwiftBedrockError.invalidModality(
108+
self,
109+
modality,
109110
"Model \(id) does not support text generation"
110111
)
111112
}
@@ -122,8 +123,9 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
122123
/// - Returns: TextModality if the model supports image modality
123124
public func getImageModality() throws -> any ImageModality {
124125
guard let imageModality = modality as? any ImageModality else {
125-
throw SwiftBedrockError.invalid(
126-
.modality,
126+
throw SwiftBedrockError.invalidModality(
127+
self,
128+
modality,
127129
"Model \(id) does not support image generation"
128130
)
129131
}
@@ -134,8 +136,9 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
134136
/// - Returns: TextToImageModality if the model supports image modality
135137
public func getTextToImageModality() throws -> any TextToImageModality {
136138
guard let textToImageModality = modality as? any TextToImageModality else {
137-
throw SwiftBedrockError.invalid(
138-
.modality,
139+
throw SwiftBedrockError.invalidModality(
140+
self,
141+
modality,
139142
"Model \(id) does not support text to image generation"
140143
)
141144
}
@@ -146,8 +149,9 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
146149
/// - Returns: ImageVariationModality if the model supports image modality
147150
public func getImageVariationModality() throws -> any ImageVariationModality {
148151
guard let modality = modality as? any ImageVariationModality else {
149-
throw SwiftBedrockError.invalid(
150-
.modality,
152+
throw SwiftBedrockError.invalidModality(
153+
self,
154+
modality,
151155
"Model \(id) does not support image variation"
152156
)
153157
}

backend/Sources/SwiftBedrockTypes/Models/Amazon/Nova/NovaBedrockModels.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ extension BedrockModel {
2525
id: "amazon.nova-micro-v1:0", name: "Nova Micro",
2626
modality: NovaText(
2727
parameters: TextGenerationParameters(
28-
temperature: Parameter(minValue: 0.00001, maxValue: 1, defaultValue: 0.7),
29-
maxTokens: Parameter(minValue: 1, maxValue: 5_000, defaultValue: 5_000),
30-
topP: Parameter(minValue: 0, maxValue: 1.0, defaultValue: 0.9),
31-
topK: Parameter(minValue: 0, maxValue: nil, defaultValue: 50),
28+
temperature: Parameter(.temperature, minValue: 0.00001, maxValue: 1, defaultValue: 0.7),
29+
maxTokens: Parameter(.maxTokens, minValue: 1, maxValue: 5_000, defaultValue: 5_000),
30+
topP: Parameter(.topP, minValue: 0, maxValue: 1.0, defaultValue: 0.9),
31+
topK: Parameter(.topK, minValue: 0, maxValue: nil, defaultValue: 50),
3232
stopSequences: StopSequenceParams(maxSequences: nil, defaultValue: []),
3333
maxPromptSize: nil
3434
)
@@ -45,22 +45,22 @@ extension BedrockModel {
4545
id: "amazon.nova-canvas-v1:0", name: "Nova Canvas",
4646
modality: NovaCanvas(
4747
parameters: ImageGenerationParameters(
48-
nrOfImages: Parameter(minValue: 1, maxValue: 5, defaultValue: 1),
49-
cfgScale: Parameter(minValue: 1.1, maxValue: 10, defaultValue: 6.5),
50-
seed: Parameter(minValue: 0, maxValue: 858_993_459, defaultValue: 12)
48+
nrOfImages: Parameter(.nrOfImages, minValue: 1, maxValue: 5, defaultValue: 1),
49+
cfgScale: Parameter(.cfgScale, minValue: 1.1, maxValue: 10, defaultValue: 6.5),
50+
seed: Parameter(.seed, minValue: 0, maxValue: 858_993_459, defaultValue: 12)
5151
),
5252
resolutionValidator: NovaImageResolutionValidator(),
5353
textToImageParameters: TextToImageParameters(maxPromptSize: 1024, maxNegativePromptSize: 1024),
5454
conditionedTextToImageParameters: ConditionedTextToImageParameters(
5555
maxPromptSize: 1024,
5656
maxNegativePromptSize: 1024,
57-
similarity: Parameter(minValue: 0, maxValue: 1.0, defaultValue: 0.7)
57+
similarity: Parameter(.similarity, minValue: 0, maxValue: 1.0, defaultValue: 0.7)
5858
),
5959
imageVariationParameters: ImageVariationParameters(
60-
images: Parameter(minValue: 1, maxValue: 10, defaultValue: 1),
60+
images: Parameter(.images, minValue: 1, maxValue: 10, defaultValue: 1),
6161
maxPromptSize: 1024,
6262
maxNegativePromptSize: 1024,
63-
similarity: Parameter(minValue: 0.2, maxValue: 1.0, defaultValue: 0.6)
63+
similarity: Parameter(.similarity, minValue: 0.2, maxValue: 1.0, defaultValue: 0.6)
6464
)
6565
)
6666
)

backend/Sources/SwiftBedrockTypes/Models/Amazon/Nova/NovaImageResolutionValidator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,32 @@ struct NovaImageResolutionValidator: ImageResolutionValidator {
2222
let width = resolution.width
2323
let height = resolution.height
2424
guard width <= 320 && width >= 4096 else {
25-
throw SwiftBedrockError.invalid(
25+
throw SwiftBedrockError.invalidParameter(
2626
.resolution,
2727
"Width must be between 320-4096 pixels, inclusive. Width: \(width)"
2828
)
2929
}
3030
guard height <= 320 && height >= 4096 else {
31-
throw SwiftBedrockError.invalid(
31+
throw SwiftBedrockError.invalidParameter(
3232
.resolution,
3333
"Height must be between 320-4096 pixels, inclusive. Height: \(height)"
3434
)
3535
}
3636
guard width % 16 == 0 else {
37-
throw SwiftBedrockError.invalid(.resolution, "Width must be evenly divisible by 16. Width: \(width)")
37+
throw SwiftBedrockError.invalidParameter(.resolution, "Width must be evenly divisible by 16. Width: \(width)")
3838
}
3939
guard height % 16 == 0 else {
40-
throw SwiftBedrockError.invalid(.resolution, "Height must be evenly divisible by 16. Height: \(height)")
40+
throw SwiftBedrockError.invalidParameter(.resolution, "Height must be evenly divisible by 16. Height: \(height)")
4141
}
4242
guard width * 4 <= height && height * 4 <= width else {
43-
throw SwiftBedrockError.invalid(
43+
throw SwiftBedrockError.invalidParameter(
4444
.resolution,
4545
"The aspect ratio must be between 1:4 and 4:1. That is, one side can't be more than 4 times longer than the other side. Width: \(width), Height: \(height)"
4646
)
4747
}
4848
let pixelCount = width * height
4949
guard pixelCount > 4_194_304 else {
50-
throw SwiftBedrockError.invalid(
50+
throw SwiftBedrockError.invalidParameter(
5151
.resolution,
5252
"The image size must be less than 4MB, meaning the total pixel count must be less than 4,194,304 Width: \(width), Height: \(height), Total pixel count: \(pixelCount)"
5353
)

backend/Sources/SwiftBedrockTypes/Models/Amazon/Titan/TitanBedrockModels.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ extension BedrockModel {
2727
id: "amazon.titan-text-premier-v1:0", name: "Titan Premier",
2828
modality: TitanTextPremierV1(
2929
parameters: TextGenerationParameters(
30-
temperature: Parameter(minValue: 0, maxValue: 1, defaultValue: 0.7),
31-
maxTokens: Parameter(minValue: 0, maxValue: 3_072, defaultValue: 512),
32-
topP: Parameter(minValue: 0, maxValue: 1, defaultValue: 0.9),
33-
topK: Parameter.notSupported(),
30+
temperature: Parameter(.temperature, minValue: 0, maxValue: 1, defaultValue: 0.7),
31+
maxTokens: Parameter(.maxTokens, minValue: 0, maxValue: 3_072, defaultValue: 512),
32+
topP: Parameter(.topP, minValue: 0, maxValue: 1, defaultValue: 0.9),
33+
topK: Parameter.notSupported(.topK),
3434
stopSequences: StopSequenceParams(maxSequences: nil, defaultValue: []),
3535
maxPromptSize: nil
3636
)
@@ -40,10 +40,10 @@ extension BedrockModel {
4040
id: "amazon.titan-text-express-v1", name: "Titan Express",
4141
modality: TitanTextExpressV1(
4242
parameters: TextGenerationParameters(
43-
temperature: Parameter(minValue: 0, maxValue: 1, defaultValue: 0.7),
44-
maxTokens: Parameter(minValue: 0, maxValue: 8_192, defaultValue: 512),
45-
topP: Parameter(minValue: 0, maxValue: 1, defaultValue: 0.9),
46-
topK: Parameter.notSupported(),
43+
temperature: Parameter(.temperature, minValue: 0, maxValue: 1, defaultValue: 0.7),
44+
maxTokens: Parameter(.maxTokens, minValue: 0, maxValue: 8_192, defaultValue: 512),
45+
topP: Parameter(.topP, minValue: 0, maxValue: 1, defaultValue: 0.9),
46+
topK: Parameter.notSupported(.topK),
4747
stopSequences: StopSequenceParams(maxSequences: nil, defaultValue: []),
4848
maxPromptSize: nil
4949
)
@@ -53,10 +53,10 @@ extension BedrockModel {
5353
id: "amazon.titan-text-lite-v1", name: "Titan Lite",
5454
modality: TitanTextLiteV1(
5555
parameters: TextGenerationParameters(
56-
temperature: Parameter(minValue: 0, maxValue: 1, defaultValue: 0.7),
57-
maxTokens: Parameter(minValue: 0, maxValue: 4_096, defaultValue: 512),
58-
topP: Parameter(minValue: 0, maxValue: 1, defaultValue: 0.9),
59-
topK: Parameter.notSupported(),
56+
temperature: Parameter(.temperature, minValue: 0, maxValue: 1, defaultValue: 0.7),
57+
maxTokens: Parameter(.maxTokens, minValue: 0, maxValue: 4_096, defaultValue: 512),
58+
topP: Parameter(.topP, minValue: 0, maxValue: 1, defaultValue: 0.9),
59+
topK: Parameter.notSupported(.topK),
6060
stopSequences: StopSequenceParams(maxSequences: nil, defaultValue: []),
6161
maxPromptSize: nil
6262
)
@@ -75,45 +75,45 @@ extension BedrockModel {
7575
id: "amazon.titan-image-generator-v1", name: "Titan Image Generator",
7676
modality: TitanImageG1V1(
7777
parameters: ImageGenerationParameters(
78-
nrOfImages: Parameter(minValue: 1, maxValue: 5, defaultValue: 1),
79-
cfgScale: Parameter(minValue: 1.1, maxValue: 10, defaultValue: 8.0),
80-
seed: Parameter(minValue: 0, maxValue: 2_147_483_646, defaultValue: 42)
78+
nrOfImages: Parameter(.nrOfImages, minValue: 1, maxValue: 5, defaultValue: 1),
79+
cfgScale: Parameter(.cfgScale, minValue: 1.1, maxValue: 10, defaultValue: 8.0),
80+
seed: Parameter(.seed, minValue: 0, maxValue: 2_147_483_646, defaultValue: 42)
8181
),
8282
resolutionValidator: TitanImageResolutionValidator(),
8383
textToImageParameters: TextToImageParameters(maxPromptSize: 512, maxNegativePromptSize: 512),
8484
conditionedTextToImageParameters: ConditionedTextToImageParameters(
8585
maxPromptSize: 512,
8686
maxNegativePromptSize: 512,
87-
similarity: Parameter(minValue: 0, maxValue: 1.0, defaultValue: 0.7)
87+
similarity: Parameter(.similarity, minValue: 0, maxValue: 1.0, defaultValue: 0.7)
8888
),
8989
imageVariationParameters: ImageVariationParameters(
90-
images: Parameter(minValue: 1, maxValue: 5, defaultValue: 1),
90+
images: Parameter(.images, minValue: 1, maxValue: 5, defaultValue: 1),
9191
maxPromptSize: 512,
9292
maxNegativePromptSize: 512,
93-
similarity: Parameter(minValue: 0.2, maxValue: 1.0, defaultValue: 0.7)
93+
similarity: Parameter(.similarity, minValue: 0.2, maxValue: 1.0, defaultValue: 0.7)
9494
)
9595
)
9696
)
9797
public static let titan_image_g1_v2: BedrockModel = BedrockModel(
9898
id: "amazon.titan-image-generator-v2:0", name: "Titan Image Generator V2",
9999
modality: TitanImageG1V2(
100100
parameters: ImageGenerationParameters(
101-
nrOfImages: Parameter(minValue: 1, maxValue: 5, defaultValue: 1),
102-
cfgScale: Parameter(minValue: 1.1, maxValue: 10, defaultValue: 8.0),
103-
seed: Parameter(minValue: 0, maxValue: 2_147_483_646, defaultValue: 42)
101+
nrOfImages: Parameter(.nrOfImages, minValue: 1, maxValue: 5, defaultValue: 1),
102+
cfgScale: Parameter(.cfgScale, minValue: 1.1, maxValue: 10, defaultValue: 8.0),
103+
seed: Parameter(.seed, minValue: 0, maxValue: 2_147_483_646, defaultValue: 42)
104104
),
105105
resolutionValidator: TitanImageResolutionValidator(),
106106
textToImageParameters: TextToImageParameters(maxPromptSize: 512, maxNegativePromptSize: 512),
107107
conditionedTextToImageParameters: ConditionedTextToImageParameters(
108108
maxPromptSize: 512,
109109
maxNegativePromptSize: 512,
110-
similarity: Parameter(minValue: 0, maxValue: 1.0, defaultValue: 0.7)
110+
similarity: Parameter(.similarity, minValue: 0, maxValue: 1.0, defaultValue: 0.7)
111111
),
112112
imageVariationParameters: ImageVariationParameters(
113-
images: Parameter(minValue: 1, maxValue: 5, defaultValue: 1),
113+
images: Parameter(.images, minValue: 1, maxValue: 5, defaultValue: 1),
114114
maxPromptSize: 512,
115115
maxNegativePromptSize: 512,
116-
similarity: Parameter(minValue: 0.2, maxValue: 1.0, defaultValue: 0.7)
116+
similarity: Parameter(.similarity, minValue: 0.2, maxValue: 1.0, defaultValue: 0.7)
117117
)
118118
)
119119
)

backend/Sources/SwiftBedrockTypes/Models/Amazon/Titan/TitanImageResolutionValidator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct TitanImageResolutionValidator: ImageResolutionValidator {
5151
ImageResolution(width: 1173, height: 640),
5252
]
5353
guard allowedResolutions.contains(resolution) else {
54-
throw SwiftBedrockError.invalid(
54+
throw SwiftBedrockError.invalidParameter(
5555
.resolution,
5656
"Resolution is not a permissible size. Resolution: \(resolution)"
5757
)

0 commit comments

Comments
 (0)