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 aiplacement_editor\external;
18
 
19
use aiplacement_editor\utils;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_value;
23
 
24
/**
25
 * External API to call an action for this placement.
26
 *
27
 * @package    aiplacement_editor
28
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class generate_text extends external_api {
32
    /**
33
     * Generate image parameters.
34
     *
35
     * @return external_function_parameters
36
     * @since  Moodle 4.5
37
     */
38
    public static function execute_parameters(): external_function_parameters {
39
        return new external_function_parameters([
40
            'contextid' => new external_value(
41
                PARAM_INT,
42
                'The context ID',
43
                VALUE_REQUIRED,
44
            ),
45
            'prompttext' => new external_value(
46
                PARAM_RAW,
47
                'The prompt text for the AI service',
48
                VALUE_REQUIRED,
49
            ),
50
        ]);
51
    }
52
 
53
    /**
54
     * Generate text from the AI placement.
55
     *
56
     * @param int $contextid The context ID.
57
     * @param string $prompttext The data encoded as a json array.
58
     * @return array The generated content.
59
     * @since  Moodle 4.5
60
     */
61
    public static function execute(
62
        int $contextid,
63
        string $prompttext
64
    ): array {
65
        global $USER;
66
        // Parameter validation.
67
        [
68
            'contextid' => $contextid,
69
            'prompttext' => $prompttext,
70
        ] = self::validate_parameters(self::execute_parameters(), [
71
            'contextid' => $contextid,
72
            'prompttext' => $prompttext,
73
        ]);
74
        // Context validation and permission check.
75
        // Get the context from the passed in ID.
76
        $context = \core\context::instance_by_id($contextid);
77
 
78
        // Check the user has permission to use the AI service.
79
        self::validate_context($context);
80
        if (!utils::is_html_editor_placement_action_available($context, 'generate_text', \core_ai\aiactions\generate_text::class)) {
81
            throw new \moodle_exception('noeditor', 'aiplacement_editor');
82
        }
83
 
84
        // Prepare the action.
85
        $action = new \core_ai\aiactions\generate_text(
86
            contextid: $contextid,
87
            userid: $USER->id,
88
            prompttext: $prompttext,
89
        );
90
 
91
        // Send the action to the AI manager.
92
        $manager = \core\di::get(\core_ai\manager::class);
93
        $response = $manager->process_action($action);
94
        // Return the response.
95
        return [
96
            'success' => $response->get_success(),
97
            'generatedcontent' => $response->get_response_data()['generatedcontent'] ?? '',
98
            'finishreason' => $response->get_response_data()['finishreason'] ?? '',
99
            'errorcode' => $response->get_errorcode(),
100
            'error' => $response->get_errormessage(),
101
            'timecreated' => $response->get_timecreated(),
102
            'prompttext' => $prompttext,
103
        ];
104
    }
105
 
106
    /**
107
     * Generate content return value.
108
     *
109
     * @return external_function_parameters
110
     * @since  Moodle 4.5
111
     */
112
    public static function execute_returns(): external_function_parameters {
113
        return new external_function_parameters([
114
            'success' => new external_value(
115
                PARAM_BOOL,
116
                'Was the request successful',
117
                VALUE_REQUIRED,
118
            ),
119
            'timecreated' => new external_value(
120
                PARAM_INT,
121
                'The time the request was created',
122
                VALUE_REQUIRED,
123
            ),
124
            'prompttext' => new external_value(
125
                PARAM_RAW,
126
                'The prompt text for the AI service',
127
                VALUE_REQUIRED,
128
            ),
129
            'generatedcontent' => new external_value(
130
                PARAM_TEXT,
131
                'The text generated by AI.',
132
                VALUE_DEFAULT,
133
            ),
134
            'finishreason' => new external_value(
135
                PARAM_ALPHAEXT,
136
                'The reason generation was stopped',
137
                VALUE_DEFAULT,
138
                'stop',
139
            ),
140
            'errorcode' => new external_value(
141
                PARAM_INT,
142
                'Error code if any',
143
                VALUE_DEFAULT,
144
                0,
145
            ),
146
            'error' => new external_value(
147
                PARAM_TEXT,
148
                'Error message if any',
149
                VALUE_DEFAULT,
150
                '',
151
            ),
152
        ]);
153
    }
154
}