-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (36 loc) · 1.09 KB
/
script.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
document.getElementById('apiCallButton').addEventListener('click', function() {
const apiKey = document.getElementById('apiKey').value;
const userInput = document.getElementById('userInput').value;
if (!apiKey) {
alert('Please enter your API key.');
return;
}
const apiRequestBody = {
model: "gpt-3.5-turbo-1106",
messages: [
{
role: "system",
content: "Your predefined system prompt goes here..."
},
{
role: "user",
content: userInput
}
]
};
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify(apiRequestBody)
})
.then(response => response.json())
.then(data => {
document.getElementById('apiResponse').textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
console.error('Error:', error);
});
});