Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_openai;
18
 
19
use core_ai\form\action_settings_form;
20
use Psr\Http\Message\RequestInterface;
21
 
22
/**
23
 * Class provider.
24
 *
25
 * @package    aiprovider_openai
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
        if (isset($this->config['orgid'])) {
47
            return $request
48
                ->withAddedHeader('Authorization', "Bearer {$this->config['apikey']}")
49
                ->withAddedHeader('OpenAI-Organization', $this->config['orgid']);
50
        } else {
51
            return $request->withAddedHeader('Authorization', "Bearer {$this->config['apikey']}");
52
        }
53
    }
54
 
55
    #[\Override]
56
    public static function get_action_settings(
57
        string $action,
58
        array $customdata = [],
59
    ): action_settings_form|bool {
60
        $actionname = substr($action, (strrpos($action, '\\') + 1));
61
        $customdata['actionname'] = $actionname;
62
        $customdata['action'] = $action;
63
        if ($actionname === 'generate_text' || $actionname === 'summarise_text' || $actionname === 'explain_text') {
64
            return new form\action_generate_text_form(customdata: $customdata);
65
        } else if ($actionname === 'generate_image') {
66
            return new form\action_generate_image_form(customdata: $customdata);
67
        }
68
 
69
        return false;
70
    }
71
 
72
    #[\Override]
73
    public static function get_action_setting_defaults(string $action): array {
74
        $actionname = substr($action, (strrpos($action, '\\') + 1));
75
        $customdata = [
76
            'actionname' => $actionname,
77
            'action' => $action,
78
            'providername' => 'aiprovider_openai',
79
        ];
80
        if ($actionname === 'generate_text' || $actionname === 'summarise_text' || $actionname === 'explain_text') {
81
            $mform = new form\action_generate_text_form(customdata: $customdata);
82
            return $mform->get_defaults();
83
        } else if ($actionname === 'generate_image') {
84
            $mform = new form\action_generate_image_form(customdata: $customdata);
85
            return $mform->get_defaults();
86
        }
87
 
88
        return [];
89
    }
90
 
91
    /**
92
     * Check this provider has the minimal configuration to work.
93
     *
94
     * @return bool Return true if configured.
95
     */
96
    public function is_provider_configured(): bool {
97
        return !empty($this->config['apikey']);
98
    }
99
}