From e4b39e764d0dcafdf7271c63802ee7dc4ab9942a Mon Sep 17 00:00:00 2001 From: David Woloszyn Date: Thu, 26 Sep 2024 15:13:05 +1000 Subject: [PATCH] MDL-83154 hub: Add model to AI usage data --- lib/classes/hub/registration.php | 16 +++++++++++++++- lib/tests/hub/registration_test.php | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/classes/hub/registration.php b/lib/classes/hub/registration.php index 88dd42eb20b..42f891b0288 100644 --- a/lib/classes/hub/registration.php +++ b/lib/classes/hub/registration.php @@ -756,6 +756,7 @@ class registration { 'fail_count' => 0, 'times' => [], 'errors' => [], + 'modelstemp' => [], ]; } @@ -769,9 +770,13 @@ class registration { // Collect errors for determing the predominant one. $data[$provider][$actionname]['errors'][] = $action->errorcode; } + + // Collect models used and identify unknown ones. + $model = $action->model ?? 'unknown'; + $data[$provider][$actionname]['modelstemp'][] = $model; } - // Parse the errors and everage the times, then add them to the data. + // Parse the errors, average the times, count the models and then add them to the data. foreach ($data as $p => $provider) { foreach ($provider as $a => $actionname) { if (isset($data[$p][$a]['errors'])) { @@ -796,6 +801,15 @@ class registration { } } unset($data[$p][$a]['times']); + + if (isset($data[$p][$a]['modelstemp'])) { + // Create an array with the models counted. + $countedmodels = array_count_values($data[$p][$a]['modelstemp']); + foreach ($countedmodels as $model => $count) { + $data[$p][$a]['models'][$model]['count'] = $count; + } + } + unset($data[$p][$a]['modelstemp']); } } diff --git a/lib/tests/hub/registration_test.php b/lib/tests/hub/registration_test.php index e0dbb519532..69c5f440c9b 100644 --- a/lib/tests/hub/registration_test.php +++ b/lib/tests/hub/registration_test.php @@ -94,6 +94,9 @@ class registration_test extends \advanced_testcase { $this->resetAfterTest(); $clock = $this->mock_clock_with_frozen(); + $gpt4omodel = 'gpt-4o'; + $dalle3model = 'dall-e-3'; + // Record some generated text. $record = new \stdClass(); $record->provider = 'openai'; @@ -104,15 +107,17 @@ class registration_test extends \advanced_testcase { $record->success = true; $record->timecreated = $clock->time() - 5; $record->timecompleted = $clock->time(); + $record->model = $gpt4omodel; $DB->insert_record('ai_action_register', $record); // Record a generated image. $record->actionname = 'generate_image'; - $record->actionid = 2; + $record->actionid = 111; $record->timecreated = $clock->time() - 20; + $record->model = $dalle3model; $DB->insert_record('ai_action_register', $record); // Record another image. - $record->actionid = 3; + $record->actionid = 222; $record->timecreated = $clock->time() - 10; $DB->insert_record('ai_action_register', $record); @@ -121,6 +126,7 @@ class registration_test extends \advanced_testcase { $record->actionid = 4; $record->success = false; $record->errorcode = 403; + $record->model = null; $DB->insert_record('ai_action_register', $record); $record->actionid = 5; $record->errorcode = 403; @@ -143,5 +149,9 @@ class registration_test extends \advanced_testcase { // Check time range is set correctly. $this->assertEquals($clock->time() - WEEKSECS, $aisuage->time_range->timefrom); $this->assertEquals($clock->time(), $aisuage->time_range->timeto); + // Check model counts. + $this->assertEquals(1, $aisuage->openai->generate_text->models->{$gpt4omodel}->count); + $this->assertEquals(2, $aisuage->openai->generate_image->models->{$dalle3model}->count); + $this->assertEquals(3, $aisuage->openai->generate_image->models->unknown->count); } }