1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace aiprovider_azureai;
|
|
|
18 |
|
|
|
19 |
use core_ai\form\action_settings_form;
|
|
|
20 |
use Psr\Http\Message\RequestInterface;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Class provider.
|
|
|
24 |
*
|
|
|
25 |
* @package aiprovider_azureai
|
|
|
26 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class provider extends \core_ai\provider {
|
|
|
30 |
/**
|
|
|
31 |
* Get the list of actions that this provider supports.
|
|
|
32 |
*
|
|
|
33 |
* @return array An array of action class names.
|
|
|
34 |
*/
|
|
|
35 |
public static function get_action_list(): array {
|
|
|
36 |
return [
|
|
|
37 |
\core_ai\aiactions\generate_text::class,
|
|
|
38 |
\core_ai\aiactions\generate_image::class,
|
|
|
39 |
\core_ai\aiactions\summarise_text::class,
|
|
|
40 |
\core_ai\aiactions\explain_text::class,
|
|
|
41 |
];
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
#[\Override]
|
|
|
45 |
public function add_authentication_headers(RequestInterface $request): RequestInterface {
|
|
|
46 |
return $request
|
|
|
47 |
->withAddedHeader('api-key', $this->config['apikey']);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
#[\Override]
|
|
|
51 |
public static function get_action_settings(
|
|
|
52 |
string $action,
|
|
|
53 |
array $customdata = [],
|
|
|
54 |
): action_settings_form|bool {
|
|
|
55 |
$actionname = substr($action, (strrpos($action, '\\') + 1));
|
|
|
56 |
$customdata['actionname'] = $actionname;
|
|
|
57 |
$customdata['action'] = $action;
|
|
|
58 |
if ($actionname === 'generate_text' || $actionname === 'summarise_text' || $actionname === 'explain_text') {
|
|
|
59 |
return new form\action_generate_text_form(customdata: $customdata);
|
|
|
60 |
} else if ($actionname === 'generate_image') {
|
|
|
61 |
return new form\action_generate_image_form(customdata: $customdata);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
return false;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
#[\Override]
|
|
|
68 |
public static function get_action_setting_defaults(string $action): array {
|
|
|
69 |
$actionname = substr($action, (strrpos($action, '\\') + 1));
|
|
|
70 |
$customdata = [
|
|
|
71 |
'actionname' => $actionname,
|
|
|
72 |
'action' => $action,
|
|
|
73 |
];
|
|
|
74 |
if ($actionname === 'generate_text' || $actionname === 'summarise_text' || $actionname === 'explain_text') {
|
|
|
75 |
$mform = new form\action_generate_text_form(customdata: $customdata);
|
|
|
76 |
return $mform->get_defaults();
|
|
|
77 |
} else if ($actionname === 'generate_image') {
|
|
|
78 |
$mform = new form\action_generate_image_form(customdata: $customdata);
|
|
|
79 |
return $mform->get_defaults();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return [];
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Check this provider has the minimal configuration to work.
|
|
|
87 |
*
|
|
|
88 |
* @return bool Return true if configured.
|
|
|
89 |
*/
|
|
|
90 |
public function is_provider_configured(): bool {
|
|
|
91 |
return !empty($this->config['apikey']) && !empty($this->config['endpoint']);
|
|
|
92 |
}
|
|
|
93 |
}
|