Skip to content

Commit b180795

Browse files
authored
Merge pull request #9 from monadierickx/week16
Week16 - part 1
2 parents 63fb1e0 + 7766d73 commit b180795

File tree

74 files changed

+1112
-5964
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1112
-5964
lines changed

.github/workflows/build_test_soundness.yml

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
name: Build And Test on EC2
22

3-
on:
4-
push:
5-
branches: ["main", "week12"]
6-
pull_request:
7-
branches: ["main"]
3+
on: [push, pull_request]
84

95
jobs:
106
build:
@@ -22,20 +18,20 @@ jobs:
2218
working-directory: backend
2319
run: swift build
2420

25-
test:
26-
runs-on: ubuntu-latest
27-
container: swift:6.0.3-amazonlinux2
21+
# test:
22+
# runs-on: ubuntu-latest
23+
# container: swift:6.0.3-amazonlinux2
2824

29-
steps:
30-
# GitHub checkout action has a dep on NodeJS 20 which is not running on Amazonlinux2
31-
# workaround is to manually checkout the repository
32-
# https://github.com/actions/checkout/issues/1487
33-
- name: Manually Clone repository
34-
run: |
35-
git clone https://github.com/${{ github.repository }} .
36-
- name: Run tests
37-
working-directory: backend
38-
run: swift test
25+
# steps:
26+
# # GitHub checkout action has a dep on NodeJS 20 which is not running on Amazonlinux2
27+
# # workaround is to manually checkout the repository
28+
# # https://github.com/actions/checkout/issues/1487
29+
# - name: Manually Clone repository
30+
# run: |
31+
# git clone https://github.com/${{ github.repository }} .
32+
# - name: Run tests
33+
# working-directory: backend
34+
# run: swift test
3935

4036
soundness:
4137
name: Soundness

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ xcuserdata
1111
Package.resolved
1212
.serverless
1313
.vscode
14+
.env
1415

1516
# backend
1617
backend/.DS_Store

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@
7171
"name": "Release SwiftBedrockService (backend)",
7272
"program": "${workspaceFolder:swift-bedrock-playground}/backend/.build/release/SwiftBedrockService",
7373
"preLaunchTask": "swift: Build Release SwiftBedrockService (backend)"
74+
},
75+
{
76+
"type": "lldb",
77+
"request": "launch",
78+
"args": [],
79+
"cwd": "${workspaceFolder:swift-fm-playground}/backend",
80+
"name": "Debug PlaygroundAPI (backend)",
81+
"program": "${workspaceFolder:swift-fm-playground}/backend/.build/debug/PlaygroundAPI",
82+
"preLaunchTask": "swift: Build Debug PlaygroundAPI (backend)"
83+
},
84+
{
85+
"type": "lldb",
86+
"request": "launch",
87+
"args": [],
88+
"cwd": "${workspaceFolder:swift-fm-playground}/backend",
89+
"name": "Release PlaygroundAPI (backend)",
90+
"program": "${workspaceFolder:swift-fm-playground}/backend/.build/release/PlaygroundAPI",
91+
"preLaunchTask": "swift: Build Release PlaygroundAPI (backend)"
7492
}
7593
]
7694
}

README.md

