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_ollama;
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_ollama
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
    #[\Override]
32
    protected function get_system_instruction(): string {
33
        return $this->provider->actionconfig[$this->action::class]['settings']['systeminstruction'];
34
    }
35
 
36
    #[\Override]
37
    protected function create_request_object(string $userid): RequestInterface {
38
        // Create the request object.
39
        $requestobj = new \stdClass();
40
        $requestobj->model = $this->get_model();
41
        $requestobj->stream = false;
42
        $requestobj->prompt = $this->action->get_configuration('prompttext');
43
        $requestobj->user = $userid;
44
        $requestobj->options = new \stdClass();
45
 
46
        // If there is a system string available, use it.
47
        $systeminstruction = $this->get_system_instruction();
48
        if (!empty($systeminstruction)) {
49
            $requestobj->system = $systeminstruction;
50
        }
51
 
52
        // Append the extra model settings.
53
        $modelsettings = $this->get_model_settings();
54
        foreach ($modelsettings as $setting => $value) {
55
            $requestobj->options->$setting = $value;
56
        }
57
 
58
        return new Request(
59
            method: 'POST',
60
            uri: '',
61
            body: json_encode($requestobj),
62
            headers: [
63
                'Content-Type' => 'application/json',
64
            ],
65
        );
66
    }
67
 
68
    /**
69
     * Handle a successful response from the external AI api.
70
     *
71
     * @param ResponseInterface $response The response object.
72
     * @return array The response.
73
     */
74
    protected function handle_api_success(ResponseInterface $response): array {
75
        $responsebody = $response->getBody();
76
        $bodyobj = json_decode($responsebody->getContents());
77
 
78
        return [
79
            'success' => true,
80
            'generatedcontent' => $bodyobj->response,
81
            'finishreason' => $bodyobj->done_reason,
82
            'prompttokens' => $bodyobj->prompt_eval_count,
83
            'completiontokens' => $bodyobj->eval_count,
84
            'model' => $bodyobj->model ?? $this->get_model(), // Fallback to config model.
85
        ];
86
    }
87
}