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 GPT completion
|
|
|
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 |
use \block_openai_chat\completion;
|
|
|
26 |
|
|
|
27 |
require_once('../../../config.php');
|
|
|
28 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
29 |
require_once($CFG->dirroot . '/blocks/openai_chat/lib.php');
|
|
|
30 |
|
|
|
31 |
global $DB, $PAGE;
|
|
|
32 |
|
|
|
33 |
if (get_config('block_openai_chat', 'restrictusage') !== "0") {
|
|
|
34 |
require_login();
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
|
38 |
header("Location: $CFG->wwwroot");
|
|
|
39 |
die();
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
$body = json_decode(file_get_contents('php://input'), true);
|
|
|
43 |
$message = clean_param($body['message'], PARAM_NOTAGS);
|
|
|
44 |
$history = clean_param_array($body['history'], PARAM_NOTAGS, true);
|
|
|
45 |
$block_id = clean_param($body['blockId'], PARAM_INT, true);
|
|
|
46 |
$thread_id = clean_param($body['threadId'], PARAM_NOTAGS, true);
|
|
|
47 |
|
|
|
48 |
// So that we're not leaking info to the client like API key, the block makes an API request including its ID
|
|
|
49 |
// Then we can look up that specific block to pull out its config data
|
|
|
50 |
$instance_record = $DB->get_record('block_instances', ['blockname' => 'openai_chat', 'id' => $block_id], '*');
|
|
|
51 |
$instance = block_instance('openai_chat', $instance_record);
|
|
|
52 |
if (!$instance) {
|
|
|
53 |
print_error('invalidblockinstance', 'error', $id);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
$context = context::instance_by_id($instance_record->parentcontextid);
|
|
|
57 |
try {
|
|
|
58 |
$context = $context->get_course_context();
|
|
|
59 |
} catch (Exception $e) {}
|
|
|
60 |
|
|
|
61 |
$PAGE->set_context($context);
|
|
|
62 |
|
|
|
63 |
$block_settings = [];
|
|
|
64 |
$setting_names = [
|
|
|
65 |
'sourceoftruth',
|
|
|
66 |
'prompt',
|
|
|
67 |
'instructions',
|
|
|
68 |
'username',
|
|
|
69 |
'assistantname',
|
|
|
70 |
'apikey',
|
|
|
71 |
'model',
|
|
|
72 |
'temperature',
|
|
|
73 |
'maxlength',
|
|
|
74 |
'topp',
|
|
|
75 |
'frequency',
|
|
|
76 |
'presence',
|
|
|
77 |
'assistant'
|
|
|
78 |
];
|
|
|
79 |
foreach ($setting_names as $setting) {
|
|
|
80 |
if ($instance->config && property_exists($instance->config, $setting)) {
|
|
|
81 |
$block_settings[$setting] = $instance->config->$setting ? $instance->config->$setting : "";
|
|
|
82 |
} else {
|
|
|
83 |
$block_settings[$setting] = "";
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$engine_class;
|
|
|
88 |
$model = get_config('block_openai_chat', 'model');
|
|
|
89 |
$api_type = get_config('block_openai_chat', 'type');
|
|
|
90 |
$engine_class = "\block_openai_chat\completion\\$api_type";
|
|
|
91 |
|
|
|
92 |
$completion = new $engine_class(...[$model, $message, $history, $block_settings, $thread_id]);
|
|
|
93 |
$response = $completion->create_completion($PAGE->context);
|
|
|
94 |
|
|
|
95 |
// Format the markdown of each completion message into HTML.
|
|
|
96 |
$response["message"] = format_text($response["message"], FORMAT_MARKDOWN, ['context' => $context]);
|
|
|
97 |
|
|
|
98 |
// Log the message
|
|
|
99 |
log_message($message, $response['message'], $context);
|
|
|
100 |
|
|
|
101 |
$response = json_encode($response);
|
|
|
102 |
echo $response;
|