-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanki_gpt_deck.html
126 lines (98 loc) · 3.39 KB
/
anki_gpt_deck.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<div id="fieldContent">{{Front}}</div>
{{FrontSide}}
<hr id=answer>
<br >
<br >
<form id="myForm">
<input type="text" id="userInput" placeholder="Enter something...">
<!-- <button type="submit" id="submitButton">Send</button> -->
</form>
<button id="showTranslationsButton">Practice Translation?</button>
<!-- Initially hidden translations -->
<div id="source_sentence" style="display:none;"></div>
<div id="response" style="display:none;"></div>
<script>
var lang = "English";
var debug = true;
// Access the element containing the field content
var fieldContentElement = debug ? "Anhang" : document.getElementById("fieldContent").innerText;
var prompt_source =
"Provide a sentence in " + lang + " for translation practice of the word " + fieldContentElement + ", showcasing its general use.";
// "German Word: " +
// fieldContentElement +
// ". Give me a source sentence in " + lang + ". which related to this word. I want only the sentence.";
if (debug) {
console.log(prompt_source);
}
function sendApiRequest({prompt, onSuccess}) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.openai.com/v1/chat/completions", true);
xhr.setRequestHeader("Content-Type", "application/json");
// SECURITY NOTE: The API Key should not be exposed in client-side code like this.
// Consider securely retrieving it or using server-side code to handle the API request.
xhr.setRequestHeader(
"Authorization",
"Bearer sk-API_KEY"
);
var body = {
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
temperature: 0.7,
}
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
const response = JSON.parse(xhr.responseText);
if (!response) {
return;
}
if (debug) {
console.log(response.choices[0].message.content);
}
onSuccess(response.choices[0].message.content);
} else {
if (debug) {
console.error("Error by request!!!");
}
}
};
xhr.send(JSON.stringify(body));
}
var source_sentence;
function request_source() {
var prompt = prompt_source;
sendApiRequest({
prompt,
onSuccess: function(response) {
source_sentence = response;
// Handle and display the response (adjust according to the actual response structure)
document.getElementById("source_sentence").innerText = source_sentence;
}
})
}
function sendRequestToOpenAI() {
var userInput = document.getElementById("userInput").value;
var prompt = "German translation of the sentence: " + source_sentence +". My attempt: '" + userInput +
"'. Is my answer grammatically correct and natural, and is my answer a correct translation of the source sentence?";
if (debug) {
console.log(prompt);
}
sendApiRequest({
prompt,
onSuccess: function(response) {
document.getElementById("response").innerText = response;
}
})
}
document.getElementById('showTranslationsButton').onclick = function(event) {
document.getElementById('source_sentence').style.display = 'block';
document.getElementById('source_sentence').innerText = '';
document.getElementById('response').style.display = 'block';
document.getElementById('response').innerText = '';
request_source();
}
document.getElementById('myForm').onsubmit = function(event) {
event.preventDefault();
document.getElementById("userInput").blur()
sendRequestToOpenAI();
};
</script>