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

MBS-9334 aipurpose_genai: Add purpose for ai based question generation #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions classes/base_connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ public function make_request(array $data): request_response {
$return = request_response::create_from_result($response->getBody());
} else {
$return = request_response::create_from_error(
$response->getStatusCode(),
get_string('error_sendingrequestfailed', 'local_ai_manager'),
$response->getBody(),
$response->getStatusCode(),
get_string('error_sendingrequestfailed', 'local_ai_manager'),
$response->getBody(),
);
}
return $return;
Expand Down Expand Up @@ -238,8 +238,8 @@ protected function create_error_response_from_exception(ClientExceptionInterface
*/
protected function get_headers(): array {
return [
'Authorization' => 'Bearer ' . $this->get_api_key(),
'Content-Type' => 'application/json;charset=utf-8',
'Authorization' => 'Bearer ' . $this->get_api_key(),
'Content-Type' => 'application/json;charset=utf-8',
];
}
}
74 changes: 48 additions & 26 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public function perform_request(string $prompttext, array $options = []): prompt
$options = $this->sanitize_options($options);
} catch (\Exception $exception) {
return prompt_response::create_from_error(
400,
get_string('error_http400', 'local_ai_manager'),
$exception->getMessage()
400,
get_string('error_http400', 'local_ai_manager'),
$exception->getMessage()
);
}

Expand All @@ -140,14 +140,16 @@ public function perform_request(string $prompttext, array $options = []): prompt
if ($userusage->get_currentusage() >= $this->configmanager->get_max_requests($this->purpose, $userinfo->get_role())) {
$period = format_time($this->configmanager->get_max_requests_period());
return prompt_response::create_from_error(
429,
get_string(
'error_http429',
'local_ai_manager',
['count' => $this->configmanager->get_max_requests($this->purpose, $userinfo->get_role()),
'period' => $period]
),
''
429,
get_string(
'error_http429',
'local_ai_manager',
[
'count' => $this->configmanager->get_max_requests($this->purpose, $userinfo->get_role()),
'period' => $period,
]
),
''
);
}

