You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: swift-bedrock-library/README.md
+19-20Lines changed: 19 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
# BedrockService
1
+
# Swift Bedrock Library
2
2
3
-
This library is a work in progress, feel free to open an issue, but do not use it in your projects just yet.
3
+
A tiny layer on top of the [AWS SDK for Swift](https://github.com/awslabs/aws-sdk-swift) for interacting with [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) foundation models. This library provides a convenient way to access Amazon Bedrock's capabilities from Swift applications.
4
4
5
5
## Getting started with BedrockService
6
6
@@ -66,12 +66,11 @@ Use the `listModels()` function to test your set-up. This function will return a
66
66
let models =tryawait bedrock.listModels()
67
67
```
68
68
69
-
70
69
## Chatting using the Converse or ConverseStream API
71
70
72
71
### Text prompt
73
72
74
-
To sent a text prompt to a model, first choose a model that supports converse, you can verify this by using the `hasConverseModality` function on the `BedrockModel`. Then use the model to create a `ConverseRequestBuilder`, add your prompt to it with the `.withPormpt` function. Use the builder to sent your request to the Converse API with the `converse` function. You can then easily print the reply and use it to create a new builder with the same model and inference parameters but with an updated history.
73
+
To send a text prompt to a model, first choose a model that supports converse, you can verify this by using the `hasConverseModality` function on the `BedrockModel`. Then use the model to create a `ConverseRequestBuilder`, add your prompt to it with the `.withPrompt` function. Use the builder to send your request to the Converse API with the `converse` function. You can then easily print the reply and use it to create a new builder with the same model and inference parameters but with an updated history.
To sent an image to a model, first ensure the model suports vision. Next simply add the image to the `ConverseRequestBuilder` with the `withImage` function. The function can either take an `ImageBlock` object or the format and bytes to construct the object.
157
+
To send an image to a model, first ensure the model supports vision. Next simply add the image to the `ConverseRequestBuilder` with the `withImage` function. The function can either take an `ImageBlock` object or the format and bytes to construct the object.
159
158
160
159
161
160
```swift
@@ -205,7 +204,7 @@ To use streaming use the exact same `ConverseRequestBuilder`, but use the `conve
205
204
206
205
### Document
207
206
208
-
To sent an document to a model, first ensure the model suports document. Next simply add the image to the `ConverseRequestBuilder` with the `withDocument` function. The function can either take an`DocumentBlock` object or the name, format and bytes to construct the object.
207
+
To send a document to a model, first ensure the model supports document. Next simply add the document to the `ConverseRequestBuilder` with the `withDocument` function. The function can either take a`DocumentBlock` object or the name, format and bytes to construct the object.
209
208
210
209
```swift
211
210
let model: BedrockModel = .nova_lite
@@ -256,7 +255,7 @@ To use streaming use the exact same `ConverseRequestBuilder`, but use the `conve
256
255
257
256
### Tools
258
257
259
-
For tool usage, first ensure the model supports the use of tools. Next define at least one `Tool` and add it to the `ConverseRequestBuilder` with the `withTool` function (or the `withTools` function to add several tools at once). After sending a request the model could now sent back a `ToolUse` asking for specific information from a specific tool. Use this to sent the information back in a `ToolResult`, by using the `withToolResult` function. You will now receive a reply informed by the result from the tool.
258
+
For tool usage, first ensure the model supports the use of tools. Next define at least one `Tool` and add it to the `ConverseRequestBuilder` with the `withTool` function (or the `withTools` function to add several tools at once). After sending a request the model could now send back a `ToolUse` asking for specific information from a specific tool. Use this to send the information back in a `ToolResult`, by using the `withToolResult` function. You will now receive a reply informed by the result from the tool.
260
259
261
260
262
261
```swift
@@ -406,7 +405,7 @@ while true {
406
405
407
406
### Reasoning
408
407
409
-
To not only get a text reply but to also follow the models reasoning, enable reasoning by using the `withReasoning` and optionally set the maximum lenght of the reasoning with `withMaxReasoningTokens`. These functions can be combined using the `withReasoning(maxReasoningTokens: Int)` function.
408
+
To not only get a text reply but to also follow the model's reasoning, enable reasoning by using the `withReasoning` and optionally set the maximum length of the reasoning with `withMaxReasoningTokens`. These functions can be combined using the `withReasoning(maxReasoningTokens: Int)` function.
410
409
411
410
```swift
412
411
let model: BedrockModel = .claudev3_7_sonnet
@@ -433,7 +432,7 @@ if let reasoning = try? reply.getReasoningBlock() {
433
432
print("\nAssistant: \(reply)")
434
433
```
435
434
436
-
To combine reasoning and streaming, use the same `ConverseRequestBuilder`, but use the `converseStream` function instead of the `converse`function. A `ContentSegment` can then contain `reasoning`.
435
+
To combine reasoning and streaming, use the same `ConverseRequestBuilder`, but use the `converseStream` function instead of the `converse`function. A `ContentSegment` can then contain `reasoning`.
437
436
438
437
```swift
439
438
let model: BedrockModel = .claudev3_7_sonnet
@@ -587,7 +586,7 @@ let nonExistent: String? = json["nonExistentKey"]
587
586
print(nonExistent ==nil) // Output: true
588
587
```
589
588
590
-
Note that the subscript methods is also able to handle nested objects.
589
+
Note that the subscript method is also able to handle nested objects.
591
590
592
591
```swift
593
592
let json =JSON([
@@ -651,7 +650,7 @@ let imageGeneration = try await bedrock.generateImage(
651
650
Note that the minimum, maximum and default values for each parameter are model specific and defined when the BedrockModel is created. Some parameters might not be supported by certain models.
652
651
653
652
## Generating image variations using the InvokeModel API
654
-
Choose a BedrockModel that supports image variations - you can verify this using the `hasImageVariationModality` and the `hasImageVariationModality` function. The `generateImageVariation` function allows you to create variations of an existing image with these parameters:
653
+
Choose a BedrockModel that supports image variations - you can verify this using the `hasImageModality` and the `hasImageVariationModality` function. The `generateImageVariation` function allows you to create variations of an existing image with these parameters:
655
654
656
655
-`images`: The base64-encoded source images used to create variations from
657
656
-`negativePrompt`: Text describing what to avoid in the generated image
@@ -700,15 +699,15 @@ Note that the minimum, maximum and default values for each parameter are model s
700
699
701
700
## Generating text using the InvokeModel API
702
701
703
-
Choose a BedrockModel that supports text generation, you can verify this using the `hasTextModality` function. when calling the `completeText` function you can provide some inference parameters:
702
+
Choose a BedrockModel that supports text generation, you can verify this using the `hasTextModality` function. When calling the `completeText` function you can provide some inference parameters:
704
703
705
704
-`maxTokens`: The maximum amount of tokens that the model is allowed to return
706
705
-`temperature`: Controls the randomness of the model's output
707
706
-`topP`: Nucleus sampling, this parameter controls the cumulative probability threshold for token selection
708
707
-`topK`: Limits the number of tokens the model considers for each step of text generation to the K most likely ones
709
708
-`stopSequences`: An array of strings that will cause the model to stop generating further text when encountered
710
709
711
-
The function returns a `TextCompletion` object containg the generated text.
710
+
The function returns a `TextCompletion` object containing the generated text.
712
711
713
712
```swift
714
713
let model: BedrockModel = .nova_micro
@@ -745,7 +744,7 @@ Note that the minimum, maximum and default values for each parameter are model s
745
744
746
745
### Converse
747
746
748
-
To add a new model that only needs the ConverseModality, simply use the `StandardConverse` and add the correct [inferece parameters](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html) and [supported converse features](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference-supported-models-features.html).
747
+
To add a new model that only needs the ConverseModality, simply use the `StandardConverse` and add the correct [inference parameters](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html) and [supported converse features](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference-supported-models-features.html).
For a text generation create a struct conforming to TextModality. Use the request body and response body you created in [the previous step](#step-1-create-family-specific-request-and-response-struct). Make sure to check for model(family) specific rules or parameters that are not supported here.
You can now create instances for any of the models that follow the request and response structure you defined. Make sure to check the allowed and default values for the inference parameters, especially if some parameters are not supported buy the model. Know that these parameters may differ significantly for model from the same family.
898
+
You can now create instances for any of the models that follow the request and response structure you defined. Make sure to check the allowed and default values for the inference parameters, especially if some parameters are not supported by the model. Know that these parameters may differ significantly for models from the same family.
900
899
901
900
```swift
902
901
extensionBedrockModel {
@@ -919,7 +918,7 @@ extension BedrockModel {
919
918
920
919
### Image
921
920
922
-
To add an image generation model from a model family that is not supported at all byt the library, the steps are much alike to the text completion models.
921
+
To add an image generation model from a model family that is not supported at all by the library, the steps are much alike to the text completion models.
923
922
924
923
#### Step 1: Create family-specific request and response struct
925
924
@@ -993,7 +992,7 @@ public struct AmazonImageResponseBody: ContainsImageGeneration {
993
992
}
994
993
```
995
994
996
-
#### Step 2: Create the the Modality
995
+
#### Step 2: Create the Modality
997
996
998
997
Determine the exact functionality and make sure to comply to the correct modality protocol. In this case we will use `TextToImageModality`.
999
998
Create a struct conforming to `ImageModality` and the specific functionality protocol. Use the request body and response body you created in [the previous step](#step-1-create-family-specific-request-and-response-struct). Make sure to check for model(family) specific rules or parameters that are not supported here.
You can now create instances for any of the models that follow the request and response structure you defined. Make sure to check the allowed and default values for the inference parameters, especially if some parameters are not supported buy the model. Know that these parameters may differ significantly for model from the same family.
1056
+
You can now create instances for any of the models that follow the request and response structure you defined. Make sure to check the allowed and default values for the inference parameters, especially if some parameters are not supported by the model. Know that these parameters may differ significantly for models from the same family.
0 commit comments