1773012cfSAndreas Gohr<?php 2773012cfSAndreas Gohr 3773012cfSAndreas Gohrnamespace dokuwiki\plugin\aichat\test; 4773012cfSAndreas Gohr 5*332eea25SAndreas Gohruse dokuwiki\plugin\aichat\Model\ChatInterface; 6*332eea25SAndreas 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; 49*332eea25SAndreas 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(); 60*332eea25SAndreas Gohr 61*332eea25SAndreas Gohr $this->assertInstanceOf(ChatInterface::class, $model, 'Model should implement ChatInterface'); 62*332eea25SAndreas Gohr $this->assertEquals( 63*332eea25SAndreas Gohr 'dokuwiki\\plugin\\aichat\\Model\\' . $this->provider . '\\ChatModel', 64*332eea25SAndreas Gohr get_class($model), 65*332eea25SAndreas Gohr 'Model seems to be the wrong class' 66*332eea25SAndreas Gohr ); 67*332eea25SAndreas Gohr 68773012cfSAndreas Gohr $reply = $model->getAnswer([ 69773012cfSAndreas Gohr ['role' => 'user', 'content' => $prompt] 70773012cfSAndreas Gohr ]); 71773012cfSAndreas Gohr 72773012cfSAndreas Gohr $this->assertStringContainsString('hello world', strtolower($reply)); 73773012cfSAndreas Gohr } 74773012cfSAndreas Gohr 75773012cfSAndreas Gohr public function testEmbedding() 76773012cfSAndreas Gohr { 77773012cfSAndreas Gohr $text = 'This is a test for embeddings.'; 78773012cfSAndreas Gohr 79773012cfSAndreas Gohr /** @var \helper_plugin_aichat $helper */ 80773012cfSAndreas Gohr $helper = plugin_load('helper', 'aichat'); 81773012cfSAndreas Gohr $model = $helper->getEmbeddingModel(); 82*332eea25SAndreas Gohr 83*332eea25SAndreas Gohr $this->assertInstanceOf(EmbeddingInterface::class, $model, 'Model should implement EmbeddingInterface'); 84*332eea25SAndreas Gohr $this->assertEquals( 85*332eea25SAndreas Gohr 'dokuwiki\\plugin\\aichat\\Model\\' . $this->provider . '\\EmbeddingModel', 86*332eea25SAndreas Gohr get_class($model), 87*332eea25SAndreas Gohr 'Model seems to be the wrong class' 88*332eea25SAndreas Gohr ); 89*332eea25SAndreas Gohr 90773012cfSAndreas Gohr $embedding = $model->getEmbedding($text); 91773012cfSAndreas Gohr $this->assertIsArray($embedding); 92773012cfSAndreas Gohr $this->assertNotEmpty($embedding, 'Embedding should not be empty'); 93773012cfSAndreas Gohr $this->assertIsFloat($embedding[0], 'Embedding should be an array of floats'); 94773012cfSAndreas Gohr } 95773012cfSAndreas Gohr} 96