Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
9157354
initial week 14 commit
monadierickx Mar 28, 2025
9e4ddd8
backend: converse image
monadierickx Mar 31, 2025
beee69b
frontend: converse vision + backend: modality changes
monadierickx Mar 31, 2025
bebf86a
backend: converse modalitity added to all supported models
monadierickx Mar 31, 2025
3e1d724
Mistral support only for converse added
monadierickx Mar 31, 2025
7145629
converse image input
monadierickx Mar 31, 2025
f5e8bae
backend: converse refactor using Blocks
monadierickx Mar 31, 2025
94bf47e
DocumentFormat
monadierickx Mar 31, 2025
d5b0575
DocumentFormat
monadierickx Mar 31, 2025
193896d
DocumentBlock
monadierickx Mar 31, 2025
ac5cb6c
DocumentBlock
monadierickx Mar 31, 2025
fec96e2
S3Location
monadierickx Mar 31, 2025
3fef779
VideoSource
monadierickx Mar 31, 2025
da385b2
VideoFormat
monadierickx Mar 31, 2025
00faaf4
VideoBlock
monadierickx Mar 31, 2025
1e8f0be
ToolStatus
monadierickx Mar 31, 2025
8ac47e1
ToolResultContent
monadierickx Mar 31, 2025
3b1fd1a
ToolResultBlock
monadierickx Mar 31, 2025
b8c20a2
Content
monadierickx Mar 31, 2025
8b9a7b9
base for toolconfig
monadierickx Mar 31, 2025
226ab12
nested types for Blocks
monadierickx Apr 1, 2025
8201a9e
backend: converse systemprompts
monadierickx Apr 1, 2025
74d8c57
JSON for tools
monadierickx Apr 2, 2025
5376807
backend: tools
monadierickx Apr 2, 2025
b527ae3
tool use
monadierickx Apr 2, 2025
c5ba8cb
Tool result
monadierickx Apr 2, 2025
1b2cf7c
Tool result
monadierickx Apr 2, 2025
97a7bea
Deleted Converse only modality for Nova
monadierickx Apr 2, 2025
ed42213
refactor: moved converse transforamtions to BedrockTypes
monadierickx Apr 3, 2025
60305fe
convenience methods for Message
monadierickx Apr 3, 2025
600b591
converse function in BedrockService split up into two
monadierickx Apr 3, 2025
3f51bbc
refactor ToolResultBlock added nested types
monadierickx Apr 3, 2025
c710e50
docstrings
monadierickx Apr 3, 2025
35f097b
README updated
monadierickx Apr 3, 2025
d7aa092
README + StanderdConverse
monadierickx Apr 3, 2025
faf0d1d
BedrockModel Cohere
monadierickx Apr 3, 2025
2b5d571
formatting
monadierickx Apr 7, 2025
1a301f8
github action
monadierickx Apr 7, 2025
aad3293
formatting
monadierickx Apr 7, 2025
967bdbe
formatting
monadierickx Apr 7, 2025
b4539ff
SSO profile name
monadierickx Apr 7, 2025
f07f085
Package updated to add libraries
monadierickx Apr 7, 2025
45efdd8
README: optional parameters
monadierickx Apr 7, 2025
9649b1b
BedrockService refactor
monadierickx Apr 7, 2025
453e097
convenience initializers for ToolResultBlock
monadierickx Apr 7, 2025
b131b22
getToolUse
monadierickx Apr 7, 2025
0a39593
convenience converse functions
monadierickx Apr 7, 2025
29f5054
convenience converse functions
monadierickx Apr 7, 2025
d3cdd4d
formatting
monadierickx Apr 7, 2025
f9fcc71
formatting
monadierickx Apr 7, 2025
5d5b318
Parameter tests
monadierickx Apr 8, 2025
546ab78
parameter tests: text, image generation, image variation
monadierickx Apr 8, 2025
b57b6cb
parameter tests: converse text
monadierickx Apr 8, 2025
f44e20e
formatting
monadierickx Apr 8, 2025
9e9eb48
formatting
monadierickx Apr 8, 2025
2869e67
formatting
monadierickx Apr 8, 2025
d80837b
tests refactor
monadierickx Apr 8, 2025
82ea5de
testing
monadierickx Apr 11, 2025
c39fb2a
formatting
monadierickx Apr 11, 2025
fb4899a
actions
monadierickx Apr 11, 2025
1f6756d
testing
monadierickx Apr 11, 2025
a4c6c01
moved libraries to different repository
monadierickx Apr 11, 2025
724f87a
formatting
monadierickx Apr 11, 2025
4a76ee2
readme
monadierickx Apr 11, 2025
6a191e3
inital week 16 commit
monadierickx Apr 11, 2025
c6f9299
ConverseReply (backend + frontend)
monadierickx Apr 11, 2025
7766d73
removed tests from github actions
monadierickx Apr 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions .github/workflows/build_test_soundness.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: Build And Test on EC2