Expand All @@ -168,21 +170,35 @@ public function perform_request(string $prompttext, array $options = []): prompt
$endtime = microtime(true);
$duration = round($endtime - $starttime, 2);
if ($requestresult->get_code() !== 200) {
$promptresponse = prompt_response::create_from_error($requestresult->get_code(), $requestresult->get_errormessage(),
$requestresult->get_debuginfo());
$promptresponse = prompt_response::create_from_error(
$requestresult->get_code(),
$requestresult->get_errormessage(),
$requestresult->get_debuginfo()
);
get_ai_response_failed::create_from_prompt_response($promptdata, $promptresponse, $duration)->trigger();
return $promptresponse;
}
$promptcompletion = $this->connector->execute_prompt_completion($requestresult->get_response(), $options);
if (!empty($options['forcenewitemid']) && !empty($options['component']) &&
!empty($options['contextid'] && !empty($options['itemid']))) {
if ($DB->record_exists('local_ai_manager_request_log',
['component' => $options['component'], 'contextid' => $options['contextid'], 'itemid' => $options['itemid']])) {
if (
!empty($options['forcenewitemid']) && !empty($options['component']) &&
!empty($options['contextid'] && !empty($options['itemid']))
) {
if ($DB->record_exists(
'local_ai_manager_request_log',
[
'component' => $options['component'],
'contextid' => $options['contextid'],
'itemid' => $options['itemid'],
]
)) {
$existingitemid = $options['itemid'];
unset($options['itemid']);
$this->log_request($prompttext, $promptcompletion, $duration, $requestoptions, $options);
$promptresponse = prompt_response::create_from_error(409, get_string('error_http409', 'local_ai_manager',
$existingitemid), '');
$promptresponse = prompt_response::create_from_error(409, get_string(
'error_http409',
'local_ai_manager',
$existingitemid
), '');
get_ai_response_failed::create_from_prompt_response($promptdata, $promptresponse, $duration)->trigger();
return $promptresponse;
}
Expand All @@ -205,9 +221,13 @@ public function perform_request(string $prompttext, array $options = []): prompt
* @param array $options part of $requestoptions, contains the options directly passed to the manager
* @return int the record id of the log record which has been stored to the database
*/
public function log_request(string $prompttext, prompt_response $promptcompletion, float $executiontime,
array $requestoptions = [],
array $options = []): int {
public function log_request(
string $prompttext,
prompt_response $promptcompletion,
float $executiontime,
array $requestoptions = [],
array $options = []
): int {
global $DB, $USER;

// phpcs:disable moodle.Commenting.TodoComment.MissingInfoInline
Expand Down Expand Up @@ -267,13 +287,15 @@ private function sanitize_options(array $options): array {
foreach ($options as $key => $value) {
if (!array_key_exists($key, $this->purpose->get_available_purpose_options())) {
throw new \coding_exception('Option ' . $key . ' is not allowed for the purpose ' .
$this->purpose->get_plugin_name());
$this->purpose->get_plugin_name());
}
if (is_array($this->purpose->get_available_purpose_options()[$key])) {
if (!in_array($value[0], array_map(fn($valueobject) => $valueobject['key'],
$this->purpose->get_available_purpose_options()[$key]))) {
if (!in_array($value[0], array_map(
fn($valueobject) => $valueobject['key'],
$this->purpose->get_available_purpose_options()[$key]
))) {
throw new \coding_exception('Value ' . $value[0] . ' for option ' . $key . ' is not allowed for the purpose ' .
$this->purpose->get_plugin_name());
$this->purpose->get_plugin_name());
}
} else {
if ($this->purpose->get_available_purpose_options()[$key] === base_purpose::PARAM_ARRAY) {
Expand Down
47 changes: 47 additions & 0 deletions purposes/genai/classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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/>.

/**
* aipurpose_genai privacy provider class.
*
* @package aipurpose_genai
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace aipurpose_genai\privacy;

/**
* aipurpose_genai privacy provider class.
*
* @package aipurpose_genai
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
75 changes: 75 additions & 0 deletions purposes/genai/classes/purpose.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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/>.

/**
* Purpose genai methods
*
* @package aipurpose_genai
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace aipurpose_genai;

use local_ai_manager\base_purpose;

/**
* Purpose genai methods
*
* @package aipurpose_genai
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class purpose extends base_purpose {

/**
* Get the request options.
*
* @param array $options
* @return array
*/
#[\Override]
public function get_request_options(array $options): array {

if (array_key_exists('messages', $options)) {
$messages = [];
foreach ($options['messages'] as $message) {
switch ($message['role']) {
case 'user':
$messages[] = ['sender' => 'user', 'message' => $message['content']];
break;
case 'system':
$messages[] = ['sender' => 'system', 'message' => $message['content']];
break;
}
}
return ['conversationcontext' => $messages];
}
return [];
}

/**
* Get the additional purpose options
*
* @return array
*/
#[\Override]
public function get_additional_purpose_options(): array {
return ['messages' => base_purpose::PARAM_ARRAY];
}
}
29 changes: 29 additions & 0 deletions purposes/genai/lang/de/aipurpose_genai.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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/>.

/**
* Lang strings for aipurpose_genai - DE.
*
* @package aipurpose_genai
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['pluginname'] = 'Testfragengenerierung';
$string['privacy:metadata'] = 'Das Zweck-Subplugin "Testfragengenerierung" speichert keine persönlichen Daten.';
$string['requestcount'] = 'GenAI-Anfragen';
$string['requestcount_shortened'] = 'GenAI-';
29 changes: 29 additions & 0 deletions purposes/genai/lang/en/aipurpose_genai.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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/>.

/**
* Lang strings for aipurpose_genai - EN.
*
* @package aipurpose_genai
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['pluginname'] = 'Questiongeneration';
$string['privacy:metadata'] = 'The local ai_manager purpose subplugin "Questiongeneration" does not store any personal data.';
$string['requestcount'] = 'GenAI requests';
$string['requestcount_shortened'] = 'GenAI';
31 changes: 31 additions & 0 deletions purposes/genai/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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/>.

/**
* Version file for aipurpose_genai.
*
* @package aipurpose_genai
* @copyright ISB Bayern, 2024
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024090300;
$plugin->requires = 2023042403;
$plugin->release = '0.0.1';
$plugin->component = 'aipurpose_genai';
$plugin->maturity = MATURITY_ALPHA;
8 changes: 7 additions & 1 deletion tools/chatgpt/classes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function get_models_by_purpose(): array {
'feedback' => $chatgptmodels,
'singleprompt' => $chatgptmodels,
'translate' => $chatgptmodels,
'genai' => $chatgptmodels,
];
}

Expand Down Expand Up @@ -90,16 +91,21 @@ public function get_prompt_data(string $prompttext, array $requestoptions): arra
];
}
}
$messages[] = ['role' => 'user', 'content' => $prompttext];

if (!empty($prompttext)) {
$messages[] = ['role' => 'user', 'content' => $prompttext];
}

$parameters = [
'temperature' => $this->instance->get_temperature(),
'messages' => $messages,
];

if (!$this->instance->azure_enabled()) {
// If azure is enabled, the model will be preconfigured in the azure resource, so we do not need to send it.
$parameters['model'] = $this->instance->get_model();
}

return $parameters;
}

Expand Down
Loading