Skip to content

Commit

Permalink
Move jsonprompt text from hardcoded to settings value
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus committed Apr 16, 2024
1 parent 63be066 commit 50b08d2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lang/en/qtype_aitext.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
$string['minwordlimit_help'] = 'If the response requires that students enter text, this is the minimum number of words that each student will be allowed to submit.';
$string['minwordlimitboundary'] = 'This question requires a response of at least {$a->limit} words and you are attempting to submit {$a->count} words. Please expand your response and try again.';
$string['nlines'] = '{$a} lines';
$string['prompt'] = 'Prompt';
$string['prompt_setting'] = 'Wrapper text for the prompt set to the AI System';
$string['jsonprompt'] = 'JSon prompt';
$string['jsonprompt_setting'] = 'Instructions sent to convert the returned value into json';
$string['pluginname'] = 'AI Text';
$string['pluginname_help'] = 'In response to a question, the respondent enters text. A response template may be provided. Responses are given a preliminary grade by an AI system (e.g. ChatGPT) then can be graded manually.';
$string['pluginname_link'] = 'question/type/AI Text';
Expand Down
18 changes: 9 additions & 9 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,19 @@ public function apply_attempt_state(question_attempt_step $step) {
public function grade_response(array $response) : array {
$ai = new ai\ai();
if (is_array($response)) {
$prompt = 'in [[' . strip_tags($response['answer']) . ']]';
$prompt .= ' analyse the part between [[ and ]] as follows: ';
$prompt .= $this->aiprompt;

if ($this->markscheme > '') {
$responsetext = strip_tags($response['answer']);
$responsetext = '[['.$responsetext.']]';
$prompt = get_config('qtype_aitext','prompt');
$prompt = preg_replace("/\[responsetext\]/", $responsetext, $prompt);
$prompt .= ' '.trim($this->aiprompt); if ($this->markscheme > '') {
$prompt .= ' '.$this->markscheme;
} else {
$prompt .= ' Set marks to null in the json object.'.PHP_EOL;
}
$prompt .= ' '.$this->get_json_prompt();

$prompt .= ' '.trim(get_config('qtype_aitext','jsonprompt'));
$prompt .= ' respond in the language '.current_language();

$llmresponse = $ai->prompt_completion($prompt);
$feedback = $llmresponse['response']['choices'][0]['message']['content'];
}
Expand Down Expand Up @@ -197,9 +199,7 @@ public function process_feedback(string $feedback) {
* @return string
*/
protected function get_json_prompt() :string {
return 'Return only a JSON object which enumerates a set of 2 elements.
The elements should have properties of "feedback" and "marks".
The resulting JSON object should be in this format: {"feedback":"string","marks":"number"}
return 'Return only a JSON object where the JSON object is in the this format: {"feedback":"string","marks":"number"}
where marks is a single value summing all marks.\n\n';
}
/**
Expand Down
15 changes: 14 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
$settings->add(new admin_setting_configtext('qtype_aitext/disclaimer',
new lang_string('disclaimer', 'qtype_aitext'),
new lang_string('disclaimer_setting', 'qtype_aitext'),
"(Response provided by ChatGPT)"));
'(Response provided by ChatGPT)'));
$settings->add(new admin_setting_configtextarea('qtype_aitext/prompt',
new lang_string('prompt', 'qtype_aitext'),
new lang_string('prompt_setting', 'qtype_aitext'),
'in [responsetext] analyse the part between [[ and ]] as follows.',
PARAM_RAW,20,3));
$settings->add(new admin_setting_configtextarea('qtype_aitext/jsonprompt',
new lang_string('jsonprompt', 'qtype_aitext'),
new lang_string('jsonprompt_setting', 'qtype_aitext'),
'Return only a JSON object which enumerates a set of 2 elements.The JSON object should be in
this format: {feedback":"string","marks":"number"} where marks is a single value summing all marks.',
PARAM_RAW, 20, 6
));

}

2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'qtype_aitext';
$plugin->version = 2024021800;
$plugin->version = 2024041401;
$plugin->requires = 2020110900;
$plugin->maturity = MATURITY_BETA;
$plugin->dependencies = [
Expand Down

0 comments on commit 50b08d2

Please sign in to comment.