Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
9 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
/**
18
 * API endpoint for retrieving thread history
19
 *
20
 * @package    block_openai_chat
21
 * @copyright  2023 Bryce Yoder <me@bryceyoder.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require_once('../../../config.php');
26
require_once($CFG->libdir . '/filelib.php');
27
require_once($CFG->dirroot . '/blocks/openai_chat/lib.php');
28
 
29
if (get_config('block_openai_chat', 'restrictusage') !== "0") {
30
    require_login();
31
}
32
 
33
$thread_id = required_param('thread_id', PARAM_NOTAGS);
34
$apikey = get_config('block_openai_chat', 'apikey');
35
 
36
$curl = new \curl();
37
$curl->setopt(array(
38
    'CURLOPT_HTTPHEADER' => array(
39
        'Authorization: Bearer ' . $apikey,
40
        'Content-Type: application/json',
41
        'OpenAI-Beta: assistants=v2'
42
    ),
43
));
44
 
45
$response = $curl->get("https://api.openai.com/v1/threads/$thread_id/messages");
46
$response = json_decode($response);
47
 
48
if (property_exists($response, 'error')) {
49
    throw new \Exception($response->error->message);
50
}
51
 
52
$api_response = [];
53
$message_list = array_reverse($response->data);
54
 
55
foreach ($message_list as $message) {
56
    array_push($api_response, [
57
        "id" => $message->id,
58
        "role" => $message->role,
59
        "message" => $message->content[0]->text->value
60
    ]);
61
}
62
 
63
$api_response = json_encode($api_response);
64
echo $api_response;