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 aiprovider_openai;
18
 
19
use GuzzleHttp\Psr7\Request;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
 
23
/**
24
 * Class process text generation.
25
 *
26
 * @package    aiprovider_openai
27
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class process_generate_text extends abstract_processor {
31
 
32
    #[\Override]
33
    protected function get_system_instruction(): string {
34
        return $this->provider->actionconfig[$this->action::class]['settings']['systeminstruction'];
35
    }
36
 
37
    #[\Override]
38
    protected function create_request_object(string $userid): RequestInterface {
39
        // Create the user object.
40
        $userobj = new \stdClass();
41
        $userobj->role = 'user';
42
        $userobj->content = $this->action->get_configuration('prompttext');
43
 
44
        // Create the request object.
45
        $requestobj = new \stdClass();
46
        $requestobj->model = $this->get_model();
47
        $requestobj->user = $userid;
48
 
49
        // If there is a system string available, use it.
50
        $systeminstruction = $this->get_system_instruction();
51
        if (!empty($systeminstruction)) {
52
            $systemobj = new \stdClass();
53
            $systemobj->role = 'system';
54
            $systemobj->content = $systeminstruction;
55
            $requestobj->messages = [$systemobj, $userobj];
56
        } else {
57
            $requestobj->messages = [$userobj];
58
        }
59
 
60
        // Append the extra model settings.
61
        $modelsettings = $this->get_model_settings();
62
        foreach ($modelsettings as $setting => $value) {
63
            $requestobj->$setting = $value;
64
        }
65
 
66
        return new Request(
67
            method: 'POST',
68
            uri: '',
69
            headers: [
70
                'Content-Type' => 'application/json',
71
            ],
72
            body: json_encode($requestobj),
73
        );
74
    }
75
 
76
    /**
77
     * Handle a successful response from the external AI api.
78
     *
79
     * @param ResponseInterface $response The response object.
80
     * @return array The response.
81
     */
82
    protected function handle_api_success(ResponseInterface $response): array {
83
        $responsebody = $response->getBody();
84
        $bodyobj = json_decode($responsebody->getContents());
85
 
86
        return [
87
            'success' => true,
88
            'id' => $bodyobj->id,
89
            'fingerprint' => $bodyobj->system_fingerprint,
90
            'generatedcontent' => $bodyobj->choices[0]->message->content,
91
            'finishreason' => $bodyobj->choices[0]->finish_reason,
92
            'prompttokens' => $bodyobj->usage->prompt_tokens,
93
            'completiontokens' => $bodyobj->usage->completion_tokens,
94
            'model' => $bodyobj->model ?? $this->get_model(), // Fallback to config model.
95
        ];
96
    }
97
}