Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * General use steps definitions.
19
 *
20
 * @package core_ai
21
 * @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
26
 
27
require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
28
 
29
use Behat\Gherkin\Node\TableNode;
30
 
31
/**
32
 * Steps definitions specific to the AI Subsystem.
33
 *
34
 * @package core_ai
35
 * @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
36
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class behat_core_ai extends behat_base {
39
    /**
40
     * Change the enabled state of an AI provider plugin.
41
     *
42
     * @Then /^I "(?P<state>(?:[^"]|\\")*)" the ai provider with name "(?P<provider>(?:[^"]|\\")*)"$/
43
     *
44
     * @param string $state The state to set the plugin to.
45
     * @param string $providername The name of the AI provider plugin.
46
     */
47
    #[\core\attribute\example('Given I "disable" the ai provider with name "OpenAI API test"')]
48
    public function i_change_the_ai_provider_state_with_name(string $state, string $providername): void {
49
        $manager = \core\di::get(\core_ai\manager::class);
50
        $providers = $manager->get_provider_instances(['name' => $providername]);
51
        $provider = reset($providers);
52
        if ($state == 'disable') {
53
            $manager->disable_provider_instance($provider);
54
        } else {
55
            $manager->enable_provider_instance($provider);
56
        }
57
    }
58
 
59
    /**
60
     * Set action configuration for AI provider instances.
61
     *
62
     * @Given /^I set the following action configuration for ai provider with name "(?P<providername>(?:[^"]|\\")*)":$/
63
     *
64
     * @param string $providername The name of the ai provider to configure actions for.
65
     * @param TableNode $data
66
     */
67
    #[\core\attribute\example('I set the following action configuration for ai provider with name "OpenAI API test":
68
        | action         | enabled | model | endpoint                                            |
69
        | generate_text  | 1       | gpt-3 | https://api.openai.com/v1/engines/gpt-3/completions |
70
        | summarise_text | 0       | gpt-4 |                                                     |')]
71
    public function configure_provider_action(string $providername, TableNode $data) {
72
        $manager = \core\di::get(\core_ai\manager::class);
73
        $providers = $manager->get_provider_instances(['name' => $providername]);
74
        $provider = reset($providers);
75
        $rows = $data->getHash();
76
        $actiondata = [];
77
        foreach ($rows as $row) {
78
            $action = 'core_ai\\aiactions\\' . $row['action'];
79
            $actiondata[$action]['enabled'] = $row['enabled'];
80
            unset ($row['action'], $row['enabled']);
81
            $actiondata[$action]['settings'] = $row;
82
        }
83
        $manager->update_provider_instance(
84
            provider: $provider,
85
            actionconfig: $actiondata
86
        );
87
    }
88
}