on:
push:
branches: ["main", "week12"]
pull_request:
branches: ["main"]
on: [push, pull_request]

jobs:
build:
Expand All @@ -22,20 +18,20 @@ jobs:
working-directory: backend
run: swift build

test:
runs-on: ubuntu-latest
container: swift:6.0.3-amazonlinux2
# test:
# runs-on: ubuntu-latest
# container: swift:6.0.3-amazonlinux2

steps:
# GitHub checkout action has a dep on NodeJS 20 which is not running on Amazonlinux2
# workaround is to manually checkout the repository
# https://github.com/actions/checkout/issues/1487
- name: Manually Clone repository
run: |
git clone https://github.com/${{ github.repository }} .
- name: Run tests
working-directory: backend
run: swift test
# steps:
# # GitHub checkout action has a dep on NodeJS 20 which is not running on Amazonlinux2
# # workaround is to manually checkout the repository
# # https://github.com/actions/checkout/issues/1487
# - name: Manually Clone repository
# run: |
# git clone https://github.com/${{ github.repository }} .
# - name: Run tests
# working-directory: backend
# run: swift test

soundness:
name: Soundness
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ xcuserdata
Package.resolved
.serverless
.vscode
.env

# backend
backend/.DS_Store
Expand Down
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@
"name": "Release SwiftBedrockService (backend)",
"program": "${workspaceFolder:swift-bedrock-playground}/backend/.build/release/SwiftBedrockService",
"preLaunchTask": "swift: Build Release SwiftBedrockService (backend)"
},
{
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:swift-fm-playground}/backend",
"name": "Debug PlaygroundAPI (backend)",
"program": "${workspaceFolder:swift-fm-playground}/backend/.build/debug/PlaygroundAPI",
"preLaunchTask": "swift: Build Debug PlaygroundAPI (backend)"
},
{
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:swift-fm-playground}/backend",
"name": "Release PlaygroundAPI (backend)",
"program": "${workspaceFolder:swift-fm-playground}/backend/.build/release/PlaygroundAPI",
"preLaunchTask": "swift: Build Release PlaygroundAPI (backend)"
}
]
}
159 changes: 33 additions & 126 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,126 +1,33 @@
# SwiftBedrockService

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

## How to add a new model family?

As an example we will add the Llama 3.1 70B Instruct model from the Meta family as an example.

"meta.llama3-70b-instruct-v1:0"

### 1. Add create BedrockModel instance

```swift
extension BedrockModel {
public static let llama3_70b_instruct: BedrockModel = BedrockModel(
id: "meta.llama3-70b-instruct-v1:0",
modality: LlamaText()
)
}
```

### 2. Create family-specific request and response struct

Make sure to create a struct that reflects exactly how the body of the request for an invokeModel call to this family should look. Make sure to add the public initializer with parameters `prompt`, `maxTokens` and `temperature` to comply to the `BedrockBodyCodable` protocol.

```json
{
"prompt": "\(prompt)",
"temperature": 1,
"top_p": 0.9,
"max_tokens": 200,
"stop": ["END"]
}
```

```swift
public struct LlamaRequestBody: BedrockBodyCodable {
let prompt: String
let max_gen_len: Int
let temperature: Double
let top_p: Double

public init(prompt: String, maxTokens: Int = 512, temperature: Double = 0.5) {
self.prompt =
"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\(prompt)<|eot_id|><|start_header_id|>assistant<|end_header_id|>"
self.max_gen_len = maxTokens
self.temperature = temperature
self.top_p = 0.9
}
}
```

Do the same for the response and ensure to add the `getTextCompletion` method to extract the completion from the response body and to comply to the `ContainsTextCompletion` protocol.

```json
{
"generation": "\n\n<response>",
"prompt_token_count": int,
"generation_token_count": int,
"stop_reason" : string
}
```

```swift
struct LlamaResponseBody: ContainsTextCompletion {
let generation: String
let prompt_token_count: Int
let generation_token_count: Int
let stop_reason: String

public func getTextCompletion() throws -> TextCompletion {
TextCompletion(generation)
}
}
```

