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 |
* Class providing completions for Azure
|
|
|
19 |
*
|
|
|
20 |
* @package block_openai_chat
|
|
|
21 |
* @copyright 2024 Bryce Yoder <me@bryceyoder.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_openai_chat\completion;
|
|
|
26 |
|
|
|
27 |
use block_openai_chat\completion;
|
|
|
28 |
defined('MOODLE_INTERNAL') || die;
|
|
|
29 |
|
|
|
30 |
class azure extends \block_openai_chat\completion\chat {
|
|
|
31 |
|
|
|
32 |
private $resourcename;
|
|
|
33 |
private $deploymentid;
|
|
|
34 |
private $apiversion;
|
|
|
35 |
|
|
|
36 |
public function __construct($model, $message, $history, $block_settings, $thread_id = null) {
|
|
|
37 |
parent::__construct($model, $message, $history, $block_settings);
|
|
|
38 |
|
|
|
39 |
$this->resourcename = $this->get_setting('resourcename');
|
|
|
40 |
$this->deploymentid = $this->get_setting('deploymentid');
|
|
|
41 |
$this->apiversion = $this->get_setting('apiversion');
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Given everything we know after constructing the parent, create a completion by constructing the prompt and making the api call
|
|
|
46 |
* @return JSON: The API response from Azure
|
|
|
47 |
*/
|
|
|
48 |
public function create_completion($context) {
|
|
|
49 |
if ($this->sourceoftruth) {
|
|
|
50 |
$this->sourceoftruth = format_string($this->sourceoftruth, true, ['context' => $context]);
|
|
|
51 |
$this->prompt .= get_string('sourceoftruthreinforcement', 'block_openai_chat');
|
|
|
52 |
}
|
|
|
53 |
$this->prompt .= "\n\n";
|
|
|
54 |
|
|
|
55 |
$history_json = $this->format_history();
|
|
|
56 |
array_unshift($history_json, ["role" => "system", "content" => $this->prompt]);
|
|
|
57 |
array_unshift($history_json, ["role" => "system", "content" => $this->sourceoftruth]);
|
|
|
58 |
|
|
|
59 |
array_push($history_json, ["role" => "user", "content" => $this->message]);
|
|
|
60 |
|
|
|
61 |
$response_data = $this->make_api_call($history_json);
|
|
|
62 |
return $response_data;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Make the actual API call to Azure
|
|
|
67 |
* @return JSON: The response from Azure
|
|
|
68 |
*/
|
|
|
69 |
private function make_api_call($history) {
|
|
|
70 |
|
|
|
71 |
$curlbody = [
|
|
|
72 |
"model" => $this->model,
|
|
|
73 |
"messages" => $history,
|
|
|
74 |
"temperature" => (float) $this->temperature,
|
|
|
75 |
"max_tokens" => (int) $this->maxlength,
|
|
|
76 |
"top_p" => (float) $this->topp,
|
|
|
77 |
"frequency_penalty" => (float) $this->frequency,
|
|
|
78 |
"presence_penalty" => (float) $this->presence,
|
|
|
79 |
"stop" => $this->username . ":"
|
|
|
80 |
];
|
|
|
81 |
|
|
|
82 |
$curl = new \curl();
|
|
|
83 |
$curl->setopt(array(
|
|
|
84 |
'CURLOPT_HTTPHEADER' => array(
|
|
|
85 |
'api-key: ' . $this->apikey,
|
|
|
86 |
'Content-Type: application/json'
|
|
|
87 |
),
|
|
|
88 |
));
|
|
|
89 |
|
|
|
90 |
$response = $curl->post(
|
|
|
91 |
"https://" . $this->resourcename . ".openai.azure.com/openai/deployments/" . $this->deploymentid . "/chat/completions?api-version=" . $this->apiversion,
|
|
|
92 |
json_encode($curlbody)
|
|
|
93 |
);
|
|
|
94 |
$response = json_decode($response);
|
|
|
95 |
|
|
|
96 |
$message = null;
|
|
|
97 |
if (property_exists($response, 'error')) {
|
|
|
98 |
$message = 'ERROR: ' . $response->error->message;
|
|
|
99 |
} else {
|
|
|
100 |
$message = $response->choices[0]->message->content;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return [
|
|
|
104 |
"id" => property_exists($response, 'id') ? $response->id : 'error',
|
|
|
105 |
"message" => $message
|
|
|
106 |
];
|
|
|
107 |
}
|
|
|
108 |
}
|