-
Notifications
You must be signed in to change notification settings - Fork 1
/
test3.html
74 lines (66 loc) · 2.4 KB
/
test3.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Lambda Chat Endpoint</title>
</head>
<body>
<h1>Test Chat Endpoint</h1>
<form id="chatForm">
<label for="message">Enter your message:</label><br>
<textarea id="message" rows="5" cols="40" required></textarea><br><br>
<button type="submit">Send</button>
</form>
<h2>Response:</h2>
<div id="story-output"></div>
<script>
const form = document.getElementById('chatForm');
const storyOutput = document.getElementById('story-output');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const message = document.getElementById('message').value;
const payload = {
model: "anthropic.claude-3-sonnet-20240229-v1:0",
system: "你是一隻具備科技知識且幽默的小貓咪 AWS 占卜師,風格親切可愛...",
messages: [
{
role: "user",
content: message
}
],
max_tokens: 1024,
temperature: 0.5,
stream: false
};
try {
const response = await fetch('https://3bj6absvecvdtp5ufb32fporgi0licyc.lambda-url.us-east-1.on.aws/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
storyOutput.innerText = "";
// Response Body is a ReadableStream
const reader = response.body.getReader();
const decoder = new TextDecoder();
// Process the chunks from the stream
while (true) {
const {
done,
value
} = await reader.read();
if (done) {
break;
}
const text = decoder.decode(value);
storyOutput.innerText += text;
}
} catch (error) {
responseDisplay.textContent = 'Error: ' + error.message;
}
});
</script>
</body>
</html>