Skip to content

Commit f7ab667

Browse files
committed
use an enum for different type of bodies in invoke response
1 parent c04960f commit f7ab667

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

Sources/InvokeModel/InvokeModelResponse.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ public struct InvokeModelResponse {
2626
let model: BedrockModel
2727
let contentType: ContentType
2828

29-
// TODO: FIXME: trasnform to an enum
30-
let textCompletionBody: ContainsTextCompletion?
31-
let imageGenerationBody: ContainsImageGeneration?
32-
let embeddingsBody: ContainsEmbeddings?
29+
let body: BodyType
30+
31+
public enum BodyType {
32+
case textCompletion(ContainsTextCompletion)
33+
case imageGeneration(ContainsImageGeneration)
34+
case embeddings(ContainsEmbeddings)
35+
}
3336

3437
private init(
3538
model: BedrockModel,
@@ -38,9 +41,7 @@ public struct InvokeModelResponse {
3841
) {
3942
self.model = model
4043
self.contentType = contentType
41-
self.textCompletionBody = textCompletionBody
42-
self.imageGenerationBody = nil
43-
self.embeddingsBody = nil
44+
self.body = .textCompletion(textCompletionBody)
4445
}
4546

4647
private init(
@@ -50,9 +51,7 @@ public struct InvokeModelResponse {
5051
) {
5152
self.model = model
5253
self.contentType = contentType
53-
self.imageGenerationBody = imageGenerationBody
54-
self.textCompletionBody = nil
55-
self.embeddingsBody = nil
54+
self.body = .imageGeneration(imageGenerationBody)
5655
}
5756

5857
private init(
@@ -62,9 +61,7 @@ public struct InvokeModelResponse {
6261
) {
6362
self.model = model
6463
self.contentType = contentType
65-
self.embeddingsBody = embeddingsBody
66-
self.textCompletionBody = nil
67-
self.imageGenerationBody = nil
64+
self.body = .embeddings(embeddingsBody)
6865
}
6966

7067
/// Creates a BedrockResponse from raw response data containing text completion
@@ -115,7 +112,7 @@ public struct InvokeModelResponse {
115112
/// - Throws: BedrockLibraryError.decodingError if the completion cannot be extracted
116113
public func getTextCompletion() throws -> TextCompletion {
117114
do {
118-
guard let textCompletionBody = textCompletionBody else {
115+
guard case .textCompletion(let textCompletionBody) = self.body else {
119116
throw BedrockLibraryError.decodingError("No text completion body found in the response")
120117
}
121118
return try textCompletionBody.getTextCompletion()
@@ -131,7 +128,7 @@ public struct InvokeModelResponse {
131128
/// - Throws: BedrockLibraryError.decodingError if the image generation cannot be extracted
132129
public func getGeneratedImage() throws -> ImageGenerationOutput {
133130
do {
134-
guard let imageGenerationBody = imageGenerationBody else {
131+
guard case .imageGeneration(let imageGenerationBody) = self.body else {
135132
throw BedrockLibraryError.decodingError("No image generation body found in the response")
136133
}
137134
return imageGenerationBody.getGeneratedImage()
@@ -144,7 +141,7 @@ public struct InvokeModelResponse {
144141

145142
public func getEmbeddings() throws -> Embeddings {
146143
do {
147-
guard let embeddingsBody else {
144+
guard case .embeddings(let embeddingsBody) = self.body else {
148145
throw BedrockLibraryError.decodingError("No embeddings body found in the response")
149146
}
150147
return embeddingsBody.getEmbeddings()

0 commit comments

Comments
 (0)