Skip to content

Commit

Permalink
JavaScript v3: Add code examples to demonstrate the InvokeModel actio…
Browse files Browse the repository at this point in the history
…ns with multiple foundation models (#5930)



Co-authored-by: Dennis Traub <[email protected]>
  • Loading branch information
DennisTraub and DennisTraub authored Jan 19, 2024
1 parent e74e09e commit ad65925
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 1 deletion.
17 changes: 16 additions & 1 deletion .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ bedrock-runtime_InvokeClaude:
title: Invoke the Anthropic Claude 2 model on &BR; for text generation
title_abbrev: Text generation with Anthropic Claude 2
synopsis: invoke the Anthropic Claude 2 model on &BR; for text generation.
category:
languages:
Go:
versions:
Expand All @@ -129,6 +128,14 @@ bedrock-runtime_InvokeClaude:
- description: Invoke the Anthropic Claude 2 foundation model to generate text.
snippet_tags:
- bedrock-runtime.java2.invoke_claude.main
JavaScript:
versions:
- sdk_version: 3
github: javascriptv3/example_code/bedrock-runtime
excerpts:
- description: Invoke the Anthropic Claude 2 foundation model to generate text.
snippet_files:
- javascriptv3/example_code/bedrock-runtime/actions/invoke-claude.js
PHP:
versions:
- sdk_version: 3
Expand Down Expand Up @@ -190,6 +197,14 @@ bedrock-runtime_InvokeJurassic2:
- description: Invoke the AI21 Labs Jurassic-2 foundation model to generate text.
snippet_tags:
- bedrock-runtime.java2.invoke_jurassic2.main
JavaScript:
versions:
- sdk_version: 3
github: javascriptv3/example_code/bedrock-runtime
excerpts:
- description: Invoke the AI21 Labs Jurassic-2 foundation model to generate text.
snippet_files:
- javascriptv3/example_code/bedrock-runtime/actions/invoke-jurassic2.js
PHP:
versions:
- sdk_version: 3
Expand Down
94 changes: 94 additions & 0 deletions javascriptv3/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Amazon Bedrock Runtime code examples for the SDK for JavaScript (v3)

## Overview

Shows how to use the AWS SDK for JavaScript (v3) to work with Amazon Bedrock Runtime.

<!--custom.overview.start-->
<!--custom.overview.end-->

_Amazon Bedrock Runtime is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon._

## ⚠ Important

* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../../README.md#Prerequisites) in the `javascriptv3` folder.


<!--custom.prerequisites.start-->
> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).
<!--custom.prerequisites.end-->
### Single actions

Code excerpts that show you how to call individual service functions.

- [Text generation with AI21 Labs Jurassic-2](javascriptv3/example_code/bedrock-runtime/actions/invoke-jurassic2.js) (`InvokeModel`)
- [Text generation with Anthropic Claude 2](javascriptv3/example_code/bedrock-runtime/actions/invoke-claude.js) (`InvokeModel`)


<!--custom.examples.start-->
<!--custom.examples.end-->

## Run the examples

### Instructions

**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).

**Run a single action**

```bash
node ./actions/<fileName>
```

**Run a scenario**
Most scenarios can be run with the following command:
```bash
node ./scenarios/<fileName>
```

<!--custom.instructions.start-->
<!--custom.instructions.end-->



### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../../README.md#Tests)
in the `javascriptv3` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

