Skip to content

Commit

Permalink
Only run unit tests if the config file contains the apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed Jun 18, 2024
1 parent e5eda69 commit 93ec792
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 4 additions & 7 deletions classes/ai/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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);

Expand Down
15 changes: 11 additions & 4 deletions tests/test_aiconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -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']);
}
}

0 comments on commit 93ec792

Please sign in to comment.