1773012cfSAndreas Gohr<?php 2773012cfSAndreas Gohr 3773012cfSAndreas Gohrnamespace dokuwiki\plugin\aichat\test; 4773012cfSAndreas Gohr 5332eea25SAndreas Gohruse dokuwiki\plugin\aichat\Model\ChatInterface; 6332eea25SAndreas Gohruse dokuwiki\plugin\aichat\Model\EmbeddingInterface; 7773012cfSAndreas Gohruse DokuWikiTest; 8773012cfSAndreas Gohr 9773012cfSAndreas Gohr/** 10773012cfSAndreas Gohr * Base for Model provider tests 11773012cfSAndreas Gohr * 12773012cfSAndreas Gohr * @group plugin_aichat 13773012cfSAndreas Gohr * @group plugins 14773012cfSAndreas Gohr * @group internet 15773012cfSAndreas Gohr */ 16773012cfSAndreas Gohrabstract class AbstractModelTest extends DokuWikiTest 17773012cfSAndreas Gohr{ 18773012cfSAndreas Gohr /** @inheritdoc */ 19773012cfSAndreas Gohr protected $pluginsEnabled = ['aichat']; 20773012cfSAndreas Gohr 21773012cfSAndreas Gohr /** @var string The provider name, e.g. 'openai', 'reka', etc. */ 22773012cfSAndreas Gohr protected string $provider; 23773012cfSAndreas Gohr 24773012cfSAndreas Gohr /** @var string The environment variable name for the API key */ 25773012cfSAndreas Gohr protected string $api_key_env; 26773012cfSAndreas Gohr 27773012cfSAndreas Gohr /** @var string The chat model name, e.g. 'gpt-3.5-turbo', 'gpt-4', etc. */ 28773012cfSAndreas Gohr protected string $chat_model; 29773012cfSAndreas Gohr 30773012cfSAndreas Gohr /** @var string The embedding model name, e.g. 'text-embedding-3-small', etc. */ 31773012cfSAndreas Gohr protected string $embedding_model; 32773012cfSAndreas Gohr 33773012cfSAndreas Gohr /** @inheritdoc */ 34773012cfSAndreas Gohr public function setUp(): void 35773012cfSAndreas Gohr { 36773012cfSAndreas Gohr parent::setUp(); 37773012cfSAndreas Gohr global $conf; 38773012cfSAndreas Gohr 39773012cfSAndreas Gohr $apikey = getenv($this->api_key_env); 40773012cfSAndreas Gohr if (!$apikey) { 41773012cfSAndreas Gohr $this->markTestSkipped('API key environment not set'); 42773012cfSAndreas Gohr } 43773012cfSAndreas Gohr 44773012cfSAndreas Gohr $providerName = ucfirst($this->provider); 45773012cfSAndreas Gohr $providerConf = strtolower($this->provider); 46773012cfSAndreas Gohr 47773012cfSAndreas Gohr $conf['plugin']['aichat']['chatmodel'] = $providerName . ' ' . $this->chat_model; 48773012cfSAndreas Gohr $conf['plugin']['aichat']['rephrasemodel'] = $providerName . ' ' . $this->chat_model; 49332eea25SAndreas Gohr $conf['plugin']['aichat']['embedmodel'] = $providerName . ' ' . $this->embedding_model; 50773012cfSAndreas Gohr $conf['plugin']['aichat'][$providerConf . '_apikey'] = $apikey; 51773012cfSAndreas Gohr } 52773012cfSAndreas Gohr 53773012cfSAndreas Gohr public function testChat() 54773012cfSAndreas Gohr { 55773012cfSAndreas Gohr $prompt = 'This is a test. Please reply with "Hello World"'; 56773012cfSAndreas Gohr 57773012cfSAndreas Gohr /** @var \helper_plugin_aichat $helper */ 58773012cfSAndreas Gohr $helper = plugin_load('helper', 'aichat'); 59773012cfSAndreas Gohr $model = $helper->getChatModel(); 60332eea25SAndreas Gohr 61332eea25SAndreas Gohr $this->assertInstanceOf(ChatInterface::class, $model, 'Model should implement ChatInterface'); 62332eea25SAndreas Gohr $this->assertEquals( 63332eea25SAndreas Gohr 'dokuwiki\\plugin\\aichat\\Model\\' . $this->provider . '\\ChatModel', 64332eea25SAndreas Gohr get_class($model), 65332eea25SAndreas Gohr 'Model seems to be the wrong class' 66332eea25SAndreas Gohr ); 67332eea25SAndreas Gohr 68*75a24d82SAndreas Gohr try { 69773012cfSAndreas Gohr $reply = $model->getAnswer([ 70773012cfSAndreas Gohr ['role' => 'user', 'content' => $prompt] 71773012cfSAndreas Gohr ]); 72*75a24d82SAndreas Gohr } catch (\Exception $e) { 73*75a24d82SAndreas Gohr if (preg_match('/(credit|fund|balance)/i', $e->getMessage())) { 74*75a24d82SAndreas Gohr $this->markTestIncomplete($e->getMessage()); 75*75a24d82SAndreas Gohr } else { 76*75a24d82SAndreas Gohr throw $e; 77*75a24d82SAndreas Gohr } 78*75a24d82SAndreas Gohr } 79773012cfSAndreas Gohr 80773012cfSAndreas Gohr $this->assertStringContainsString('hello world', strtolower($reply)); 81773012cfSAndreas Gohr } 82773012cfSAndreas Gohr 83773012cfSAndreas Gohr public function testEmbedding() 84773012cfSAndreas Gohr { 85773012cfSAndreas Gohr $text = 'This is a test for embeddings.'; 86773012cfSAndreas Gohr 87773012cfSAndreas Gohr /** @var \helper_plugin_aichat $helper */ 88773012cfSAndreas Gohr $helper = plugin_load('helper', 'aichat'); 89773012cfSAndreas Gohr $model = $helper->getEmbeddingModel(); 90332eea25SAndreas Gohr 91332eea25SAndreas Gohr $this->assertInstanceOf(EmbeddingInterface::class, $model, 'Model should implement EmbeddingInterface'); 92332eea25SAndreas Gohr $this->assertEquals( 93332eea25SAndreas Gohr 'dokuwiki\\plugin\\aichat\\Model\\' . $this->provider . '\\EmbeddingModel', 94332eea25SAndreas Gohr get_class($model), 95332eea25SAndreas Gohr 'Model seems to be the wrong class' 96332eea25SAndreas Gohr ); 97332eea25SAndreas Gohr 98*75a24d82SAndreas Gohr try { 99773012cfSAndreas Gohr $embedding = $model->getEmbedding($text); 100*75a24d82SAndreas Gohr } catch (\Exception $e) { 101*75a24d82SAndreas Gohr if (preg_match('/(credit|fund|balance)/i', $e->getMessage())) { 102*75a24d82SAndreas Gohr $this->markTestIncomplete($e->getMessage()); 103*75a24d82SAndreas Gohr } else { 104*75a24d82SAndreas Gohr throw $e; 105*75a24d82SAndreas Gohr } 106*75a24d82SAndreas Gohr } 107*75a24d82SAndreas Gohr 108773012cfSAndreas Gohr $this->assertIsArray($embedding); 109773012cfSAndreas Gohr $this->assertNotEmpty($embedding, 'Embedding should not be empty'); 110773012cfSAndreas Gohr $this->assertIsFloat($embedding[0], 'Embedding should be an array of floats'); 111773012cfSAndreas Gohr } 112773012cfSAndreas Gohr} 113