- [Amazon Bedrock Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
- [Amazon Bedrock Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
- [SDK for JavaScript (v3) Amazon Bedrock Runtime reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
70 changes: 70 additions & 0 deletions javascriptv3/example_code/bedrock-runtime/actions/invoke-claude.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {fileURLToPath} from "url";

import {BedrockRuntimeClient, InvokeModelCommand} from "@aws-sdk/client-bedrock-runtime";

/**
* @typedef {Object} ResponseBody
* @property {string} completion
*/

/**
* Invokes the Anthropic Claude 2 model to run an inference using the input
* provided in the request body.
*
* @param {string} prompt - The prompt that you want Claude to complete.
* @returns {string} The inference response (completion) from the model.
*/
export const invokeClaude = async (prompt) => {
const client = new BedrockRuntimeClient( { region: 'us-east-1' } );

const modelId = 'anthropic.claude-v2';

/* Claude requires you to enclose the prompt as follows: */
const enclosedPrompt = `Human: ${prompt}\n\nAssistant:`;

/* The different model providers have individual request and response formats.
* For the format, ranges, and default values for Anthropic Claude, refer to:
* https://docs.anthropic.com/claude/reference/complete_post
*/
const payload = {
prompt: enclosedPrompt,
max_tokens_to_sample: 500,
temperature: 0.5,
stop_sequences: [ '\n\nHuman:' ],
};

const command = new InvokeModelCommand({
body: JSON.stringify(payload),
contentType: 'application/json',
accept: 'application/json',
modelId,
});

try {
const response = await client.send(command);
const decodedResponseBody = new TextDecoder().decode(response.body);

/** @type {ResponseBody} */
const responseBody = JSON.parse(decodedResponseBody);

return responseBody.completion;

} catch (err) {
console.error(err);
}
};

// Invoke the function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const prompt = 'Complete the following: "Once upon a time..."';
console.log('\nModel: Anthropic Claude v2');
console.log(`Prompt: ${prompt}`);

const completion = await invokeClaude(prompt);
console.log('Completion:');
console.log(completion);
console.log('\n');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {fileURLToPath} from "url";

import {BedrockRuntimeClient, InvokeModelCommand} from "@aws-sdk/client-bedrock-runtime";

/**
* @typedef {Object} Data
* @property {string} text
*
* @typedef {Object} Completion
* @property {Data} data
*
* @typedef {Object} ResponseBody
* @property {Completion[]} completions
*/

/**
* Invokes the AI21 Labs Jurassic-2 large-language model to run an inference
* using the input provided in the request body.
*
* @param {string} prompt - The prompt that you want Jurassic-2 to complete.
* @returns {string} The inference response (completion) from the model.
*/
export const invokeJurassic2 = async (prompt) => {
const client = new BedrockRuntimeClient( { region: 'us-east-1' } );

const modelId = 'ai21.j2-mid-v1';

/* The different model providers have individual request and response formats.
* For the format, ranges, and default values for AI21 Labs Jurassic-2, refer to:
* https://docs.ai21.com/reference/j2-complete-ref
*/
const payload = {
prompt,
maxTokens: 500,
temperature: 0.5,
};

const command = new InvokeModelCommand({
body: JSON.stringify(payload),
contentType: 'application/json',
accept: 'application/json',
modelId,
});

try {
const response = await client.send(command);
const decodedResponseBody = new TextDecoder().decode(response.body);

/** @type {ResponseBody} */
const responseBody = JSON.parse(decodedResponseBody);

return responseBody.completions[0].data.text;

} catch (err) {
console.error(err);
}
};

// Invoke the function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const prompt = 'Complete the following: "Once upon a time..."';
console.log('\nModel: AI21 Labs Jurassic-2');
console.log(`Prompt: ${prompt}`);

const completion = await invokeJurassic2(prompt);
console.log('Completion:');
console.log(completion);
console.log('\n');
}
16 changes: 16 additions & 0 deletions javascriptv3/example_code/bedrock-runtime/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "bedrock-runtime-examples",
"version": "1.0.0",
"author": "Dennis Traub <[email protected]>",
"license": "Apache-2.0",
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
},
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.489.0"
},
"devDependencies": {
"vitest": "^1.1.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, it, expect } from "vitest";

import { invokeClaude } from '../actions/invoke-claude.js';
import { invokeJurassic2 } from '../actions/invoke-jurassic2.js';

const TEST_PROMPT = 'Hello, this is a test prompt';

describe('invoke claude with test prompt', () => {
it('should return a text completion', async () => {
const response = await invokeClaude(TEST_PROMPT);
expect(typeof response).toBe('string');
expect(response).not.toBe('');
})
})

describe('invoke jurassic-2 with test prompt', () => {
it('should return a text completion', async () => {
const response = await invokeJurassic2(TEST_PROMPT);
expect(typeof response).toBe('string');
expect(response).not.toBe('');
})
})
11 changes: 11 additions & 0 deletions javascriptv3/example_code/bedrock-runtime/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
testTimeout: 50000,
threads: false,
},
});
1 change: 1 addition & 0 deletions javascriptv3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"workspaces": [
"example_code/auto-scaling",
"example_code/bedrock",
"example_code/bedrock-runtime",
"example_code/cloudwatch",
"example_code/cloudwatch-events",
"example_code/cloudwatch-logs",
Expand Down

0 comments on commit ad65925

Please sign in to comment.