Skip to content

Commit

Permalink
chore(bedrock): replacing BEDROCK_DEV with isTestEnvironment (#47)
Browse files Browse the repository at this point in the history
* chore(bedrock): replacing BEDROCK_DEV with isTestEnvironment

* chore(bedrock): replacing BEDROCK_DEV with isTestEnvironment

* Update bedrock/README.md

* Update bedrock/bedrock.w

* Update bedrock/joke.test.w
  • Loading branch information
ekeren authored Jan 2, 2024
1 parent ada64e4 commit aa8afc4
Show file tree
Hide file tree
Showing 5 changed files with 479 additions and 460 deletions.
7 changes: 4 additions & 3 deletions bedrock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ pub class JokeMaker {
}
```

## Development
## Development & Testing

When running in the simulator, set `BEDROCK_DEV=1` to send requests to Amazon Bedrock instead of
replying with a mock response.
When running in simulator using `wing run`, request are sent to Amazon Bedrock.
When running tests using `wing test` or by running tests from within Wing Console, requests are
handled by the mocked service.

## Maintainers

Expand Down
13 changes: 9 additions & 4 deletions bedrock/bedrock.w
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ pub class Model impl a.IModel {
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? {
if target == "sim" {
if std.Node.of(this).app.isTestEnvironment {
// in case of test running on sim, use simulator version
this.inner = new s.Model_sim(modelId) as "sim";
} else {
// in case of running on sim interactively (in development mode), use AWS version
this.inner = new t.Model_tfaws(modelId) as "tf-aws";
}
} elif target == "tf-aws" {
this.inner = new t.Model_tfaws(modelId) as "tf-aws";
} else {
throw "Unsupported target {target}";
Expand Down
4 changes: 2 additions & 2 deletions bedrock/joke.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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")? {
// this will only kick in if is in sim and test mode
if util.env("WING_TARGET") == "sim" && std.Node.of(joke).app.isTestEnvironment {
let model = std.Node.of(joke).findChild("claude").node.findChild("sim");
let sim: s.Model_sim = unsafeCast(model);
sim.setMockResponse(inflight (body) => {
Expand Down
Loading

0 comments on commit aa8afc4

Please sign in to comment.