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
 * General plugin functions
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
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * Fetch the current API type from the database, defaulting to "chat"
29
 * @return String: the API type (chat|azure|assistant)
30
 */
31
function get_type_to_display() {
32
    $stored_type = get_config('block_openai_chat', 'type');
33
    if ($stored_type) {
34
        return $stored_type;
35
    }
36
 
37
    return 'chat';
38
}
39
 
40
/**
41
 * Use an API key to fetch a list of assistants from a user's OpenAI account
42
 * @param Int (optional): The ID of a block instance. If this is passed, the API can be pulled from the block rather than the site level.
43
 * @return Array: The list of assistants
44
 */
45
function fetch_assistants_array($block_id = null) {
46
    global $DB;
47
 
48
    if (!$block_id) {
49
        $apikey = get_config('block_openai_chat', 'apikey');
50
    } else {
51
        $instance_record = $DB->get_record('block_instances', ['blockname' => 'openai_chat', 'id' => $block_id], '*');
52
        $instance = block_instance('openai_chat', $instance_record);
53
        $apikey = $instance->config->apikey ? $instance->config->apikey : get_config('block_openai_chat', 'apikey');
54
    }
55
 
56
    if (!$apikey) {
57
        return [];
58
    }
59
 
60
    $curl = new \curl();
61
    $curl->setopt(array(
62
        'CURLOPT_HTTPHEADER' => array(
63
            'Authorization: Bearer ' . $apikey,
64
            'Content-Type: application/json',
65
            'OpenAI-Beta: assistants=v2'
66
        ),
67
    ));
68
 
69
    $response = $curl->get("https://api.openai.com/v1/assistants?order=desc");
70
    $response = json_decode($response);
71
    $assistant_array = [];
72
    if (property_exists($response, 'data')) {
73
        foreach ($response->data as $assistant) {
74
            $assistant_array[$assistant->id] = $assistant->name;
75
        }
76
    }
77
 
78
    return $assistant_array;
79
}
80
 
81
/**
82
 * Return a list of available models, and the type of each model.
83
 * @return Array: The list of model info
84
 */
85
function get_models() {
86
    return [
87
        "models" => [
88
            'gpt-4o-2024-05-13' => 'gpt-4o-2024-05-13',
89
            'gpt-4o' => 'gpt-4o',
90
            'gpt-4-turbo-preview' => 'gpt-4-turbo-preview',
91
            'gpt-4-turbo-2024-04-09' => 'gpt-4-turbo-2024-04-09',
92
            'gpt-4-turbo' => 'gpt-4-turbo',
93
            'gpt-4-32k-0314' => 'gpt-4-32k-0314',
94
            'gpt-4-1106-vision-preview' => 'gpt-4-1106-vision-preview',
95
            'gpt-4-1106-preview' => 'gpt-4-1106-preview',
96
            'gpt-4-0613' => 'gpt-4-0613',
97
            'gpt-4-0314' => 'gpt-4-0314',
98
            'gpt-4-0125-preview' => 'gpt-4-0125-preview',
99
            'gpt-4' => 'gpt-4',
100
            'gpt-3.5-turbo-16k-0613' => 'gpt-3.5-turbo-16k-0613',
101
            'gpt-3.5-turbo-16k' => 'gpt-3.5-turbo-16k',
102
            'gpt-3.5-turbo-1106' => 'gpt-3.5-turbo-1106',
103
            'gpt-3.5-turbo-0613' => 'gpt-3.5-turbo-0613',
104
            'gpt-3.5-turbo-0301' => 'gpt-3.5-turbo-0301',
105
            'gpt-3.5-turbo-0125' => 'gpt-3.5-turbo-0125',
106
            'gpt-3.5-turbo' => 'gpt-3.5-turbo'
107
        ],
108
        "types" => [
109
            'gpt-4o-2024-05-13'          =>  'chat',
110
            'gpt-4o'                     =>  'chat',
111
            'gpt-4-turbo-preview'        =>  'chat',
112
            'gpt-4-turbo-2024-04-09'     =>  'chat',
113
            'gpt-4-turbo'                =>  'chat',
114
            'gpt-4-32k-0314'             =>  'chat',
115
            'gpt-4-1106-vision-preview'  =>  'chat',
116
            'gpt-4-1106-preview'         =>  'chat',
117
            'gpt-4-0613'                 =>  'chat',
118
            'gpt-4-0314'                 =>  'chat',
119
            'gpt-4-0125-preview'         =>  'chat',
120
            'gpt-4'                      =>  'chat',
121
            'gpt-3.5-turbo-16k-0613'     =>  'chat',
122
            'gpt-3.5-turbo-16k'          =>  'chat',
123
            'gpt-3.5-turbo-1106'         =>  'chat',
124
            'gpt-3.5-turbo-0613'         =>  'chat',
125
            'gpt-3.5-turbo-0301'         =>  'chat',
126
            'gpt-3.5-turbo-0125'         =>  'chat',
127
            'gpt-3.5-turbo'              =>  'chat'
128
        ]
129
    ];
130
}
131
 
132
/**
133
 * If setting is enabled, log the user's message and the AI response
134
 * @param string usermessage: The text sent from the user
135
 * @param string airesponse: The text returned by the AI
136
 */
137
function log_message($usermessage, $airesponse, $context) {
138
    global $USER, $DB;
139
 
140
    if (!get_config('block_openai_chat', 'logging')) {
141
        return;
142
    }
143
 
144
    $DB->insert_record('block_openai_chat_log', (object) [
145
        'userid' => $USER->id,
146
        'usermessage' => $usermessage,
147
        'airesponse' => $airesponse,
148
        'contextid' => $context->id,
149
        'timecreated' => time()
150
    ]);
151
}
152
 
153
function block_openai_chat_extend_navigation_course($nav, $course, $context) {
154
    if ($nav->get('coursereports')) {
155
        $nav->get('coursereports')->add(
156
            get_string('openai_chat_logs', 'block_openai_chat'),
157
            new moodle_url('/blocks/openai_chat/report.php', ['courseid' => $course->id]),
158
            navigation_node::TYPE_SETTING,
159
            null
160
        );
161
    }
162
}