-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaScript v3: Add code examples to demonstrate the InvokeModel actio…
…ns with multiple foundation models (#5930) Co-authored-by: Dennis Traub <[email protected]>
- Loading branch information
1 parent
e74e09e
commit ad65925
Showing
8 changed files
with
305 additions
and
1 deletion.
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
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,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
70
javascriptv3/example_code/bedrock-runtime/actions/invoke-claude.js
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,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'); | ||
} |
72 changes: 72 additions & 0 deletions
72
javascriptv3/example_code/bedrock-runtime/actions/invoke-jurassic2.js
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,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'); | ||
} |
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,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" | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
javascriptv3/example_code/bedrock-runtime/tests/invoke-model.integration.test.js
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 @@ | ||
// 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(''); | ||
}) | ||
}) |
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,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, | ||
}, | ||
}); |
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