Skip to content

Commit bc5efdf

Browse files
authored
Merge pull request #2730 from giusedroid/main
bedrock-lambda-nodejs Updates to support Nova Lite, Converse API, NodeJS 20
2 parents 7891a63 + 3e546a1 commit bc5efdf

File tree

3 files changed

+42
-45
lines changed

3 files changed

+42
-45
lines changed

bedrock-lambda-nodejs/example-pattern.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"title": "Bedrock in a NodeJS Lambda",
3-
"description": "Create a Step Functions workflow to query Amazon Athena.",
3+
"description": "Create an AWS Lambda Function that invokes Amazon Bedrock via Converse API",
44
"language": "Node.js",
55
"level": "200",
66
"framework": "AWS SAM",

bedrock-lambda-nodejs/src/.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v16.20.2
1+
v20.19.1

bedrock-lambda-nodejs/src/index.js

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
1+
12
const {
23
BedrockRuntimeClient,
3-
InvokeModelCommand
4+
ConversationRole,
5+
ConverseCommand,
6+
} = require( "@aws-sdk/client-bedrock-runtime");
47

5-
} = require("@aws-sdk/client-bedrock-runtime")
8+
const client = new BedrockRuntimeClient({ region: "us-west-2" });
69

7-
// a client can be shared by different commands.
8-
const client = new BedrockRuntimeClient({ region: "us-west-2" })
10+
const modelId = "us.amazon.nova-lite-v1:0";
911

1012
exports.handler = async event => {
11-
1213
const { text } = event
1314

14-
const body = {
15-
"prompt": `provide a summary of the following text in 5 bulletpoints;
16-
extract 5 tags to categorize the article in a CMS;
17-
provide output as a json object with properties :
18-
"summary" as a list of bulletpoints and "tags" as a list of tags;
19-
<text>${text}</text>
20-
`,
21-
"maxTokens": 1600,
22-
"temperature": 0.3,
23-
"topP": 1.0,
24-
"stopSequences": [],
25-
"countPenalty": { "scale": 0 },
26-
"presencePenalty": { "scale": 0 },
27-
"frequencyPenalty": { "scale": 0 }
28-
}
29-
30-
const params = {
31-
"modelId": "ai21.j2-ultra-v1",
32-
"contentType": "application/json",
33-
"accept": "application/json",
34-
"body": JSON.stringify(body)
35-
}
36-
37-
const command = new InvokeModelCommand(params)
38-
39-
let data, completions
15+
const inputText =`provide a summary of the following text in 5 bulletpoints;
16+
extract 5 tags to categorize the article in a CMS;
17+
provide output as a json object with properties :
18+
"summary" as a list of bulletpoints and "tags" as a list of tags;
19+
<text>${text}</text>
20+
`;
21+
22+
const message = {
23+
content: [{ text: inputText }],
24+
role: ConversationRole.USER,
25+
};
26+
27+
const request = {
28+
modelId,
29+
messages: [message],
30+
inferenceConfig: {
31+
maxTokens: 500, // The maximum response length
32+
temperature: 0.5, // Using temperature for randomness control
33+
//topP: 0.9, // Alternative: use topP instead of temperature
34+
},
35+
};
36+
37+
let responseText = "", statusCode = 200;
4038

4139
try {
42-
data = await client.send(command)
43-
44-
completions = JSON.parse(new TextDecoder().decode(data.body)).completions
45-
console.log(JSON.parse(completions[0].data.text))
46-
47-
48-
}
49-
catch (error) {
50-
console.error(error)
40+
const response = await client.send(new ConverseCommand(request));
41+
responseText = response.output.message.content[0].text;
42+
console.log(response.output.message.content[0].text);
43+
} catch (error) {
44+
console.error(`ERROR: Can't invoke '${modelId}'. Reason: ${error.message}`);
45+
responseText = `ERROR: Can't invoke '${modelId}'. Reason: ${error.message}`;
46+
statusCode = 500;
5147
}
5248

5349
const response = {
54-
statusCode: 200,
55-
body: JSON.stringify(completions[0].data.text),
50+
statusCode,
51+
body: JSON.stringify(responseText),
5652
}
5753

58-
return response
54+
return response;
55+
5956
}

0 commit comments

Comments
 (0)