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 |
* Base completion object class
|
|
|
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 |
namespace block_openai_chat;
|
|
|
26 |
defined('MOODLE_INTERNAL') || die;
|
|
|
27 |
|
|
|
28 |
class completion {
|
|
|
29 |
|
|
|
30 |
protected $apikey;
|
|
|
31 |
protected $message;
|
|
|
32 |
protected $history;
|
|
|
33 |
|
|
|
34 |
protected $assistantname;
|
|
|
35 |
protected $username;
|
|
|
36 |
protected $prompt;
|
|
|
37 |
protected $sourceoftruth;
|
|
|
38 |
protected $model;
|
|
|
39 |
protected $temperature;
|
|
|
40 |
protected $maxlength;
|
|
|
41 |
protected $topp;
|
|
|
42 |
protected $frequency;
|
|
|
43 |
protected $presence;
|
|
|
44 |
|
|
|
45 |
protected $assistant;
|
|
|
46 |
protected $instructions;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Initialize all the class properties that we'll need regardless of model
|
|
|
50 |
* @param string model: The name of the model we're using
|
|
|
51 |
* @param string message: The most recent message sent by the user
|
|
|
52 |
* @param array history: An array of objects containing the history of the conversation
|
|
|
53 |
* @param string block_settings: An object containing the instance-level settings if applicable
|
|
|
54 |
*/
|
|
|
55 |
public function __construct($model, $message, $history, $block_settings) {
|
|
|
56 |
// Set default values
|
|
|
57 |
$this->model = $model;
|
|
|
58 |
$this->apikey = get_config('block_openai_chat', 'apikey');
|
|
|
59 |
|
|
|
60 |
// We fetch defaults for both chat and assistant APIs, even though only one can be active at a time
|
|
|
61 |
// In the past, multiple different completion classes shared API types, so this might happen again
|
|
|
62 |
// Any settings that don't apply to the current API type are just ignored
|
|
|
63 |
|
|
|
64 |
$this->prompt = $this->get_setting('prompt', get_string('defaultprompt', 'block_openai_chat'));
|
|
|
65 |
$this->assistantname = $this->get_setting('assistantname', get_string('defaultassistantname', 'block_openai_chat'));
|
|
|
66 |
$this->username = $this->get_setting('username', get_string('defaultusername', 'block_openai_chat'));
|
|
|
67 |
|
|
|
68 |
$this->temperature = $this->get_setting('temperature', 0.5);
|
|
|
69 |
$this->maxlength = $this->get_setting('maxlength', 500);
|
|
|
70 |
$this->topp = $this->get_setting('topp', 1);
|
|
|
71 |
$this->frequency = $this->get_setting('frequency', 1);
|
|
|
72 |
$this->presence = $this->get_setting('presence', 1);
|
|
|
73 |
|
|
|
74 |
$this->assistant = $this->get_setting('assistant');
|
|
|
75 |
$this->instructions = $this->get_setting('instructions');
|
|
|
76 |
|
|
|
77 |
// Then override with block settings if applicable
|
|
|
78 |
if (get_config('block_openai_chat', 'allowinstancesettings') === "1") {
|
|
|
79 |
foreach ($block_settings as $name => $value) {
|
|
|
80 |
if ($value) {
|
|
|
81 |
$this->$name = $value;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$this->message = $message;
|
|
|
87 |
$this->history = $history;
|
|
|
88 |
|
|
|
89 |
$this->build_source_of_truth($block_settings['sourceoftruth']);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Attempt to get the saved value for a setting; if this isn't set, return a passed default instead
|
|
|
94 |
* @param string settingname: The name of the setting to fetch
|
|
|
95 |
* @param mixed default_value: The default value to return if the setting isn't already set
|
|
|
96 |
* @return mixed: The saved or default value
|
|
|
97 |
*/
|
|
|
98 |
protected function get_setting($settingname, $default_value = null) {
|
|
|
99 |
$setting = get_config('block_openai_chat', $settingname);
|
|
|
100 |
if (!$setting && (float) $setting != 0) {
|
|
|
101 |
$setting = $default_value;
|
|
|
102 |
}
|
|
|
103 |
return $setting;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Make the source of truth ready to add to the prompt by appending some extra information
|
|
|
108 |
* @param string localsourceoftruth: The instance-level source of truth we got from the API call
|
|
|
109 |
*/
|
|
|
110 |
private function build_source_of_truth($localsourceoftruth) {
|
|
|
111 |
$sourceoftruth = get_config('block_openai_chat', 'sourceoftruth');
|
|
|
112 |
|
|
|
113 |
if ($sourceoftruth || $localsourceoftruth) {
|
|
|
114 |
$sourceoftruth =
|
|
|
115 |
get_string('sourceoftruthpreamble', 'block_openai_chat')
|
|
|
116 |
. $sourceoftruth . "\n\n"
|
|
|
117 |
. $localsourceoftruth . "\n\n";
|
|
|
118 |
}
|
|
|
119 |
$this->sourceoftruth = $sourceoftruth;
|
|
|
120 |
}
|
|
|
121 |
}
|