-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathconverseStream.js
More file actions
50 lines (41 loc) · 1.51 KB
/
converseStream.js
File metadata and controls
50 lines (41 loc) · 1.51 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
48
49
50
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[javascript.v3.bedrock-runtime.ConverseStream_Mistral]
// Use the Conversation API to send a text message to Mistral.
import {
BedrockRuntimeClient,
ConverseStreamCommand,
} 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., Mistral Large.
const modelId = "mistral.mistral-large-2402-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 ConverseStreamCommand({
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 streamed response text in real-time.
for await (const item of response.stream) {
if (item.contentBlockDelta) {
process.stdout.write(item.contentBlockDelta.delta?.text);
}
}
} catch (err) {
console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`);
process.exit(1);
}
// snippet-end:[javascript.v3.bedrock-runtime.ConverseStream_Mistral]