Skip to content

Commit

Permalink
to squash
Browse files Browse the repository at this point in the history
  • Loading branch information
PM84 committed Sep 4, 2024
1 parent 81e3e3b commit a1637d8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
74 changes: 48 additions & 26 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public function perform_request(string $prompttext, array $options = []): prompt
$options = $this->sanitize_options($options);
} catch (\Exception $exception) {
return prompt_response::create_from_error(
400,
get_string('error_http400', 'local_ai_manager'),
$exception->getMessage()
400,
get_string('error_http400', 'local_ai_manager'),
$exception->getMessage()
);
}

Expand All @@ -140,14 +140,16 @@ public function perform_request(string $prompttext, array $options = []): prompt
if ($userusage->get_currentusage() >= $this->configmanager->get_max_requests($this->purpose, $userinfo->get_role())) {
$period = format_time($this->configmanager->get_max_requests_period());
return prompt_response::create_from_error(
429,
get_string(
'error_http429',
'local_ai_manager',
['count' => $this->configmanager->get_max_requests($this->purpose, $userinfo->get_role()),
'period' => $period]
),
''
429,
get_string(
'error_http429',
'local_ai_manager',
[
'count' => $this->configmanager->get_max_requests($this->purpose, $userinfo->get_role()),
'period' => $period
]
),
''
);
}

Expand All @@ -168,21 +170,35 @@ public function perform_request(string $prompttext, array $options = []): prompt
$endtime = microtime(true);
$duration = round($endtime - $starttime, 2);
if ($requestresult->get_code() !== 200) {
$promptresponse = prompt_response::create_from_error($requestresult->get_code(), $requestresult->get_errormessage(),
$requestresult->get_debuginfo());
$promptresponse = prompt_response::create_from_error(
$requestresult->get_code(),
$requestresult->get_errormessage(),
$requestresult->get_debuginfo()
);
get_ai_response_failed::create_from_prompt_response($promptdata, $promptresponse, $duration)->trigger();
return $promptresponse;
}
$promptcompletion = $this->connector->execute_prompt_completion($requestresult->get_response(), $options);
if (!empty($options['forcenewitemid']) && !empty($options['component']) &&
!empty($options['contextid'] && !empty($options['itemid']))) {
if ($DB->record_exists('local_ai_manager_request_log',
['component' => $options['component'], 'contextid' => $options['contextid'], 'itemid' => $options['itemid']])) {
if (
!empty($options['forcenewitemid']) && !empty($options['component']) &&
!empty($options['contextid'] && !empty($options['itemid']))
) {
if ($DB->record_exists(
'local_ai_manager_request_log',
[
'component' => $options['component'],
'contextid' => $options['contextid'],
'itemid' => $options['itemid']
]
)) {
$existingitemid = $options['itemid'];
unset($options['itemid']);
$this->log_request($prompttext, $promptcompletion, $duration, $requestoptions, $options);
$promptresponse = prompt_response::create_from_error(409, get_string('error_http409', 'local_ai_manager',
$existingitemid), '');
$promptresponse = prompt_response::create_from_error(409, get_string(
'error_http409',
'local_ai_manager',
$existingitemid
), '');
get_ai_response_failed::create_from_prompt_response($promptdata, $promptresponse, $duration)->trigger();
return $promptresponse;
}
Expand All @@ -205,9 +221,13 @@ public function perform_request(string $prompttext, array $options = []): prompt
* @param array $options part of $requestoptions, contains the options directly passed to the manager
* @return int the record id of the log record which has been stored to the database
*/
public function log_request(string $prompttext, prompt_response $promptcompletion, float $executiontime,
array $requestoptions = [],
array $options = []): int {
public function log_request(
string $prompttext,
prompt_response $promptcompletion,
float $executiontime,
array $requestoptions = [],
array $options = []
): int {
global $DB, $USER;

// phpcs:disable moodle.Commenting.TodoComment.MissingInfoInline
Expand Down Expand Up @@ -267,13 +287,15 @@ private function sanitize_options(array $options): array {
foreach ($options as $key => $value) {
if (!array_key_exists($key, $this->purpose->get_available_purpose_options())) {
throw new \coding_exception('Option ' . $key . ' is not allowed for the purpose ' .
$this->purpose->get_plugin_name());
$this->purpose->get_plugin_name());
}
if (is_array($this->purpose->get_available_purpose_options()[$key])) {
if (!in_array($value[0], array_map(fn($valueobject) => $valueobject['key'],
$this->purpose->get_available_purpose_options()[$key]))) {
if (!in_array($value[0], array_map(
fn($valueobject) => $valueobject['key'],
$this->purpose->get_available_purpose_options()[$key]
))) {
throw new \coding_exception('Value ' . $value[0] . ' for option ' . $key . ' is not allowed for the purpose ' .
$this->purpose->get_plugin_name());
$this->purpose->get_plugin_name());
}
} else {
if ($this->purpose->get_available_purpose_options()[$key] === base_purpose::PARAM_ARRAY) {
Expand Down
4 changes: 2 additions & 2 deletions purposes/genai/classes/purpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function get_request_options(array $options): array {

if (array_key_exists('messages', $options)) {
$messages = [];
foreach($options['messages'] as $message) {
switch($message['role']) {
foreach ($options['messages'] as $message) {
switch ($message['role']) {
case 'user':
$messages[] = ['sender' => 'user', 'message' => $message['content']];
break;
Expand Down

0 comments on commit a1637d8

Please sign in to comment.