xref: /plugin/aichat/_test/AbstractModelTest.php (revision 75a24d825ad1d95643e5d94c4600879a28908e17)
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        try {
69            $reply = $model->getAnswer([
70                ['role' => 'user', 'content' => $prompt]
71            ]);
72        } catch (\Exception $e) {
73            if (preg_match('/(credit|fund|balance)/i', $e->getMessage())) {
74                $this->markTestIncomplete($e->getMessage());
75            } else {
76                throw $e;
77            }
78        }
79
80        $this->assertStringContainsString('hello world', strtolower($reply));
81    }
82
83    public function testEmbedding()
84    {
85        $text = 'This is a test for embeddings.';
86
87        /** @var \helper_plugin_aichat $helper */
88        $helper = plugin_load('helper', 'aichat');
89        $model = $helper->getEmbeddingModel();
90
91        $this->assertInstanceOf(EmbeddingInterface::class, $model, 'Model should implement EmbeddingInterface');
92        $this->assertEquals(
93            'dokuwiki\\plugin\\aichat\\Model\\' . $this->provider . '\\EmbeddingModel',
94            get_class($model),
95            'Model seems to be the wrong class'
96        );
97
98        try {
99            $embedding = $model->getEmbedding($text);
100        } catch (\Exception $e) {
101            if (preg_match('/(credit|fund|balance)/i', $e->getMessage())) {
102                $this->markTestIncomplete($e->getMessage());
103            } else {
104                throw $e;
105            }
106        }
107
108        $this->assertIsArray($embedding);
109        $this->assertNotEmpty($embedding, 'Embedding should not be empty');
110        $this->assertIsFloat($embedding[0], 'Embedding should be an array of floats');
111    }
112}
113