Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Moodle 4.5 Core AI subsystem #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lang/en/qtype_aitext.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
$string['showprompt'] = 'Show prompt';
$string['thedefaultmarksscheme'] = 'Deduct a point from the total score for each grammar or spelling mistake.';
$string['thedefaultprompt'] = 'Explain if there is anything wrong with the grammar and spelling in the text.';
$string['usemebis_ai'] = 'Use Mebis AI';
PhMemmel marked this conversation as resolved.
Show resolved Hide resolved
$string['usemebis_ai_setting'] = 'Use the Local aiconnector plugin from Mebis-lp to process AI related queries (must be installed)';
PM84 marked this conversation as resolved.
Show resolved Hide resolved
$string['untestedquestionbehaviour'] = 'Untested question behaviour';
$string['wordcount'] = 'Word count: {$a}';
$string['wordcounttoofew'] = 'Word count: {$a->count}, less than the required {$a->limit} words.';
Expand Down
70 changes: 39 additions & 31 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,41 @@ public function compute_final_grade($responses, $totaltries) {
public function apply_attempt_state(question_attempt_step $step) {
$this->step = $step;
}
/**
* Call the llm using either the 4.5 core api
* or the the mebis ai depending on the usemebisai
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* or the the mebis ai depending on the usemebisai
* or the the mebis ai depending on the uselocalaimanager

* settings checkbox.
*
* @param string $prompt
* @return string $response
*/
public function perform_request(string $prompt, string $purpose): string {
if (get_config('qtype_aitext', 'usemebisai')) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (get_config('qtype_aitext', 'usemebisai')) {
if (get_config('qtype_aitext', 'uselocalaimanager')) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

$manager = new local_ai_manager\manager($purpose);
$llmresponse = (object) $manager->perform_request($prompt, ['component' => 'qtype_aitext', 'contextid' => $this->contextid]);
if ($llmresponse->get_code() !== 200) {
throw new moodle_exception(
'err_retrievingfeedback',
'qtype_aitext',
'',
$llmresponse->get_errormessage(),
$llmresponse->get_debuginfo()
);
}
return $llmresponse->get_content();
} else {
global $USER;
$manager = new \core_ai\manager();
$action = new \core_ai\aiactions\generate_text(
contextid: $this->contextid,
userid: $USER->id,
prompttext: $prompt
);
$llmresponse = $manager->process_action($action);
$responsedata = $llmresponse->get_response_data();
return $responsedata['generatedcontent'];
}
}

/**
* Get the spellchecking response.
Expand All @@ -155,18 +190,8 @@ public function apply_attempt_state(question_attempt_step $step) {
*/
private function get_spellchecking(array $response):string {
$fullaiprompt = $this->build_full_ai_spellchecking_prompt($response['answer']);
$ai = new local_ai_manager\manager('feedback');
$llmresponse = $ai->perform_request($fullaiprompt, ['component' => 'qtype_aitext', 'contextid' => $this->contextid]);
if ($llmresponse->get_code() !== 200) {
throw new moodle_exception(
'err_airesponsefailed',
'qtype_aitext',
'',
$llmresponse->get_errormessage(),
$llmresponse->get_debuginfo()
);
}
return $llmresponse->get_content();
$response = $this->perform_request($fullaiprompt, 'feedback');
return $response;
}

/**
Expand All @@ -187,18 +212,11 @@ public function grade_response(array $response): array {
$grade = [0 => 0, question_state::$needsgrading];
return $grade;
}
$ai = new local_ai_manager\manager('feedback');
if (is_array($response)) {
$fullaiprompt = $this->build_full_ai_prompt($response['answer'], $this->aiprompt,
$this->defaultmark, $this->markscheme);
$llmresponse = $ai->perform_request($fullaiprompt, ['component' => 'qtype_aitext', 'contextid' => $this->contextid]);
if ($llmresponse->get_code() !== 200) {
throw new moodle_exception('err_retrievingfeedback', 'qtype_aitext', '', $llmresponse->get_errormessage(),
$llmresponse->get_debuginfo());
}
$feedback = $llmresponse->get_content();
$feedback = $this->perform_request($fullaiprompt, 'feedback');
}

$contentobject = $this->process_feedback($feedback);

// If there are no marks, write the feedback and set to needs grading .
Expand Down Expand Up @@ -256,7 +274,6 @@ public function build_full_ai_prompt($response, $aiprompt, $defaultmark, $marksc
* @throws coding_exception
*/
public function build_full_ai_spellchecking_prompt(string $response): string {
// $response = strip_tags($response);
return get_string('spellcheck_prompt', 'qtype_aitext') . ($response);
}

Expand All @@ -282,10 +299,6 @@ public function process_feedback(string $feedback) {
$contentobject->feedback = trim($contentobject->feedback);
$contentobject->feedback = preg_replace(['/\[\[/', '/\]\]/'], '"', $contentobject->feedback);
$disclaimer = get_config('qtype_aitext', 'disclaimer');
// TODO Model currently is only used for connecting and at this point I believe. We need to remove all the model
// selection logic or make local_ai_manager support the selection of models.
$disclaimer = str_replace("[[model]]",
\local_ai_manager\ai_manager_utils::get_connector_instance_by_purpose('feedback')->get_model(), $disclaimer);
$contentobject->feedback .= ' '.$this->llm_translate($disclaimer);
} else {
$contentobject = (object) [
Expand All @@ -307,15 +320,10 @@ protected function llm_translate(string $text): string {
if (current_language() == 'en') {
return $text;
}
$ai = new local_ai_manager\manager('translate');
$cache = cache::make('qtype_aitext', 'stringdata');
if (($translation = $cache->get(current_language().'_'.$text)) === false) {
$prompt = 'translate "'.$text .'" into '.current_language();
$llmresponse = $ai->perform_request($prompt);
if ($llmresponse->get_code() !== 200) {
throw new moodle_exception('Could not retrieve the translation from the AI tool');
}
$translation = $llmresponse->get_content();
$translation = $this->perform_request($prompt, 'translate');
$translation = trim($translation, '"');
$cache->set(current_language().'_'.$text, $translation);
}
Expand Down
8 changes: 7 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'qtype_aitext/disclaimer',
new lang_string('disclaimer', 'qtype_aitext'),
new lang_string('disclaimer_setting', 'qtype_aitext'),
'(Response provided by [[model]])'
'(Response provided by an AI System)'
));
$settings->add(new admin_setting_configtextarea(
'qtype_aitext/prompt',
Expand Down Expand Up @@ -66,6 +66,12 @@
new lang_string('responseformat_setting', 'qtype_aitext'),
0, ['plain' => 'plain', 'editor' => 'editor', 'monospaced' => 'monospaced']
));
$settings->add(new admin_setting_configcheckbox(
'qtype_aitext/usemebisai',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'qtype_aitext/usemebisai',
'qtype_aitext/uselocalaimanager',

new lang_string('usemebis_ai', 'qtype_aitext'),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new lang_string('usemebis_ai', 'qtype_aitext'),
new lang_string('use_local_ai_manager', 'qtype_aitext'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this as well, and approve

new lang_string('usemebis_ai_setting', 'qtype_aitext'),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new lang_string('usemebis_ai_setting', 'qtype_aitext'),
new lang_string('use_local_ai_manager_setting', 'qtype_aitext'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like and approve

0
));

}