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