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_ollama;
|
|
|
18 |
|
|
|
19 |
use core_ai\form\action_settings_form;
|
|
|
20 |
use Psr\Http\Message\RequestInterface;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Class provider.
|
|
|
24 |
*
|
|
|
25 |
* @package aiprovider_ollama
|
|
|
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 |
#[\Override]
|
|
|
32 |
public static function get_action_list(): array {
|
|
|
33 |
return [
|
|
|
34 |
\core_ai\aiactions\generate_text::class,
|
|
|
35 |
\core_ai\aiactions\summarise_text::class,
|
|
|
36 |
\core_ai\aiactions\explain_text::class,
|
|
|
37 |
];
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
#[\Override]
|
|
|
41 |
public static function get_action_settings(
|
|
|
42 |
string $action,
|
|
|
43 |
array $customdata = [],
|
|
|
44 |
): action_settings_form|bool {
|
|
|
45 |
$actionname = substr($action, (strrpos($action, '\\') + 1));
|
|
|
46 |
$customdata['actionname'] = $actionname;
|
|
|
47 |
$customdata['action'] = $action;
|
|
|
48 |
$customdata['providername'] = 'aiprovider_ollama';
|
|
|
49 |
if ($actionname === 'generate_text' || $actionname === 'summarise_text' || $actionname === 'explain_text') {
|
|
|
50 |
return new form\action_generate_text_form(customdata: $customdata);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return false;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
#[\Override]
|
|
|
57 |
public function add_authentication_headers(RequestInterface $request): RequestInterface {
|
|
|
58 |
if (empty($this->config['basicauthenabled'])) {
|
|
|
59 |
return $request;
|
|
|
60 |
} else {
|
|
|
61 |
// Add the Authorization header for basic auth.
|
|
|
62 |
$authheader = 'Basic ' . base64_encode($this->config['username'] . ':' . $this->config['password']);
|
|
|
63 |
return $request->withAddedHeader('Authorization', $authheader);
|
|
|
64 |
}
|
|
|
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 |
'providername' => 'aiprovider_ollama',
|
|
|
74 |
];
|
|
|
75 |
if ($actionname === 'generate_text' || $actionname === 'summarise_text' || $actionname === 'explain_text') {
|
|
|
76 |
$mform = new form\action_generate_text_form(customdata: $customdata);
|
|
|
77 |
return $mform->get_defaults();
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return [];
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
#[\Override]
|
|
|
84 |
public function is_provider_configured(): bool {
|
|
|
85 |
return !empty($this->config['endpoint']);
|
|
|
86 |
}
|
|
|
87 |
}
|