Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bedrock): introduce bedrock #20

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions .github/workflows/bedrock-pull.yaml
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
37 changes: 37 additions & 0 deletions .github/workflows/bedrock-release.yaml
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 }}
2 changes: 2 additions & 0 deletions bedrock/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions bedrock/LICENSE
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.
56 changes: 56 additions & 0 deletions bedrock/README.md
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).
4 changes: 4 additions & 0 deletions bedrock/api.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

pub interface IModel {
inflight invoke(body: Json): Json;
}
14 changes: 14 additions & 0 deletions bedrock/bedrock.js
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());
};
25 changes: 25 additions & 0 deletions bedrock/bedrock.sim.w
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);
}
}
35 changes: 35 additions & 0 deletions bedrock/bedrock.test.w
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));
}
29 changes: 29 additions & 0 deletions bedrock/bedrock.tfaws.w
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;
}
30 changes: 30 additions & 0 deletions bedrock/bedrock.w
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);
}
}

24 changes: 24 additions & 0 deletions bedrock/joke.test.w
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);
}
25 changes: 25 additions & 0 deletions bedrock/joke.w
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();
}
}
Loading