-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bedrock): introduce bedrock (#20)
- Loading branch information
Showing
15 changed files
with
1,532 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: bedrock-pull | ||
on: | ||
pull_request: | ||
paths: | ||
- bedrock/** | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
sparse-checkout: bedrock | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18.x | ||
registry-url: https://registry.npmjs.org | ||
- name: Install winglang | ||
run: npm i -g winglang | ||
- name: Install dependencies | ||
run: npm install --include=dev | ||
working-directory: bedrock | ||
- name: Test | ||
run: wing test | ||
working-directory: bedrock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: bedrock-release | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- bedrock/** | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
sparse-checkout: bedrock | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18.x | ||
registry-url: https://registry.npmjs.org | ||
- name: Install winglang | ||
run: npm i -g winglang | ||
- name: Install dependencies | ||
run: npm install --include=dev | ||
working-directory: bedrock | ||
- name: Test | ||
run: wing test | ||
working-directory: bedrock | ||
- name: Pack | ||
run: wing pack | ||
working-directory: bedrock | ||
- name: Publish | ||
run: npm publish --access=public --registry https://registry.npmjs.org --tag | ||
latest *.tgz | ||
working-directory: bedrock | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Wing | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# bedrock | ||
|
||
A Wing library for working with [Amazon Bedrock](https://aws.amazon.com/bedrock/). | ||
|
||
## Prerequisites | ||
|
||
* [winglang](https://winglang.io). | ||
|
||
## Installation | ||
|
||
`sh | ||
npm i @winglibs/bedrock | ||
` | ||
|
||
## Usage | ||
|
||
```js | ||
bring bedrock; | ||
|
||
pub class JokeMaker { | ||
claud: bedrock.Model; | ||
|
||
new() { | ||
this.claud = new bedrock.Model("anthropic.claude-v2") as "claude"; | ||
} | ||
|
||
pub inflight makeJoke(topic: str): str { | ||
let res = this.claud.invoke({ | ||
prompt: "\n\nHuman: Tell me a joke about {topic}\n\nAssistant:", | ||
max_tokens_to_sample: 300, | ||
temperature: 0.5, | ||
top_k: 250, | ||
top_p: 1, | ||
stop_sequences: [ | ||
"\n\nHuman:" | ||
], | ||
anthropic_version: "bedrock-2023-05-31" | ||
}); | ||
|
||
return res.get("completion").asStr(); | ||
} | ||
} | ||
``` | ||
|
||
## Development | ||
|
||
When running in the simulator, set `BEDROCK_DEV=1` to send requests to Amazon Bedrock instead of | ||
replying with a mock response. | ||
|
||
## Maintainers | ||
|
||
[@eladb](https://github.com/eladb), [@ekeren](https://github.com/ekeren) | ||
|
||
## License | ||
|
||
This library is licensed under the [MIT License](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
pub interface IModel { | ||
inflight invoke(body: Json): Json; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime"; | ||
|
||
exports._invokeModel = async ({ modelId, body }) => { | ||
const client = new BedrockRuntimeClient(); | ||
let command = new InvokeModelCommand({ | ||
modelId, | ||
body: JSON.stringify(body), | ||
contentType: "application/json", | ||
accept: "application/json", | ||
}); | ||
|
||
const response = await client.send(command); | ||
return JSON.parse(response.body.transformToString()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
bring "./api.w" as b; | ||
|
||
pub class Model_sim impl b.IModel { | ||
modelId: str; | ||
var mockResponse: inflight (Json): Json; | ||
|
||
new(modelId: str) { | ||
this.modelId = modelId; | ||
this.mockResponse = inflight (body) => { | ||
return { | ||
response: "We are living in a simulation", | ||
model: this.modelId, | ||
request: body, | ||
}; | ||
}; | ||
} | ||
|
||
pub setMockResponse(fn: inflight (Json): Json) { | ||
this.mockResponse = fn; | ||
} | ||
|
||
pub inflight invoke(body: Json): Json { | ||
return this.mockResponse(body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
bring expect; | ||
bring "./bedrock.w" as bedrock; | ||
|
||
let titan = new bedrock.Model("amazon.titan-text-lite-v1") as "titan"; | ||
let claude = new bedrock.Model("anthropic.claude-v2") as "claude"; | ||
|
||
test "titan" { | ||
let res = titan.invoke({ | ||
inputText: "what is a joke?", | ||
textGenerationConfig: { | ||
maxTokenCount: 4096, | ||
stopSequences: [], | ||
temperature: 0, | ||
topP: 1 | ||
} | ||
}); | ||
|
||
log(Json.stringify(res)); | ||
} | ||
|
||
test "claude" { | ||
let res = claude.invoke({ | ||
prompt: "\n\nHuman: Hello universe\n\nAssistant:", | ||
max_tokens_to_sample: 300, | ||
temperature: 0.5, | ||
top_k: 250, | ||
top_p: 1, | ||
stop_sequences: [ | ||
"\n\nHuman:" | ||
], | ||
anthropic_version: "bedrock-2023-05-31" | ||
}); | ||
|
||
log(Json.stringify(res)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
bring "./api.w" as b; | ||
bring aws; | ||
|
||
pub class Model_tfaws impl b.IModel { | ||
pub modelId: str; | ||
|
||
new(modelId: str) { | ||
this.modelId = modelId; | ||
} | ||
|
||
pub inflight invoke(body: Json): Json { | ||
return Model_tfaws._invokeModel({ modelId: this.modelId, body: body }); | ||
} | ||
|
||
pub onLift(host: std.IInflightHost, ops: Array<str>) { | ||
if let lambda = aws.Function.from(host) { | ||
lambda.addPolicyStatements({ | ||
actions: ["bedrock:InvokeModel"], | ||
effect: aws.Effect.ALLOW, | ||
resources: [ | ||
"arn:aws:bedrock:*::foundation-model/{this.modelId}" | ||
] | ||
}); | ||
} | ||
} | ||
|
||
extern "./bedrock.js" | ||
static inflight _invokeModel(req: Json): Json; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
bring aws; | ||
bring util; | ||
bring "./api.w" as a; | ||
bring "./bedrock.sim.w" as s; | ||
bring "./bedrock.tfaws.w" as t; | ||
|
||
pub class Model impl a.IModel { | ||
pub modelId: str; | ||
|
||
inner: a.IModel; | ||
|
||
new(modelId: str) { | ||
this.modelId = modelId; | ||
|
||
let target = util.env("WING_TARGET"); | ||
let dev = util.tryEnv("BEDROCK_DEV"); | ||
if target == "sim" && !dev? { | ||
this.inner = new s.Model_sim(modelId) as "sim"; | ||
} elif target == "tf-aws" || dev? { | ||
this.inner = new t.Model_tfaws(modelId) as "tf-aws"; | ||
} else { | ||
throw "Unsupported target {target}"; | ||
} | ||
} | ||
|
||
pub inflight invoke(body: Json): Json { | ||
return this.inner.invoke(body); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
bring "./joke.w" as j; | ||
bring "./bedrock.sim.w" as s; | ||
bring util; | ||
|
||
let joke = new j.JokeMaker(); | ||
|
||
// an experimental pattern for mocking responses | ||
// this will only kick in if `DEV=1` is not set | ||
if util.env("WING_TARGET") == "sim" && !util.tryEnv("BEDROCK_DEV")? { | ||
let model = std.Node.of(joke).findChild("claude").node.findChild("sim"); | ||
let sim: s.Model_sim = unsafeCast(model); | ||
sim.setMockResponse(inflight (body) => { | ||
return { | ||
completion: " Here's a silly joke about oranges:\n\nHow does an orange ask another orange to dance?\n\"Hey orange, want to grab a peeling and tango?\"", | ||
stop_reason: "stop_sequence", | ||
stop: "\n\nHuman:" | ||
}; | ||
}); | ||
} | ||
|
||
test "make a joke" { | ||
let res = joke.makeJoke("oranges"); | ||
log(res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
bring "./bedrock.w" as bedrock; | ||
|
||
pub class JokeMaker { | ||
claud: bedrock.Model; | ||
|
||
new() { | ||
this.claud = new bedrock.Model("anthropic.claude-v2") as "claude"; | ||
} | ||
|
||
pub inflight makeJoke(topic: str): str { | ||
let res = this.claud.invoke({ | ||
prompt: "\n\nHuman: Tell me a joke about {topic}\n\nAssistant:", | ||
max_tokens_to_sample: 300, | ||
temperature: 0.5, | ||
top_k: 250, | ||
top_p: 1, | ||
stop_sequences: [ | ||
"\n\nHuman:" | ||
], | ||
anthropic_version: "bedrock-2023-05-31" | ||
}); | ||
|
||
return res.get("completion").asStr(); | ||
} | ||
} |
Oops, something went wrong.