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