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 core\http_client;
|
|
|
20 |
use core_ai\process_base;
|
|
|
21 |
use GuzzleHttp\Exception\RequestException;
|
|
|
22 |
use GuzzleHttp\RequestOptions;
|
|
|
23 |
use Psr\Http\Message\RequestInterface;
|
|
|
24 |
use Psr\Http\Message\ResponseInterface;
|
|
|
25 |
use Psr\Http\Message\UriInterface;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Class process text generation.
|
|
|
29 |
*
|
|
|
30 |
* @package aiprovider_azureai
|
|
|
31 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
abstract class abstract_processor extends process_base {
|
|
|
35 |
/**
|
|
|
36 |
* Get the endpoint URI.
|
|
|
37 |
*
|
|
|
38 |
* @return UriInterface
|
|
|
39 |
*/
|
|
|
40 |
abstract protected function get_endpoint(): UriInterface;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Get the system instructions.
|
|
|
44 |
*
|
|
|
45 |
* @return string
|
|
|
46 |
*/
|
|
|
47 |
protected function get_system_instruction(): string {
|
|
|
48 |
return $this->action::get_system_instruction();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Get the deployment name.
|
|
|
53 |
*
|
|
|
54 |
* @return string
|
|
|
55 |
*/
|
|
|
56 |
protected function get_deployment_name(): string {
|
|
|
57 |
return $this->provider->actionconfig[$this->action::class]['settings']['deployment'];
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Get the api version to use.
|
|
|
62 |
*
|
|
|
63 |
* @return string
|
|
|
64 |
*/
|
|
|
65 |
protected function get_api_version(): string {
|
|
|
66 |
return $this->provider->actionconfig[$this->action::class]['settings']['apiversion'];
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Create the request object to send to the OpenAI API.
|
|
|
71 |
*
|
|
|
72 |
* This object contains all the required parameters for the request.
|
|
|
73 |
*
|
|
|
74 |
* @param string $userid The user id.
|
|
|
75 |
* @return RequestInterface The request object to send to the OpenAI API.
|
|
|
76 |
*/
|
|
|
77 |
abstract protected function create_request_object(
|
|
|
78 |
string $userid,
|
|
|
79 |
): RequestInterface;
|
|
|
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 |
abstract protected function handle_api_success(ResponseInterface $response): array;
|
|
|
88 |
|
|
|
89 |
#[\Override]
|
|
|
90 |
protected function query_ai_api(): array {
|
|
|
91 |
$request = $this->create_request_object(
|
|
|
92 |
userid: $this->provider->generate_userid($this->action->get_configuration('userid')),
|
|
|
93 |
);
|
|
|
94 |
$request = $this->provider->add_authentication_headers($request);
|
|
|
95 |
|
|
|
96 |
$client = \core\di::get(http_client::class);
|
|
|
97 |
try {
|
|
|
98 |
// Call the external AI service.
|
|
|
99 |
$response = $client->send($request, [
|
|
|
100 |
'base_uri' => $this->get_endpoint(),
|
|
|
101 |
RequestOptions::HTTP_ERRORS => false,
|
|
|
102 |
]);
|
|
|
103 |
} catch (RequestException $e) {
|
|
|
104 |
// Handle any exceptions.
|
|
|
105 |
return [
|
|
|
106 |
'success' => false,
|
|
|
107 |
'errorcode' => $e->getCode(),
|
|
|
108 |
'errormessage' => $e->getMessage(),
|
|
|
109 |
];
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Double-check the response codes, in case of a non 200 that didn't throw an error.
|
|
|
113 |
$status = $response->getStatusCode();
|
|
|
114 |
if ($status === 200) {
|
|
|
115 |
return $this->handle_api_success($response);
|
|
|
116 |
} else {
|
|
|
117 |
return $this->handle_api_error($response);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Handle an error from the external AI api.
|
|
|
123 |
*
|
|
|
124 |
* @param ResponseInterface $response The response object.
|
|
|
125 |
* @return array The error response.
|
|
|
126 |
*/
|
|
|
127 |
protected function handle_api_error(ResponseInterface $response): array {
|
|
|
128 |
$responsearr = [
|
|
|
129 |
'success' => false,
|
|
|
130 |
'errorcode' => $response->getStatusCode(),
|
|
|
131 |
];
|
|
|
132 |
|
|
|
133 |
$status = $response->getStatusCode();
|
|
|
134 |
if ($status >= 500 && $status < 600) {
|
|
|
135 |
$responsearr['errormessage'] = $response->getReasonPhrase();
|
|
|
136 |
} else {
|
|
|
137 |
$bodyobj = json_decode($response->getBody()->getContents());
|
|
|
138 |
$responsearr['errormessage'] = $bodyobj->error->message;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
return $responsearr;
|
|
|
142 |
}
|
|
|
143 |
}
|