1<?php 2 3namespace dokuwiki\plugin\aichat\test; 4 5use dokuwiki\plugin\aichat\Model\ChatInterface; 6use dokuwiki\plugin\aichat\Model\EmbeddingInterface; 7use DokuWikiTest; 8 9/** 10 * Base for Model provider tests 11 * 12 * @group plugin_aichat 13 * @group plugins 14 * @group internet 15 */ 16abstract class AbstractModelTest extends DokuWikiTest 17{ 18 /** @inheritdoc */ 19 protected $pluginsEnabled = ['aichat']; 20 21 /** @var string The provider name, e.g. 'openai', 'reka', etc. */ 22 protected string $provider; 23 24 /** @var string The environment variable name for the API key */ 25 protected string $api_key_env; 26 27 /** @var string The chat model name, e.g. 'gpt-3.5-turbo', 'gpt-4', etc. */ 28 protected string $chat_model; 29 30 /** @var string The embedding model name, e.g. 'text-embedding-3-small', etc. */ 31 protected string $embedding_model; 32 33 /** @inheritdoc */ 34 public function setUp(): void 35 { 36 parent::setUp(); 37 global $conf; 38 39 $apikey = getenv($this->api_key_env); 40 if (!$apikey) { 41 $this->markTestSkipped('API key environment not set'); 42 } 43 44 $providerName = ucfirst($this->provider); 45 $providerConf = strtolower($this->provider); 46 47 $conf['plugin']['aichat']['chatmodel'] = $providerName . ' ' . $this->chat_model; 48 $conf['plugin']['aichat']['rephrasemodel'] = $providerName . ' ' . $this->chat_model; 49 $conf['plugin']['aichat']['embedmodel'] = $providerName . ' ' . $this->embedding_model; 50 $conf['plugin']['aichat'][$providerConf . '_apikey'] = $apikey; 51 } 52 53 public function testChat() 54 { 55 $prompt = 'This is a test. Please reply with "Hello World"'; 56 57 /** @var \helper_plugin_aichat $helper */ 58 $helper = plugin_load('helper', 'aichat'); 59 $model = $helper->getChatModel(); 60 61 $this->assertInstanceOf(ChatInterface::class, $model, 'Model should implement ChatInterface'); 62 $this->assertEquals( 63 'dokuwiki\\plugin\\aichat\\Model\\' . $this->provider . '\\ChatModel', 64 get_class($model), 65 'Model seems to be the wrong class' 66 ); 67 68 $reply = $model->getAnswer([ 69 ['role' => 'user', 'content' => $prompt] 70 ]); 71 72 $this->assertStringContainsString('hello world', strtolower($reply)); 73 } 74 75 public function testEmbedding() 76 { 77 $text = 'This is a test for embeddings.'; 78 79 /** @var \helper_plugin_aichat $helper */ 80 $helper = plugin_load('helper', 'aichat'); 81 $model = $helper->getEmbeddingModel(); 82 83 $this->assertInstanceOf(EmbeddingInterface::class, $model, 'Model should implement EmbeddingInterface'); 84 $this->assertEquals( 85 'dokuwiki\\plugin\\aichat\\Model\\' . $this->provider . '\\EmbeddingModel', 86 get_class($model), 87 'Model seems to be the wrong class' 88 ); 89 90 $embedding = $model->getEmbedding($text); 91 $this->assertIsArray($embedding); 92 $this->assertNotEmpty($embedding, 'Embedding should not be empty'); 93 $this->assertIsFloat($embedding[0], 'Embedding should be an array of floats'); 94 } 95} 96