-
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.
- Loading branch information
Showing
16 changed files
with
345 additions
and
12 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
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
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
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,106 @@ | ||
# Amazon Bedrock code examples for the SDK for JavaScript (v3) | ||
|
||
## Overview | ||
|
||
Shows how to use the AWS SDK for JavaScript (v3) to work with Amazon Bedrock. | ||
|
||
<!--custom.overview.start--> | ||
<!--custom.overview.end--> | ||
|
||
_Amazon Bedrock enables you to build and scale generative AI applications with foundation models._ | ||
|
||
## ⚠ 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 foundation model before you can use it. If you try to use the foundation model 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--> | ||
### Get started | ||
|
||
- [Hello Amazon Bedrock](javascriptv3/example_code/bedrock/hello.js) (`ListFoundationModels`) | ||
|
||
|
||
### Single actions | ||
|
||
Code excerpts that show you how to call individual service functions. | ||
|
||
- [Get details about an Amazon Bedrock foundation model](javascriptv3/example_code/bedrock/actions/get-foundation-model.js) (`GetFoundationModel`) | ||
- [List available Amazon Bedrock foundation models](javascriptv3/example_code/bedrock/actions/list-foundation-models.js) (`ListFoundationModels`) | ||
|
||
|
||
<!--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--> | ||
|
||
#### Hello Amazon Bedrock | ||
|
||
This example shows you how to get started using Amazon Bedrock. | ||
|
||
```bash | ||
node ./hello.js | ||
``` | ||
|
||
|
||
### 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 User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) | ||
- [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) | ||
- [SDK for JavaScript (v3) Amazon Bedrock reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock) | ||
|
||
<!--custom.resources.start--> | ||
<!--custom.resources.end--> | ||
|
||
--- | ||
|
||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: Apache-2.0 |
31 changes: 31 additions & 0 deletions
31
javascriptv3/example_code/bedrock/actions/get-foundation-model.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,31 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { fileURLToPath } from 'url'; | ||
|
||
import { BedrockClient, GetFoundationModelCommand } from '@aws-sdk/client-bedrock'; | ||
|
||
/** | ||
* Get details about an Amazon Bedrock foundation model. | ||
* | ||
* @return {FoundationModelDetails} - The list of available bedrock foundation models. | ||
*/ | ||
export const getFoundationModel = async () => { | ||
const client = new BedrockClient(); | ||
|
||
const command = new GetFoundationModelCommand({ | ||
modelIdentifier: 'amazon.titan-embed-text-v1' | ||
}); | ||
|
||
const response = await client.send(command); | ||
|
||
return response.modelDetails; | ||
}; | ||
|
||
// Invoke main function if this file was run directly. | ||
if (process.argv[1] === fileURLToPath(import.meta.url)) { | ||
const model = await getFoundationModel(); | ||
console.log(model); | ||
} |
36 changes: 36 additions & 0 deletions
36
javascriptv3/example_code/bedrock/actions/list-foundation-models.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,36 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { fileURLToPath } from 'url'; | ||
|
||
import { BedrockClient, ListFoundationModelsCommand } from '@aws-sdk/client-bedrock'; | ||
|
||
/** | ||
* List the available Amazon Bedrock foundation models. | ||
* | ||
* @return {FoundationModelSummary[]} - The list of available bedrock foundation models. | ||
*/ | ||
export const listFoundationModels = async () => { | ||
const client = new BedrockClient(); | ||
|
||
const input = { | ||
// byProvider: 'STRING_VALUE', | ||
// byCustomizationType: 'FINE_TUNING' || 'CONTINUED_PRE_TRAINING', | ||
// byOutputModality: 'TEXT' || 'IMAGE' || 'EMBEDDING', | ||
// byInferenceType: 'ON_DEMAND' || 'PROVISIONED', | ||
}; | ||
|
||
const command = new ListFoundationModelsCommand(input); | ||
|
||
const response = await client.send(command); | ||
|
||
return response.modelSummaries; | ||
} | ||
|
||
// Invoke main function if this file was run directly. | ||
if (process.argv[1] === fileURLToPath(import.meta.url)) { | ||
const models = await listFoundationModels(); | ||
console.log(models); | ||
} |
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,47 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { fileURLToPath } from 'url'; | ||
|
||
import { BedrockClient, ListFoundationModelsCommand } from '@aws-sdk/client-bedrock'; | ||
|
||
const REGION = 'us-east-1'; | ||
const client = new BedrockClient( { region: REGION } ); | ||
|
||
export const main = async () => { | ||
const command = new ListFoundationModelsCommand({}); | ||
|
||
const response = await client.send(command); | ||
const models = response.modelSummaries; | ||
|
||
console.log('Listing the available Bedrock foundation models:'); | ||
|
||
for (let model of models) { | ||
console.log('='.repeat(42)); | ||
console.log(` Model: ${model.modelId}`); | ||
console.log('-'.repeat(42)); | ||
console.log(` Name: ${model.modelName}`); | ||
console.log(` Provider: ${model.providerName}`); | ||
console.log(` Model ARN: ${model.modelArn}`); | ||
console.log(` Input modalities: ${model.inputModalities}`); | ||
console.log(` Output modalities: ${model.outputModalities}`); | ||
console.log(` Supported customizations: ${model.customizationsSupported}`); | ||
console.log(` Supported inference types: ${model.inferenceTypesSupported}`); | ||
console.log(` Lifecycle status: ${model.modelLifecycle.status}`); | ||
console.log('='.repeat(42) + '\n'); | ||
} | ||
|
||
const active = models.filter(m => m.modelLifecycle.status === 'ACTIVE').length; | ||
const legacy = models.filter(m => m.modelLifecycle.status === 'LEGACY').length; | ||
|
||
console.log(`There are ${active} active and ${legacy} legacy foundation models in ${REGION}.`); | ||
|
||
return response; | ||
}; | ||
|
||
// Invoke main function if this file was run directly. | ||
if (process.argv[1] === fileURLToPath(import.meta.url)) { | ||
await main(); | ||
} |
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,17 @@ | ||
{ | ||
"name": "bedrock-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": "^3.485.0" | ||
}, | ||
"devDependencies": { | ||
"vitest": "^1.1.3", | ||
"zod": "^3.22.4" | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
javascriptv3/example_code/bedrock/tests/get-foundation-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,24 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { z } from 'zod'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import { getFoundationModel } from '../actions/get-foundation-model.js'; | ||
|
||
const ExpectedSchema = z.object({ | ||
modelId: z.string(), | ||
modelName: z.string() | ||
}); | ||
|
||
describe('get-foundation-model', () => { | ||
it('should return the model\' details', async () => { | ||
let modelDetails = await getFoundationModel(); | ||
expect(modelDetails).not.toBeNull(); | ||
|
||
const isModelDetails = ExpectedSchema.safeParse(modelDetails).success; | ||
expect(isModelDetails).toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.