-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_based_assistant_streams.js
117 lines (109 loc) · 3.52 KB
/
cli_based_assistant_streams.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const {
BedrockRuntimeClient,
InvokeModelWithResponseStreamCommand,
} = require("@aws-sdk/client-bedrock-runtime");
const { TextDecoder } = require("util");
async function askClaudeStream(userMessage, client, modelId) {
const conversation = [
{
role: "user",
content: [{ type: "text", text: userMessage }],
},
];
const body = JSON.stringify({
anthropic_version: "bedrock-2023-05-31",
max_tokens: 1000,
messages: conversation,
temperature: 1,
top_k: 250,
});
const command = new InvokeModelWithResponseStreamCommand({
modelId,
body,
contentType: "application/json",
accept: "application/json",
});
try {
const response = await client.send(command);
if (response.body) {
console.log("Streaming response received...");
const decoder = new TextDecoder("utf-8");
let accumulatedData = "";
for await (const chunk of response.body) {
const decodedChunk = decoder.decode(chunk.chunk.bytes, {
stream: true,
});
accumulatedData += decodedChunk;
// Split accumulated data into separate JSON strings
const jsonStrings = accumulatedData.split(/(?<=})\s*(?={)/g);
accumulatedData = jsonStrings.pop(); // Keep the last part for the next iteration
for (const jsonString of jsonStrings) {
try {
const jsonObject = JSON.parse(jsonString);
if (jsonObject.type === "content_block_delta") {
const text = jsonObject.delta?.text;
if (text) {
process.stdout.write(text);
}
} else if (
jsonObject.type === "message_start" ||
jsonObject.type === "message_delta"
) {
// Handle other message types if needed
}
} catch (error) {
console.error("Error parsing JSON:", error);
console.error("Problematic JSON string:", jsonString);
}
}
}
// Process any remaining data
if (accumulatedData.trim().length > 0) {
try {
const jsonObject = JSON.parse(accumulatedData.trim());
if (jsonObject.type === "content_block_delta") {
const text = jsonObject.delta?.text;
if (text) {
process.stdout.write(text);
}
}
} catch (error) {
console.error("Error parsing remaining JSON:", error);
console.error(
"Problematic remaining JSON string:",
accumulatedData.trim()
);
}
}
} else {
console.log("No body available in response.");
}
} catch (error) {
console.error(`ERROR: Can't invoke '${modelId}'. Reason: ${error}`);
}
}
async function main() {
const client = new BedrockRuntimeClient({ region: "eu-west-2" });
const modelId = "anthropic.claude-3-sonnet-20240229-v1:0";
console.log("Welcome to CMND.ai Claude 3 Sonnet Assistant");
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const askQuestion = (query) =>
new Promise((resolve) => readline.question(query, resolve));
while (true) {
const userMessage = await askQuestion("User: ");
if (
userMessage.toLowerCase() === "exit" ||
userMessage.toLowerCase() === "quit"
) {
console.log("Claude: Goodbye!");
readline.close();
break;
}
await askClaudeStream(userMessage, client, modelId);
console.log("\n"); // Add a newline after Claude's response
}
}
main();