-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
103 lines (87 loc) · 3.1 KB
/
main.js
File metadata and controls
103 lines (87 loc) · 3.1 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
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
import './style.css';
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
const modelId = 'anthropic.claude-v2';
const paragraphPrompt = "Generate a paragraph for speaking practice. Imagine you are answering an interview(news,job,entertainment) question.";
const tips = "Give daily two liner tip to overcome speaking anxiety.";
const paragraphConversation = [
{
role: "user",
content: [{ text: paragraphPrompt }],
},
];
const tipsConversation = [
{
role: "user",
content: [{ text: tips }],
},
];
let client = null;
async function fetchNewParagraph() {
disableButton(true);
showLoadingAnimation();
try {
// Fetching paragraph for speaking practice
const response = await client.send(new ConverseCommand({ modelId, messages: paragraphConversation }));
console.log("Response from Bedrock (Paragraph):", response);
const paragraph = response.output.message.content[0].text;
document.querySelector("#paragraph").innerHTML = paragraph;
} catch (err) {
console.error("Error fetching new paragraph:", err);
document.querySelector("#paragraph").innerHTML = 'An error occurred while fetching the paragraph.';
}
disableButton(false);
}
async function fetchNewTip(){
try {
// Fetching daily tip for speaking anxiety
const response = await client.send(new ConverseCommand({ modelId, messages: tipsConversation }));
console.log("Response from Bedrock (Tips):", response);
const tip = response.output.message.content[0].text;
document.querySelector("#tips").innerHTML = tip;
} catch (err) {
console.error("Error fetching daily tip:", err);
document.querySelector("#tips").innerHTML = 'Unable to fetch daily tip.';
}
}
function showLoadingAnimation() {
document.querySelector("#paragraph").innerHTML = '<div class="loading-spinner"></div>';
}
function disableButton(isDisabled) {
const paragraphButton = document.querySelector("#getNewParagraph");
paragraphButton.disabled = isDisabled;
}
async function init() {
try {
const creds = await fetchCredentials();
client = await createBedrockClient(creds);
await fetchNewParagraph();
await fetchNewTip();
} catch (err) {
console.error("Error initializing application:", err);
document.querySelector("#paragraph").innerHTML = 'An error occurred while initializing the application.';
document.querySelector("#tips").innerHTML = 'An error occurred while initializing the application.';
}
const paragraphButton = document.querySelector("#getNewParagraph");
paragraphButton.addEventListener("click", fetchNewParagraph);
}
async function createBedrockClient(creds) {
try {
return new BedrockRuntimeClient({
credentials: creds.credentials,
region: creds.region,
});
} catch (err) {
console.error("Error creating Bedrock client:", err);
throw err;
}
}
async function fetchCredentials() {
return {
region: "us-west-2",
credentials: {
accessKeyId: import.meta.env.VITE_AWS_ACCESS_KEY_ID,
secretAccessKey: import.meta.env.VITE_AWS_SECRET_ACCESS_KEY,
},
};
}
init();