-
Notifications
You must be signed in to change notification settings - Fork 6
/
question.php
executable file
·545 lines (492 loc) · 18.3 KB
/
question.php
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* aitext question definition class.
*
* @package qtype_aitext
* @subpackage aitext
* @copyright 2024 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/questionbase.php');
use tool_aiconnect\ai;
/**
* Represents an aitext question.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_aitext_question extends question_graded_automatically_with_countback {
/**
* Plain text or html
* @var string
*/
public $responseformat;
/**
* Count of lines of text
*
* @var int
*/
public $responsefieldlines;
/** @var int indicates whether the minimum number of words required */
public $minwordlimit;
/** @var int indicates whether the maximum number of words required */
public $maxwordlimit;
/**
* LLM Model, will vary between AI systems, e.g. gpt4 or llama3
* @var stream_set_blocking
*/
public $model;
/**
* used in the question editing interface
*
* @var string
*/
public $sampleanswer;
/**
* Information on how to manually grade
*
* @var string
*/
public $graderinfo;
/**
* Plain text or html
* @var int
*/
public $graderinfoformat;
/**
* Text to appear in area where student types response.
* @var string
*/
public $responsetemplate;
/**
* plain text or html
*
* @var int
*/
public $responsetemplateformat;
/**
* String to pass to the LLM telling it how to give
* feedback
* @var mixed
*/
public $aiprompt;
/**
* String to pass to the LLM telling it how to mark
* a submission
* @var string
*/
public $markscheme;
/**
* Question attempt step
*
* @var mixed
*/
public $step;
/** @var int */
public $defaultmark;
/** @var array The string array of file types accepted upon file submission. */
public $filetypeslist;
/**
* Required by the interface question_automatically_gradable_with_countback.
*
* @param array $responses
* @param array $totaltries
* @return number
*/
public function compute_final_grade($responses, $totaltries) {
return true;
}
/**
* Re-initialise the state during a quiz (or question use)
*
* @param question_attempt_step $step
* @return void
*/
public function apply_attempt_state(question_attempt_step $step) {
$this->step = $step;
}
/**
* Grade response by making a call to external
* large language model such as ChatGPT
*
* @param array $response
* @return void
*/
public function grade_response(array $response): array {
if (!$this->is_complete_response($response)) {
$grade = [0 => 0, question_state::$needsgrading];
return $grade;
}
$ai = new ai\ai($this->model);
if (is_array($response)) {
$fullaiprompt = $this->build_full_ai_prompt($response['answer'], $this->aiprompt,
$this->defaultmark, $this->markscheme);
$llmresponse = $ai->prompt_completion($fullaiprompt);
$feedback = $llmresponse['response']['choices'][0]['message']['content'];
}
$contentobject = $this->process_feedback($feedback);
// If there are no marks, write the feedback and set to needs grading .
if (is_null($contentobject->marks)) {
$grade = [0 => 0, question_state::$needsgrading];
} else {
$fraction = $contentobject->marks / $this->defaultmark;
$grade = [$fraction, question_state::graded_state_for_fraction($fraction)];
}
$this->insert_feedback_and_prompt($fullaiprompt, $contentobject);
return $grade;
}
/**
* Inserts the AI feedback and prompt into the attempt step data.
*
* This method is used to insert the AI generated prompt and feedback
* into the attempt step data, which is used during question preview.
* It also adds the feedback as a comment in HTML format.
*
* @param string $fullaiprompt The full AI-generated prompt.
* @param object $contentobject An object containing the feedback.
* @return void
*/
public function insert_feedback_and_prompt($fullaiprompt, $contentobject): void {
// The -aicontent data is used in question preview. Only needs to happen in preview.
$this->insert_attempt_step_data('-aiprompt', $fullaiprompt);
$this->insert_attempt_step_data('-aicontent', $contentobject->feedback);
$this->insert_attempt_step_data('-comment', $contentobject->feedback);
$this->insert_attempt_step_data('-commentformat', FORMAT_HTML);
}
/**
* Used by prompttester in the editing form
*
* @param string $response
* @param string $aiprompt
* @param number $defaultmark
* @param string $markscheme
* @return string;
*/
public function build_full_ai_prompt($response, $aiprompt, $defaultmark, $markscheme): string {
$responsetext = strip_tags($response);
$responsetext = '[['.$responsetext.']]';
$prompt = get_config('qtype_aitext', 'prompt');
$prompt = preg_replace("/\[responsetext\]/", $responsetext, $prompt);
$prompt .= ' '.trim($aiprompt);
if ($markscheme > '') {
// Tell the LLM how to mark the submission.
$prompt .= " The total score is: $defaultmark .";
$prompt .= ' '.$markscheme;
} else {
// Todo should this be a plugin setting value?.
$prompt .= ' Set marks to null in the json object.'.PHP_EOL;
}
$prompt .= ' '.trim(get_config('qtype_aitext', 'jsonprompt'));
$prompt .= ' translate the feedback to the language '.current_language();
return $prompt;
}
/**
*
* Convert string json returned from LLM call to an object,
* if it is not valid json apend as string to new object
*
* @param string $feedback
* @return \stdClass
*/
public function process_feedback(string $feedback) {
if (preg_match('/\{[^{}]*\}/', $feedback, $matches)) {
// Array $matches[1] contains the captured text inside the braces.
$feedback = $matches[0];
}
$contentobject = json_decode($feedback);
if (json_last_error() === JSON_ERROR_NONE) {
$contentobject->feedback = trim($contentobject->feedback);
$contentobject->feedback = preg_replace(['/\[\[/', '/\]\]/'], '"', $contentobject->feedback);
$disclaimer = get_config('qtype_aitext', 'disclaimer');
$disclaimer = str_replace("[[model]]", $this->model, $disclaimer);
$contentobject->feedback .= ' '.$this->llm_translate($disclaimer);
} else {
$contentobject = (object) [
"feedback" => $feedback,
"marks" => null,
];
}
return $contentobject;
}
/**
* Translate into the current language and
* store in a cache
*
* @param string $text
* @return string
*/
protected function llm_translate(string $text): string {
if (current_language() == 'en') {
return $text;
}
$ai = new ai\ai();
$cache = cache::make('qtype_aitext', 'stringdata');
if (($translation = $cache->get(current_language().'_'.$text)) === false) {
$prompt = 'translate "'.$text .'" into '.current_language();
$llmresponse = $ai->prompt_completion($prompt);
$translation = $llmresponse['response']['choices'][0]['message']['content'];
$translation = trim($translation, '"');
$cache->set(current_language().'_'.$text, $translation);
}
return $translation;
}
/**
* Fake manual grading
*
* @param string $name
* @param string $value
* @return void
*/
protected function insert_attempt_step_data(string $name, string $value ): void {
global $DB;
$data = [
'attemptstepid' => $this->step->get_id(),
'name' => $name,
'value' => $value,
];
$DB->insert_record('question_attempt_step_data', $data);
}
/**
* Possibly redundant, a legacy from filesubmission
*
* @param moodle_page $page the page we are outputting to.
* @return renderer_base the response-format-specific renderer.
*/
public function get_format_renderer(moodle_page $page) {
return $page->get_renderer('qtype_aitext', 'format_' . $this->responseformat);
}
/**
* Get expected data types
* @return array
*/
public function get_expected_data() {
$expecteddata = ['answer' => PARAM_RAW];
$expecteddata['answerformat'] = PARAM_ALPHANUMEXT;
return $expecteddata;
}
/**
* Value returned will be written to responsesummary field of the
* question_attempts table
*
* @param array $response
* @return string
*/
public function summarise_response(array $response) {
$output = null;
if (isset($response['answer'])) {
$output .= question_utils::to_plain_text($response['answer'],
$response['answerformat'], ['para' => false]);
}
return $output;
}
/**
* Construct a response that could have lead to the given response summary.
* For testing only
* @param string $summary
* @return array
*/
public function un_summarise_response(string $summary) {
if (empty($summary)) {
return [];
}
if (str_contains($this->responseformat, 'editor')) {
return ['answer' => text_to_html($summary), 'answerformat' => FORMAT_HTML];
} else {
return ['answer' => $summary, 'answerformat' => FORMAT_PLAIN];
}
}
/**
* There is no one correct response for this quesiton type
* so return null.
* @return array|null
*/
public function get_correct_response() {
return null;
}
/**
* Is there some text and does it match the required word count?
* @param array $response
* @return bool
* @throws coding_exception
*/
public function is_complete_response(array $response) {
// Determine if the given response has online text and attachments.
$hasinlinetext = array_key_exists('answer', $response) && ($response['answer'] !== '');
// If there is a response and min/max word limit is set in the form then validate the number of words in response.
if ($hasinlinetext) {
if ($this->check_input_word_count($response['answer'])) {
return false;
}
}
return true;
}
/**
* Return null if is_complete_response() returns true
* otherwise, return the minmax-limit error message
*
* @param array $response
* @return string
*/
public function get_validation_error(array $response) {
if ($this->is_complete_response($response)) {
return '';
}
return $this->check_input_word_count($response['answer']);
}
/**
* Has the quesiton been answered, the attachment stuff needs removing
*
* @param array $response
* @return bool
*/
public function is_gradable_response(array $response) {
// Determine if the given response has online text and attachments.
if (array_key_exists('answer', $response) && ($response['answer'] !== '')) {
return true;
} else if (array_key_exists('attachments', $response)
&& $response['attachments'] instanceof question_response_files) {
return true;
} else {
return false;
}
}
/**
* If you are moving from viewing one question to another this will
* discard the processing if the answer has not changed. If you don't
* use this method it will constantantly generate new question steps and
* the question will be repeatedly set to incomplete. This is a comparison of
* the equality of two arrays.
*
* @param array $prevresponse
* @param array $newresponse
* @return bool
*/
public function is_same_response(array $prevresponse, array $newresponse) {
if (array_key_exists('answer', $prevresponse) && $prevresponse['answer'] !== $this->responsetemplate) {
$value1 = (string) $prevresponse['answer'];
} else {
$value1 = '';
}
if (array_key_exists('answer', $newresponse) && $newresponse['answer'] !== $this->responsetemplate) {
$value2 = (string) $newresponse['answer'];
} else {
$value2 = '';
}
return $value1 === $value2 && ($this->attachments == 0 ||
question_utils::arrays_same_at_key_missing_is_blank(
$prevresponse, $newresponse, 'attachments'));
}
/**
* Checks whether the users is allow to be served a particular file.
*
* @param question_attempt $qa
* @param question_display_options $options
* @param string $component
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool
*/
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($component == 'question' && $filearea == 'response_attachments') {
// Response attachments visible if the question has them.
return $this->attachments != 0;
} else if ($component == 'question' && $filearea == 'response_answer') {
// Response attachments visible if the question has them.
return $this->responseformat === 'editorfilepicker';
} else if ($component == 'qtype_aitext' && $filearea == 'graderinfo') {
return $options->manualcomment && $args[0] == $this->id;
} else {
return parent::check_file_access($qa, $options, $component,
$filearea, $args, $forcedownload);
}
}
/**
* Return the question settings that define this question as structured data.
*
* @param question_attempt $qa the current attempt for which we are exporting the settings.
* @param question_display_options $options the question display options which say which aspects of the question
* should be visible.
* @return mixed structure representing the question settings. In web services, this will be JSON-encoded.
*/
public function get_question_definition_for_external_rendering(question_attempt $qa, question_display_options $options) {
// This is a partial implementation, returning only the most relevant question settings for now,
// ideally, we should return as much as settings as possible (depending on the state and display options).
$settings = [
'responseformat' => $this->responseformat,
'responsefieldlines' => $this->responsefieldlines,
'responsetemplate' => $this->responsetemplate,
'responsetemplateformat' => $this->responsetemplateformat,
'minwordlimit' => $this->minwordlimit,
'maxwordlimit' => $this->maxwordlimit,
];
return $settings;
}
/**
* Check the input word count and return a message to user
* when the number of words are outside the boundary settings.
*
* @param string $responsestring
* @return string|null
.*/
private function check_input_word_count($responsestring) {
if (!$this->minwordlimit && !$this->maxwordlimit) {
// This question does not care about the word count.
return null;
}
// Count the number of words in the response string.
$count = count_words($responsestring);
if ($this->maxwordlimit && $count > $this->maxwordlimit) {
return get_string('maxwordlimitboundary', 'qtype_aitext',
['limit' => $this->maxwordlimit, 'count' => $count]);
} else if ($count < $this->minwordlimit) {
return get_string('minwordlimitboundary', 'qtype_aitext',
['limit' => $this->minwordlimit, 'count' => $count]);
} else {
return null;
}
}
/**
* If this question uses word counts, then return a display of the current
* count, and whether it is within limit, for when the question is being reviewed.
*
* @param array $response responses, as returned by
* {@see question_attempt_step::get_qt_data()}.
* @return string If relevant to this question, a display of the word count.
*/
public function get_word_count_message_for_review(array $response): string {
if (!$this->minwordlimit && !$this->maxwordlimit) {
// This question does not care about the word count.
return '';
}
if (!array_key_exists('answer', $response) || ($response['answer'] === '')) {
// No response.
return '';
}
$count = count_words($response['answer']);
if ($this->maxwordlimit && $count > $this->maxwordlimit) {
return get_string('wordcounttoomuch', 'qtype_aitext',
['limit' => $this->maxwordlimit, 'count' => $count]);
} else if ($count < $this->minwordlimit) {
return get_string('wordcounttoofew', 'qtype_aitext',
['limit' => $this->minwordlimit, 'count' => $count]);
} else {
return get_string('wordcount', 'qtype_aitext', $count);
}
}
}