Lines changed: 33 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,33 @@
1-
# SwiftBedrockService
2-
3-
Work in progress, feel free to open issue, but do not use in your projects.
4-
5-
## How to add a new model family?
6-
7-
As an example we will add the Llama 3.1 70B Instruct model from the Meta family as an example.
8-
9-
"meta.llama3-70b-instruct-v1:0"
10-
11-
### 1. Add create BedrockModel instance
12-
13-
```swift
14-
extension BedrockModel {
15-
public static let llama3_70b_instruct: BedrockModel = BedrockModel(
16-
id: "meta.llama3-70b-instruct-v1:0",
17-
modality: LlamaText()
18-
)
19-
}
20-
```
21-
22-
### 2. Create family-specific request and response struct
23-
24-
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.
25-
26-
```json
27-
{
28-
"prompt": "\(prompt)",
29-
"temperature": 1,
30-
"top_p": 0.9,
31-
"max_tokens": 200,
32-
"stop": ["END"]
33-
}
34-
```
35-
36-
```swift
37-
public struct LlamaRequestBody: BedrockBodyCodable {
38-
let prompt: String
39-
let max_gen_len: Int
40-
let temperature: Double
41-
let top_p: Double
42-
43-
public init(prompt: String, maxTokens: Int = 512, temperature: Double = 0.5) {
44-
self.prompt =
45-
"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\(prompt)<|eot_id|><|start_header_id|>assistant<|end_header_id|>"
46-
self.max_gen_len = maxTokens
47-
self.temperature = temperature
48-
self.top_p = 0.9
49-
}
50-
}
51-
```
52-
53-
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.
54-
55-
```json
56-
{
57-
"generation": "\n\n<response>",
58-
"prompt_token_count": int,
59-
"generation_token_count": int,
60-
"stop_reason" : string
61-
}
62-
```
63-
64-
```swift
65-
struct LlamaResponseBody: ContainsTextCompletion {
66-
let generation: String
67-
let prompt_token_count: Int
68-
let generation_token_count: Int
69-
let stop_reason: String
70-
71-
public func getTextCompletion() throws -> TextCompletion {
72-
TextCompletion(generation)
73-
}
74-
}
75-
```
76-
77-
### 3. Add the Modality (TextModality or ImageModality)
78-
79-
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).
80-
81-
```swift
82-
struct LlamaText: TextModality {
83-
func getName() -> String { "Llama Text Generation" }
84-
85-
func getTextRequestBody(prompt: String, maxTokens: Int, temperature: Double) throws -> BedrockBodyCodable {
86-
LlamaRequestBody(prompt: prompt, maxTokens: maxTokens, temperature: temperature)
87-
}
88-
89-
func getTextResponseBody(from data: Data) throws -> ContainsTextCompletion {
90-
let decoder = JSONDecoder()
91-
return try decoder.decode(LlamaResponseBody.self, from: data)
92-
}
93-
}
94-
```
95-
96-
### 4. Optionally you can create a BedrockModel initializer for your newly implemented models
97-
```swift
98-
extension BedrockModel {
99-
init?(_ id: String) {
100-
switch id {
101-
case "meta.llama3-70b-instruct-v1:0": self = .llama3_70b_instruct
102-
// ...
103-
default:
104-
return nil
105-
}
106-
}
107-
}
108-
```
109-
110-
111-
## How to add a new model?
112-
113-
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.
114-
115-
```swift
116-
typealias ClaudeNewModel = AnthropicText
117-
118-
extension BedrockModel {
119-
public static let instant: BedrockModel = BedrockModel(
120-
id: "anthropic.claude-new-model",
121-
modality: ClaudeNewModel()
122-
)
123-
}
124-
```
125-
126-
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.
1+
# Swift FM Playground
2+
3+
Welcome to the Swift Foundation Model (FM) Playground, an example app to explore how to use **Amazon Bedrock** with the AWS SDK for Swift.
4+
5+
> 🚨 **Important:** This application is for educational purposes and not intended for production use.
6+
7+
## Overview
8+
9+
> 🚧 Under construction 🚧
10+
11+
## Prerequisites
12+
13+
> 🚧 Under construction 🚧
14+
15+
## Running the Application
16+
17+
> 🚧 Under construction 🚧
18+
19+
## Accessing the Application
20+
21+
To access the application, open `http://localhost:3000` in your web browser.
22+
23+
## Stopping the Application
24+
25+
To halt the application, you will need to stop both the backend and frontend processes.
26+
27+
### Stopping the Frontend
28+
29+
In the terminal where the frontend is running, press `Ctrl + C` to terminate the process.
30+
31+
### Stopping the Backend
32+
33+
Similarly, in the backend terminal, use the `Ctrl + C` shortcut to stop the server.

backend/Package.swift

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,25 @@
44
import PackageDescription
55

66
let package = Package(
7-
name: "HummingbirdBackend", // FIXME: better name
8-
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
7+
name: "SwiftBedrock",
8+
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
99
products: [
10-
.executable(name: "App", targets: ["App"]) // FIXME: better name
10+
.executable(name: "PlaygroundAPI", targets: ["PlaygroundAPI"])
1111
],
1212
dependencies: [
1313
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
1414
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
15-
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.45"),
16-
.package(url: "https://github.com/smithy-lang/smithy-swift", from: "0.118.0"),
17-
.package(url: "https://github.com/swiftlang/swift-testing", branch: "main"),
18-
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
15+
.package(url: "https://github.com/monadierickx/swift-bedrock-library.git", branch: "week16"),
1916
],
2017
targets: [
2118
.executableTarget(
22-
name: "App",
19+
name: "PlaygroundAPI",
2320
dependencies: [
24-
.target(name: "BedrockService"),
2521
.product(name: "ArgumentParser", package: "swift-argument-parser"),
2622
.product(name: "Hummingbird", package: "hummingbird"),
23+
.product(name: "BedrockService", package: "swift-bedrock-library"),
2724
],
28-
path: "Sources/App"
29-
),
30-
.target(
31-
name: "BedrockService",
32-
dependencies: [
33-
.target(name: "BedrockTypes"),
34-
.product(name: "AWSClientRuntime", package: "aws-sdk-swift"),
35-
.product(name: "AWSBedrock", package: "aws-sdk-swift"),
36-
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
37-
.product(name: "Smithy", package: "smithy-swift"),
38-
.product(name: "Logging", package: "swift-log"),
39-
],
40-
path: "Sources/BedrockService"
41-
),
42-
.target(
43-
name: "BedrockTypes",
44-
path: "Sources/BedrockTypes"
45-
),
46-
.testTarget(
47-
name: "BedrockServiceTests",
48-
dependencies: [
49-
.target(name: "BedrockService"),
50-
.product(name: "Testing", package: "swift-testing"),
51-
],
52-
path: "Tests/BedrockServiceTests"
53-
),
54-
// .testTarget(name: "AppTests",
55-
// dependencies: [
56-
// .byName(name: "App"),
57-
// .target(name: "SwiftBedrockService"),
58-
// .product(name: "HummingbirdTesting", package: "hummingbird")
59-
// ],
60-
// path: "Tests/AppTests"
61-
// )
25+
path: "Sources/PlaygroundAPI"
26+
)
6227
]
6328
)

backend/Sources/BedrockService/Converse/ConverseRequest.swift

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)