-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
204 lines (195 loc) · 5.4 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html>
<head>
<title>Termen en definities - AI oefentoets</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
body {
padding: 20px;
max-width: 700px;
position: relative;
margin: auto;
font-family: Arial, Helvetica, sans-serif;
}
output {
color: red;
}
.word {
text-align: center;
min-width: 120px;
}
.word:hover {
cursor: pointer;
background-color: lightgray;
}
table {
margin-top: 30px;
border-collapse: collapse;
}
table td {
border: 1px solid black;
padding: 10px;
}
h1 img {
position: absolute;
right: 10px;
top: 10px;
}
textarea {
width: 100%;
height: 100px;
}
label, input, select, button, textarea {
display: inline-block;
margin-bottom: 10px;
}
label {
width: 110px;
margin-right: 5px ;
}
#loader {
position: absolute;
width: 100%;
height: 100%;
background: white;
opacity: 0.8;
text-align: center;
font-size: 50px;
padding: 30px;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1 class="text-3xl font-bold">
Termen en definities
<img src="logo.webp" alt="AI" width="150" height="150">
</h1>
<div id="app">
<div id="loader" v-show="loading">
⏳
</div>
<div v-show="showForm">
<div>
<label>OpenAI key</label>
<input autocomplete="on" type="password" v-model="apiKey" placeholder="Voer je OpenAI key in">
<output>{{ error }}</output>
</div>
<div>
<label>Niveau</label>
<select v-model="grade">
<option value="3">Groep 3</option>
<option value="4">Groep 4</option>
<option value="5">Groep 5</option>
<option value="6">Groep 6</option>
<option value="7">Groep 7</option>
<option value="8">Groep 8</option>
</select>
</div>
<div>
<label>Woorden</label>
<textarea v-model="input" placeholder="Voer de woorden in"></textarea>
</div>
<div>
<button type="submit" @click="submit">maak oefentoets</button>
</div>
</div>
<div v-show="!showForm">
<a href="#" @click="showForm = true">< Terug</a>
<table v-show="toets.vragen.length">
<tr>
<th>Term</th>
<th>Definities</th>
</tr>
<tr v-for="(vraag,index) in toets.vragen">
<td class="word" @click="clickWord(index)" title="klik voor een hint">{{ word(index) }}</td>
<td>
{{ vraag.definitie }}
</td>
</tr>
</table>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
loading: false,
apiKey: localStorage.getItem('apiKey'),
input: '',
grade: 7,
toets: {
onderwerp: '',
vragen: [],
},
clicks: [],
error: null,
showForm: true
},
methods: {
async submit() {
if (!this.apiKey) {
this.error = 'Voer je OpenAI key in';
return;
}
this.loading = true;
this.error = null;
localStorage.setItem('apiKey', this.apiKey);
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
response_format: { "type": "json_object" },
messages: [
{
"role": "system",
"content": `Je bent een API service die oefentoetsen genereert in JSON formaat.
Je maakt een taaltoets over definities van woorden.
Het niveau v/d toets en de definities is groep ${this.grade}.
De definities moeten simpel en duidelijk zijn.
De definities mogen het woord in kwestie niet bevatten.
De output voldoet aan: { vragen: [ { woord: string, definitie: string } ] }`
},
{
"role": "user",
"content": "Voor de taaltoets, geef me definities voor de volgende woorden: " + this.input
},
]
})
});
this.loading = false;
if (response.status == 401) {
this.error = 'Verkeerde OpenAI key';
localStorage.removeItem('apiKey');
} else {
const data = await response.json();
const toets = data.choices[0].message.content;
this.toets = JSON.parse(toets);
this.clicks = this.toets.vragen.map(() => 0);
this.showForm = false;
}
},
clickWord(index) {
this.clicks[index]++;
this.clicks = [...this.clicks];
},
word(index) {
const clicks = this.clicks[index];
if (clicks == 0) {
return '...';
}
if (clicks == 1) {
return this.toets.vragen[index].woord.slice(0, 1) + '...';
}
return this.toets.vragen[index].woord;
},
}
});
</script>
</body>
</html>