-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlib.php
executable file
·162 lines (147 loc) · 5.73 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* General plugin functions
*
* @package block_openai_chat
* @copyright 2023 Bryce Yoder <me@bryceyoder.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Fetch the current API type from the database, defaulting to "chat"
* @return String: the API type (chat|azure|assistant)
*/
function get_type_to_display() {
$stored_type = get_config('block_openai_chat', 'type');
if ($stored_type) {
return $stored_type;
}
return 'chat';
}
/**
* Use an API key to fetch a list of assistants from a user's OpenAI account
* @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.
* @return Array: The list of assistants
*/
function fetch_assistants_array($block_id = null) {
global $DB;
if (!$block_id) {
$apikey = get_config('block_openai_chat', 'apikey');
} else {
$instance_record = $DB->get_record('block_instances', ['blockname' => 'openai_chat', 'id' => $block_id], '*');
$instance = block_instance('openai_chat', $instance_record);
$apikey = $instance->config->apikey ? $instance->config->apikey : get_config('block_openai_chat', 'apikey');
}
if (!$apikey) {
return [];
}
$curl = new \curl();
$curl->setopt(array(
'CURLOPT_HTTPHEADER' => array(
'Authorization: Bearer ' . $apikey,
'Content-Type: application/json',
'OpenAI-Beta: assistants=v2'
),
));
$response = $curl->get("https://api.openai.com/v1/assistants?order=desc");
$response = json_decode($response);
$assistant_array = [];
if (property_exists($response, 'data')) {
foreach ($response->data as $assistant) {
$assistant_array[$assistant->id] = $assistant->name;
}
}
return $assistant_array;
}
/**
* Return a list of available models, and the type of each model.
* @return Array: The list of model info
*/
function get_models() {
return [
"models" => [
'gpt-4o-2024-05-13' => 'gpt-4o-2024-05-13',
'gpt-4o' => 'gpt-4o',
'gpt-4-turbo-preview' => 'gpt-4-turbo-preview',
'gpt-4-turbo-2024-04-09' => 'gpt-4-turbo-2024-04-09',
'gpt-4-turbo' => 'gpt-4-turbo',
'gpt-4-32k-0314' => 'gpt-4-32k-0314',
'gpt-4-1106-vision-preview' => 'gpt-4-1106-vision-preview',
'gpt-4-1106-preview' => 'gpt-4-1106-preview',
'gpt-4-0613' => 'gpt-4-0613',
'gpt-4-0314' => 'gpt-4-0314',
'gpt-4-0125-preview' => 'gpt-4-0125-preview',
'gpt-4' => 'gpt-4',
'gpt-3.5-turbo-16k-0613' => 'gpt-3.5-turbo-16k-0613',
'gpt-3.5-turbo-16k' => 'gpt-3.5-turbo-16k',
'gpt-3.5-turbo-1106' => 'gpt-3.5-turbo-1106',
'gpt-3.5-turbo-0613' => 'gpt-3.5-turbo-0613',
'gpt-3.5-turbo-0301' => 'gpt-3.5-turbo-0301',
'gpt-3.5-turbo-0125' => 'gpt-3.5-turbo-0125',
'gpt-3.5-turbo' => 'gpt-3.5-turbo'
],
"types" => [
'gpt-4o-2024-05-13' => 'chat',
'gpt-4o' => 'chat',
'gpt-4-turbo-preview' => 'chat',
'gpt-4-turbo-2024-04-09' => 'chat',
'gpt-4-turbo' => 'chat',
'gpt-4-32k-0314' => 'chat',
'gpt-4-1106-vision-preview' => 'chat',
'gpt-4-1106-preview' => 'chat',
'gpt-4-0613' => 'chat',
'gpt-4-0314' => 'chat',
'gpt-4-0125-preview' => 'chat',
'gpt-4' => 'chat',
'gpt-3.5-turbo-16k-0613' => 'chat',
'gpt-3.5-turbo-16k' => 'chat',
'gpt-3.5-turbo-1106' => 'chat',
'gpt-3.5-turbo-0613' => 'chat',
'gpt-3.5-turbo-0301' => 'chat',
'gpt-3.5-turbo-0125' => 'chat',
'gpt-3.5-turbo' => 'chat'
]
];
}
/**
* If setting is enabled, log the user's message and the AI response
* @param string usermessage: The text sent from the user
* @param string airesponse: The text returned by the AI
*/
function log_message($usermessage, $airesponse, $context) {
global $USER, $DB;
if (!get_config('block_openai_chat', 'logging')) {
return;
}
$DB->insert_record('block_openai_chat_log', (object) [
'userid' => $USER->id,
'usermessage' => $usermessage,
'airesponse' => $airesponse,
'contextid' => $context->id,
'timecreated' => time()
]);
}
function block_openai_chat_extend_navigation_course($nav, $course, $context) {
if ($nav->get('coursereports')) {
$nav->get('coursereports')->add(
get_string('openai_chat_logs', 'block_openai_chat'),
new moodle_url('/blocks/openai_chat/report.php', ['courseid' => $course->id]),
navigation_node::TYPE_SETTING,
null
);
}
}