-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwords_exercise.py
More file actions
320 lines (247 loc) · 13.6 KB
/
words_exercise.py
File metadata and controls
320 lines (247 loc) · 13.6 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import math
import os
import random
import re
from typing import List, Optional
import jinja2
from utils import get_assistant_response
from pydantic import BaseModel, Field, ValidationError
from exercise import Exercise
class ExampleTestSentenceSchema(BaseModel):
class Config:
extra = 'forbid'
example_sentence: str
sentence_translation: str
difficulty: int
class ExampleSentenceSchema(BaseModel):
class Config:
extra = 'forbid'
example_sentence: str
sentence_translation: str
pronunciation: Optional[str]
class WordTestSchema(BaseModel):
example_list: list[ExampleTestSentenceSchema]
class Config:
extra = 'forbid'
class WordExamplesSchema(BaseModel):
example_list: list[ExampleSentenceSchema]
pronunciation: Optional[str]
conjugations: Optional[str]
class Config:
extra = 'forbid'
class ResponseCorrectionSchema(BaseModel):
translation_score: int
score_justification: str
mistakes_explanation: Optional[str]
corrected_translation: str
class Config:
extra = 'forbid'
class FlashCardExampleSchema(BaseModel):
example: str = Field(..., description="Example sentence")
translation_of_example: str = Field(..., description="Translate the example sentence")
translation_of_word: str= Field(..., description="Translate the word itself")
class Config:
extra = 'forbid'
class FlashcardCorrectionSchema(BaseModel):
translation_score: int
score_justification: str
class Config:
extra = 'forbid'
class WordsExerciseLearn(Exercise):
def __init__(self, word, meaning, word_id, lang, uilang, num_reps, interface, templates):
super().__init__()
self.word = word
self.meaning = meaning
self.word_id = word_id
self.lang = lang
self.uilang = uilang
self.interface = interface
self.templates = templates
self.num_reps = num_reps + 1 if not math.isnan(num_reps) else 1
self.model_base = os.getenv('MODEL_BASE')
self.model_substitute = os.getenv('MODEL_SUBSTITUTE')
self.is_responded = True
async def get_next_user_message(self, user_response: Optional[str]) -> tuple[str, int]:
message_template = self.templates.get_template(self.uilang, self.lang, 'learn_word_query')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
word_phrase = "word" if len(self.word.split()) == 1 else "phrase"
lang_tr = self.interface[self.lang][self.uilang]
query = template.render(word_phrase=word_phrase, word=self.word, meaning=self.meaning, lang=lang_tr)
schema = WordExamplesSchema.model_json_schema()
response_format = {
"type": "json_schema",
"json_schema": {"strict": True,
"name": "word_example",
"schema": schema
}
}
assistant_response = await get_assistant_response(self.interface, query, uilang=self.uilang, model_base=self.model_base,
model_substitute=self.model_substitute, response_format=response_format, validation_cls=WordExamplesSchema)
message_template = self.templates.get_template(self.uilang, self.lang, 'learn_word_user_message')
# examples = [(entry.example_sentence, entry.sentence_translation) for entry in assistant_response.example_list]
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
message = template.render(word=self.word, examples=assistant_response.example_list, conjugations=assistant_response.conjugations, pronunciation=assistant_response.pronunciation)
return message, None
class WordsExerciseTest(Exercise):
def __init__(self, word, word_id, lang, uilang, level, interface, templates):
super().__init__()
self.word = word
self.word_id = word_id
self.lang = lang
self.uilang = uilang
self.level = level
self.interface = interface
self.templates = templates
self.n_examples = 1
self.hint_clicked = False
self.correct_answer_clicked = False
self.is_responded = False
self.difficulty = 3
self.assistant_responses = []
self.user_messages = []
self.next_query_idx = 0
self.model_base = os.getenv('MODEL_BASE')
self.model_substitute = os.getenv('MODEL_SUBSTITUTE')
def correct_answer(self):
return self.assistant_responses[0][self.difficulty - 1]['answer']
def test_sentence(self):
return self.assistant_responses[0][self.difficulty - 1]['test']
async def get_next_user_message(self, user_response: Optional[str]):
lang_tr = self.interface[self.lang][self.uilang]
if user_response is None:
# first message to the user
message_template = self.templates.get_template(self.uilang, self.lang, 'test_word_query_1')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
query = template.render(word=self.word, lang=lang_tr, level=self.level)
validation_cls = WordTestSchema
schema = validation_cls.model_json_schema()
response_format = {
"type": "json_schema",
"json_schema": {"strict": True,
"name": "word_example",
"schema": schema
}
}
assistant_response = await get_assistant_response(self.interface, query, model_base=self.model_base,
model_substitute=self.model_substitute, uilang=self.uilang,
response_format=response_format, validation_cls=validation_cls)
examples = sorted(assistant_response.example_list, key=lambda x: x.difficulty)
examples = [dict(test=item.sentence_translation, answer=item.example_sentence) for item in examples]
self.difficulty = 3
self.assistant_responses.append(examples)
message_template = self.templates.get_template(self.uilang, self.lang, 'test_word_user_message_1')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
message = template.render(lang=lang_tr, test_sentence=self.test_sentence())
quality = None
else:
self.user_messages.append(user_response)
message_template = self.templates.get_template(self.uilang, self.lang, 'test_word_query_2')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
query = template.render(lang=self.interface[self.lang][self.uilang],
user_response=user_response, sentence=self.test_sentence(),
word=self.word)
validation_cls = ResponseCorrectionSchema
schema = validation_cls.model_json_schema()
response_format = {
"type": "json_schema",
"json_schema": {"strict": True,
"name": "word_example",
"schema": schema
}
}
assistant_response = await get_assistant_response(self.interface, query, model_base=self.model_base,
model_substitute=self.model_substitute, uilang=self.uilang,
response_format=response_format, validation_cls=validation_cls)
message_template = self.templates.get_template(self.uilang, self.lang, 'test_word_user_message_2')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
message = template.render(score=assistant_response.translation_score,
justification=assistant_response.score_justification,
explanation=assistant_response.mistakes_explanation,
corrected_translation=assistant_response.corrected_translation,
original_translation=self.correct_answer())
quality = assistant_response.translation_score
return message, quality
def change_difficulty(self, easier: bool) -> None:
lang_tr = self.interface[self.lang][self.uilang]
if easier:
self.difficulty = max(1, self.difficulty - 1)
else:
self.difficulty = min(5, self.difficulty + 1)
message_template = self.templates.get_template(self.uilang, self.lang, 'test_word_user_message_1')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
message = template.render(lang=lang_tr, test_sentence=self.test_sentence())
return message
class FlashcardExercise(Exercise):
def __init__(self, word, word_id, lang, uilang, level, interface, templates):
super().__init__()
self.word = word
self.word_id = word_id
self.lang = lang
self.uilang = uilang
self.level = level
self.interface = interface
self.templates = templates
self.n_examples = 1
self.hint_clicked = False
self.correct_answer_clicked = False
self.is_responded = False
self.assistant_responses = []
self.user_messages = []
self.next_query_idx = 0
self.model_base = os.getenv('MODEL_BASE')
self.model_substitute = os.getenv('MODEL_SUBSTITUTE')
def correct_answer(self):
return f'{self.word}\n\n{self.interface["Example"][self.uilang]}: {self.assistant_responses[0]["example"]}'
async def get_next_user_message(self, user_response: Optional[str]):
lang_tr = self.interface[self.lang][self.uilang]
if user_response is None:
# first message to the user
message_template = self.templates.get_template(self.uilang, self.lang, 'flashcard_query_1')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
query = template.render(word=self.word, level=self.level, lang=lang_tr, lang_ui=self.uilang)
validation_cls = FlashCardExampleSchema
schema = validation_cls.model_json_schema()
response_format = {
"type": "json_schema",
"json_schema": {"strict": True,
"name": "word_example",
"schema": schema
}
}
assistant_response = await get_assistant_response(self.interface, query, model_base=self.model_base,
model_substitute=self.model_substitute, uilang=self.uilang,
response_format=response_format, validation_cls=validation_cls)
self.assistant_responses.append(dict(example=assistant_response.example, translation_example=assistant_response.translation_of_example,
translation_word=assistant_response.translation_of_word))
message_template = self.templates.get_template(self.uilang, self.lang, 'flashcard_user_message_1')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
message = template.render(lang=lang_tr, lang_ui=self.uilang, word=assistant_response.translation_of_word, example=assistant_response.translation_of_example)
quality = None
else:
self.user_messages.append(user_response)
message_template = self.templates.get_template(self.uilang, self.lang, 'flashcard_query_2')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
query = template.render(lang=self.interface[self.lang][self.uilang], user_response=user_response,
word_translation=self.assistant_responses[-1]['translation_word'], correct_answer=self.word)
validation_cls = FlashcardCorrectionSchema
schema = validation_cls.model_json_schema()
response_format = {
"type": "json_schema",
"json_schema": {"strict": True,
"name": "word_example",
"schema": schema
}
}
assistant_response = await get_assistant_response(self.interface, query, model_base=self.model_base,
model_substitute=self.model_substitute, uilang=self.uilang,
response_format=response_format, validation_cls=validation_cls)
message_template = self.templates.get_template(self.uilang, self.lang, 'flashcard_user_message_2')
template = jinja2.Template(message_template, undefined=jinja2.StrictUndefined)
correct_answer = self.word if assistant_response.translation_score < 5 else None
context_translation = self.assistant_responses[-1]['example']
message = template.render(score=assistant_response.translation_score,
justification=assistant_response.score_justification,
correct_answer=correct_answer,
context_translation=context_translation)
quality = assistant_response.translation_score
return message, quality