From 93ec792ea45187703ed51b92e29f4c188d0ea028 Mon Sep 17 00:00:00 2001 From: Marcus Green Date: Tue, 18 Jun 2024 22:10:17 +0100 Subject: [PATCH] Only run unit tests if the config file contains the apikey --- classes/ai/ai.php | 11 ++++------- tests/test_aiconnect.php | 15 +++++++++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/classes/ai/ai.php b/classes/ai/ai.php index 40a168c..5987d42 100644 --- a/classes/ai/ai.php +++ b/classes/ai/ai.php @@ -66,7 +66,7 @@ class ai { */ public function __construct(?string $model = null) { $this->model = $model ?? trim(explode(',', get_config('tool_aiconnect', 'model'))[0]); - $this->openaiapikey = get_config('tool_aiconnect', 'apikey'); + $this->apikey = get_config('tool_aiconnect', 'apikey'); $this->temperature = get_config('tool_aiconnect', 'temperature'); $this->endpoint = trim(get_config('tool_aiconnect', 'endpoint')); } @@ -133,12 +133,9 @@ private function make_request(array $data, string $apikey, $multipart = null): a * @throws moodle_exception If the model is empty. */ public function prompt_completion($prompttext) { - if (PHPUNIT_TEST) { - return []; - } - if (empty($this->model)) { - throw new moodle_exception('misssingmodelerror', 'tool_aiconnect', '', null, 'Empty query model.'); - } + // if (empty($this->model)) { + // throw new moodle_exception('misssingmodelerror', 'tool_aiconnect', '', null, 'Empty query model.'); + // } $data = $this->get_prompt_data($prompttext); $result = $this->make_request($data, $this->apikey); diff --git a/tests/test_aiconnect.php b/tests/test_aiconnect.php index 3208738..19f122e 100644 --- a/tests/test_aiconnect.php +++ b/tests/test_aiconnect.php @@ -39,10 +39,11 @@ class test_aiconnect extends \advanced_testcase { /** * The class with most of the functionality - * @var ai + * @var $ai */ public $ai; + /** * Initialise everything * @@ -51,8 +52,8 @@ class test_aiconnect extends \advanced_testcase { public function setUp(): void { if (defined('TEST_LLM_APIKEY')) { set_config('apikey', TEST_LLM_APIKEY, 'tool_aiconnect'); + $this->ai = new ai\ai(); } - $this->ai = new ai\ai(); } /** * Work around the get_prompt_data method @@ -77,11 +78,17 @@ public function test_get_prompt_data(): void { } /** - * This doesn't do anything especially useful. + * Ask the LLM to do some maths * @return void */ public function test_prompt_completion(): void { - $result = $this->ai->prompt_completion('query'); + $this->resetAfterTest(); + if (!$this->ai) { + $this->markTestSkipped(); + } + $query = "What is 2 * 4?"; + $result = $this->ai->prompt_completion($query); $this->assertIsArray($result); + $this->assertStringContainsString("8", $result['response']['choices'][0]['message']['content']); } }