-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (42 loc) · 1.62 KB
/
app.js
File metadata and controls
43 lines (42 loc) · 1.62 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
const form = document.getElementById('chat-form');
const mytextInput = document.getElementById('mytext');
const responseModel1Textarea = document.getElementById('response-model1'); // Updated
const a=1;
const API_KEY = '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';
form.addEventListener('submit', async (e) => {
e.preventDefault();
const mytext = mytextInput.value.trim();
if (mytext) {
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [{
role: 'user',
content: mytext
}],
temperature: 1.0,
top_p: 0.7,
n: 1,
stream: false,
presence_penalty: 0,
frequency_penalty: 0,
}),
});
if (response.ok) {
const data = await response.json();
responseModel1Textarea.value = data.choices[0].message.content; // Updated
} else {
responseModel1Textarea.value = 'Error: Unable to process your request.'; // Updated
}
} catch (error) {
console.error(error);
responseModel1Textarea.value = 'Error: Unable to process your request.'; // Updated
}
}
});