Skip to content

Commit

Permalink
MBS-9335 aipurpose_genai: Add purpose for ai based question generation
Browse files Browse the repository at this point in the history
  • Loading branch information
PM84 committed Sep 3, 2024
1 parent 8ec8695 commit 81e3e3b
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 46 deletions.
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',
];
}
}
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
46 changes: 24 additions & 22 deletions tools/gemini/classes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ class connector extends \local_ai_manager\base_connector {
public function get_models_by_purpose(): array {
$textmodels = ['gemini-1.0-pro-latest', 'gemini-1.0-pro-vision-latest', 'gemini-1.5-flash-latest', 'gemini-1.5-pro-latest'];
return [
'chat' => $textmodels,
'feedback' => $textmodels,
'singleprompt' => $textmodels,
'translate' => $textmodels,
'chat' => $textmodels,
'feedback' => $textmodels,
'singleprompt' => $textmodels,
'translate' => $textmodels,
'genai' => $textmodels,
];
}

Expand All @@ -61,12 +62,13 @@ public function execute_prompt_completion(StreamInterface $result, array $option
$textanswer .= $part['text'];
}
return prompt_response::create_from_result(
$this->instance->get_model(),
new usage(
(float) $content['usageMetadata']['totalTokenCount'],
(float) $content['usageMetadata']['promptTokenCount'],
(float) $content['usageMetadata']['candidatesTokenCount']),
$textanswer,
$this->instance->get_model(),
new usage(
(float) $content['usageMetadata']['totalTokenCount'],
(float) $content['usageMetadata']['promptTokenCount'],
(float) $content['usageMetadata']['candidatesTokenCount']
),
$textanswer,
);
}

Expand All @@ -90,24 +92,24 @@ public function get_prompt_data(string $prompttext, array $requestoptions): arra
throw new \moodle_exception('Bad message format');
}
$messages[] = [
'role' => $role,
'parts' => [
['text' => $message['message']],
],
'role' => $role,
'parts' => [
['text' => $message['message']],
],
];
}
}
$messages[] = [
'role' => 'user',
'parts' => [
['text' => $prompttext],
],
'role' => 'user',
'parts' => [
['text' => $prompttext],
],
];
return [
'contents' => $messages,
'generationConfig' => [
'temperature' => $this->instance->get_temperature(),
],
'contents' => $messages,
'generationConfig' => [
'temperature' => $this->instance->get_temperature(),
],
];
}

Expand Down
Loading

0 comments on commit 81e3e3b

Please sign in to comment.