-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
61 lines (56 loc) · 2.37 KB
/
main.js
File metadata and controls
61 lines (56 loc) · 2.37 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
function sendTranslation(from_language, to_language, user_text){
console.log(from_language, to_language, user_text);
const data = {
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: `Please help me translate ${from_language} to ${to_language}`},
//system sets the context of what you're asking
{ role: 'user', content: `${user_text}` }
//user is the end user asking the question
]
};
fetch(url, {
method: 'POST', //post to openai api
headers: {
'Authorization': `Bearer ${apiKey}`, //bearer token with apikey
'Content-Type': 'application/json' //application utilizing json
},
body: JSON.stringify(data) //flattens our message into json
})
.then(response => response.json()) //response is set (reminder to display this later)
.then(reply => {
const output = document.getElementById('output');
const message = reply.choices[0].message.content; //pulls the message out of the object located inside the choices array
// console.log(typeof message);
output.innerText = message;
});
}
//sendTranslation("english", "spanish", "hello world");
document.addEventListener('DOMContentLoaded', () => {
const button = document.getElementById('submit');
button.addEventListener('click', () => {
const from_language = document.getElementById('from_language');
const to_language = document.getElementById('to_language');
const user_text = document.getElementById('user_text');
console.log(from_language.value, to_language.value, user_text.value);
sendTranslation(from_language.value, to_language.value, user_text.value);
});
});
// response object as a reply
// {
// id: 'chatcmpl-9jbydllHH3Y0h5P0aEYhkER9QiUcW',
// object: 'chat.completion',
// created: 1720657771,
// model: 'gpt-3.5-turbo-0125',
// choices: [
// {
// index: 0,
// message: [Object],
// logprobs: null,
// finish_reason: 'stop'
// }
// ],
// usage: { prompt_tokens: 23, completion_tokens: 9, total_tokens: 32 },
// system_fingerprint: null
// }
// language option popup