1441 |
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 |
* AI data generator for tests.
|
|
|
19 |
*
|
|
|
20 |
* @package core_ai
|
|
|
21 |
* @copyright 2024 David Woloszyn <david.woloszyn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
class core_ai_generator extends component_generator_base {
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Creates AI action registry records.
|
|
|
28 |
*
|
|
|
29 |
* @param array $data
|
|
|
30 |
*/
|
|
|
31 |
public function create_ai_actions(array $data): void {
|
|
|
32 |
global $DB;
|
|
|
33 |
|
|
|
34 |
if (!isset($data['actionname'])) {
|
|
|
35 |
throw new Exception('\'ai actions\' requires the field \'actionname\' to be specified');
|
|
|
36 |
}
|
|
|
37 |
if (!isset($data['success'])) {
|
|
|
38 |
throw new Exception('\'ai actions\' requires the field \'success\' to be specified');
|
|
|
39 |
}
|
|
|
40 |
if (!isset($data['userid'])) {
|
|
|
41 |
throw new Exception('\'ai actions\' requires the field \'user\' to be specified');
|
|
|
42 |
}
|
|
|
43 |
if (!isset($data['contextid'])) {
|
|
|
44 |
throw new Exception('\'ai actions\' requires the field \'contextid\' to be specified');
|
|
|
45 |
}
|
|
|
46 |
if (!isset($data['provider'])) {
|
|
|
47 |
throw new Exception('\'ai actions\' requires the field \'provider\' to be specified');
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
// Create the child action record.
|
|
|
51 |
$child = new stdClass();
|
|
|
52 |
$child->prompt = 'Prompt text';
|
|
|
53 |
|
|
|
54 |
// Generate image actions need to be structured differently.
|
|
|
55 |
if ($data['actionname'] === 'generate_image') {
|
|
|
56 |
$child->numberimages = 1;
|
|
|
57 |
$child->quality = 'hd';
|
|
|
58 |
$child->aspectratio = 'landscape';
|
|
|
59 |
$child->style = 'vivid';
|
|
|
60 |
$child->sourceurl = 'http://localhost/yourimage';
|
|
|
61 |
$child->revisedprompt = 'Revised prompt';
|
|
|
62 |
} else {
|
|
|
63 |
// Generate text (and variants).
|
|
|
64 |
$child->generatedcontent = 'Your generated content';
|
|
|
65 |
$child->prompttokens = $data['prompttokens'] ?? 111;
|
|
|
66 |
$child->completiontoken = $data['completiontokens'] ?? 222;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
// Simulate an error.
|
|
|
70 |
if ($data['success'] == 0) {
|
|
|
71 |
$data['errorcode'] = 403;
|
|
|
72 |
$data['errormessage'] = 'Forbidden';
|
|
|
73 |
// Unset some values that won't be present with an error.
|
|
|
74 |
unset($child->generatedcontent);
|
|
|
75 |
unset($child->revisedprompt);
|
|
|
76 |
unset($child->prompttokens);
|
|
|
77 |
unset($child->completiontoken);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$childid = $DB->insert_record("ai_action_{$data['actionname']}", $child);
|
|
|
81 |
|
|
|
82 |
// Finalise some fields before inserting.
|
|
|
83 |
$data['actionid'] = $childid;
|
|
|
84 |
$data['timecreated'] = time();
|
|
|
85 |
$data['timecompleted'] = time() + 1;
|
|
|
86 |
$DB->insert_record('ai_action_register', $data);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Creates AI provider instance.
|
|
|
91 |
*
|
|
|
92 |
* @param array $data
|
|
|
93 |
* @return void
|
|
|
94 |
*/
|
|
|
95 |
public function create_ai_provider(array $data) {
|
|
|
96 |
$manager = \core\di::get(\core_ai\manager::class);
|
|
|
97 |
$classname = $data['provider'] . '\\' . 'provider';
|
|
|
98 |
$name = $data['name'];
|
|
|
99 |
$enabled = $data['enabled'];
|
|
|
100 |
unset($data['provider'], $data['name'], $data['enabled']);
|
|
|
101 |
$manager->create_provider_instance(
|
|
|
102 |
classname: $classname,
|
|
|
103 |
name: $name,
|
|
|
104 |
enabled: $enabled,
|
|
|
105 |
config: $data,
|
|
|
106 |
);
|
|
|
107 |
}
|
|
|
108 |
}
|