### 3. Add the Modality (TextModality or ImageModality)

For a text generation create a struct conforming to TextModality. Use the request body and response body you created in [the previous step](#2-create-family-specific-request-and-response-struct).

```swift
struct LlamaText: TextModality {
func getName() -> String { "Llama Text Generation" }

func getTextRequestBody(prompt: String, maxTokens: Int, temperature: Double) throws -> BedrockBodyCodable {
LlamaRequestBody(prompt: prompt, maxTokens: maxTokens, temperature: temperature)
}

func getTextResponseBody(from data: Data) throws -> ContainsTextCompletion {
let decoder = JSONDecoder()
return try decoder.decode(LlamaResponseBody.self, from: data)
}
}
```

### 4. Optionally you can create a BedrockModel initializer for your newly implemented models
```swift
extension BedrockModel {
init?(_ id: String) {
switch id {
case "meta.llama3-70b-instruct-v1:0": self = .llama3_70b_instruct
// ...
default:
return nil
}
}
}
```


## How to add a new model?

If you want to add a model that has a request and response structure that is already implemented you can skip a few steps. Simply create a typealias for the Modality that matches the structure and use it to create a BedrockModel instance.

```swift
typealias ClaudeNewModel = AnthropicText

extension BedrockModel {
public static let instant: BedrockModel = BedrockModel(
id: "anthropic.claude-new-model",
modality: ClaudeNewModel()
)
}
```

Note that the model will not automatically be included in the BedrockModel initializer that creates an instance from a raw string value. Consider creating a custom initializer that includes your models.
# Swift FM Playground

Welcome to the Swift Foundation Model (FM) Playground, an example app to explore how to use **Amazon Bedrock** with the AWS SDK for Swift.

> 🚨 **Important:** This application is for educational purposes and not intended for production use.

## Overview

> 🚧 Under construction 🚧

## Prerequisites

> 🚧 Under construction 🚧

## Running the Application

> 🚧 Under construction 🚧

## Accessing the Application

To access the application, open `http://localhost:3000` in your web browser.

## Stopping the Application

To halt the application, you will need to stop both the backend and frontend processes.

### Stopping the Frontend

In the terminal where the frontend is running, press `Ctrl + C` to terminate the process.

### Stopping the Backend

Similarly, in the backend terminal, use the `Ctrl + C` shortcut to stop the server.
51 changes: 8 additions & 43 deletions backend/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,25 @@
import PackageDescription

let package = Package(
name: "HummingbirdBackend", // FIXME: better name
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
name: "SwiftBedrock",
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
products: [
.executable(name: "App", targets: ["App"]) // FIXME: better name
.executable(name: "PlaygroundAPI", targets: ["PlaygroundAPI"])
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.45"),
.package(url: "https://github.com/smithy-lang/smithy-swift", from: "0.118.0"),
.package(url: "https://github.com/swiftlang/swift-testing", branch: "main"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
.package(url: "https://github.com/monadierickx/swift-bedrock-library.git", branch: "week16"),
],
targets: [
.executableTarget(
name: "App",
name: "PlaygroundAPI",
dependencies: [
.target(name: "BedrockService"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "BedrockService", package: "swift-bedrock-library"),
],
path: "Sources/App"
),
.target(
name: "BedrockService",
dependencies: [
.target(name: "BedrockTypes"),
.product(name: "AWSClientRuntime", package: "aws-sdk-swift"),
.product(name: "AWSBedrock", package: "aws-sdk-swift"),
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
.product(name: "Smithy", package: "smithy-swift"),
.product(name: "Logging", package: "swift-log"),
],
path: "Sources/BedrockService"
),
.target(
name: "BedrockTypes",
path: "Sources/BedrockTypes"
),
.testTarget(
name: "BedrockServiceTests",
dependencies: [
.target(name: "BedrockService"),
.product(name: "Testing", package: "swift-testing"),
],
path: "Tests/BedrockServiceTests"
),
// .testTarget(name: "AppTests",
// dependencies: [
// .byName(name: "App"),
// .target(name: "SwiftBedrockService"),
// .product(name: "HummingbirdTesting", package: "hummingbird")
// ],
// path: "Tests/AppTests"
// )
path: "Sources/PlaygroundAPI"
)
]
)
88 changes: 0 additions & 88 deletions backend/Sources/BedrockService/Converse/ConverseRequest.swift

This file was deleted.

Loading