-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathconverse.js
More file actions
47 lines (38 loc) · 1.41 KB
/
converse.js
File metadata and controls
47 lines (38 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[javascript.v3.bedrock-runtime.Converse_CohereCommand]
// Use the Conversation API to send a text message to Cohere Command.
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
// Create a Bedrock Runtime client in the AWS Region you want to use.
const client = new BedrockRuntimeClient({ region: "us-east-1" });
// Set the model ID, e.g., Command R.
const modelId = "cohere.command-r-v1:0";
// Start a conversation with the user message.
const userMessage =
"Describe the purpose of a 'hello world' program in one line.";
const conversation = [
{
role: "user",
content: [{ text: userMessage }],
},
];
// Create a command with the model ID, the message, and a basic configuration.
const command = new ConverseCommand({
modelId,
messages: conversation,
inferenceConfig: { maxTokens: 512, temperature: 0.5, topP: 0.9 },
});
try {
// Send the command to the model and wait for the response
const response = await client.send(command);
// Extract and print the response text.
const responseText = response.output.message.content[0].text;
console.log(responseText);
} catch (err) {
console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`);
process.exit(1);
}
// snippet-end:[javascript.v3.bedrock-runtime.Converse_CohereCommand]