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
namespace core_ai\aiactions\responses;
18
 
19
/**
20
 * Explain text action response class.
21
 *
22
 * Any method that processes an action must return an instance of this class.
23
 *
24
 * @package    core_ai
25
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class response_explain_text extends response_base {
29
    /** @var string|null A unique identifier for the chat completion, returned by the AI. */
30
    private ?string $id = null;
31
 
32
    /** @var string|null This fingerprint represents the backend configuration that the model runs with. */
33
    private ?string $fingerprint = null;
34
 
35
    /** @var string|null The contents of the generated message. */
36
    private ?string $generatedcontent = null;
37
 
38
    /** @var string|null The reason the model stopped generating tokens. */
39
    private ?string $finishreason = null;
40
 
41
    /** @var string|null Number of tokens in the prompt. */
42
    private ?string $prompttokens = null;
43
 
44
    /** @var string|null Number of tokens in the generated completion. */
45
    private ?string $completiontokens = null;
46
 
47
    /**
48
     * Constructor.
49
     *
50
     * @param bool $success The success status of the action.
51
     * @param int $errorcode Error code. Must exist if success is false.
52
     * @param string $errormessage Error message. Must exist if success is false
53
     */
54
    public function __construct(
55
        bool $success,
56
        int $errorcode = 0,
57
        string $errormessage = '',
58
    ) {
59
        parent::__construct(
60
            success: $success,
61
            actionname: 'explain_text',
62
            errorcode: $errorcode,
63
            errormessage: $errormessage,
64
        );
65
    }
66
 
67
    #[\Override]
68
    public function set_response_data(array $response): void {
69
        $this->id = $response['id'] ?? null;
70
        $this->fingerprint = $response['fingerprint'] ?? null;
71
        $this->generatedcontent = $response['generatedcontent'] ?? null;
72
        $this->finishreason = $response['finishreason'] ?? null;
73
        $this->prompttokens = $response['prompttokens'] ?? null;
74
        $this->completiontokens = $response['completiontokens'] ?? null;
75
        $this->model = $response['model'] ?? null;
76
    }
77
 
78
    #[\Override]
79
    public function get_response_data(): array {
80
        return [
81
            'id' => $this->id,
82
            'fingerprint' => $this->fingerprint,
83
            'generatedcontent' => $this->generatedcontent,
84
            'finishreason' => $this->finishreason,
85
            'prompttokens' => $this->prompttokens,
86
            'completiontokens' => $this->completiontokens,
87
            'model' => $this->model,
88
        ];
89
    }
90
}