xref: /plugin/aichat/_test/AbstractModelTest.php (revision 773012cf12c00971b494249c070969945d137830)
1<?php
2
3namespace dokuwiki\plugin\aichat\test;
4
5use DokuWikiTest;
6
7/**
8 * Base for Model provider tests
9 *
10 * @group plugin_aichat
11 * @group plugins
12 * @group internet
13 */
14abstract class AbstractModelTest extends DokuWikiTest
15{
16    /** @inheritdoc */
17    protected $pluginsEnabled = ['aichat'];
18
19    /** @var string The provider name, e.g. 'openai', 'reka', etc. */
20    protected string $provider;
21
22    /** @var string The environment variable name for the API key */
23    protected string $api_key_env;
24
25    /** @var string The chat model name, e.g. 'gpt-3.5-turbo', 'gpt-4', etc. */
26    protected string $chat_model;
27
28    /** @var string The embedding model name, e.g. 'text-embedding-3-small', etc. */
29    protected string $embedding_model;
30
31    /** @inheritdoc */
32    public function setUp(): void
33    {
34        parent::setUp();
35        global $conf;
36
37        $apikey = getenv($this->api_key_env);
38        if (!$apikey) {
39            $this->markTestSkipped('API key environment not set');
40        }
41
42        $providerName = ucfirst($this->provider);
43        $providerConf = strtolower($this->provider);
44
45        $conf['plugin']['aichat']['chatmodel'] = $providerName . ' ' . $this->chat_model;
46        $conf['plugin']['aichat']['rephrasemodel'] = $providerName . ' ' . $this->chat_model;
47        $conf['plugin']['aichat']['embeddingmodel'] = $providerName . ' ' . $this->embedding_model;
48        $conf['plugin']['aichat'][$providerConf . '_apikey'] = $apikey;
49    }
50
51    public function testChat()
52    {
53        global $conf;
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        $reply = $model->getAnswer([
61            ['role' => 'user', 'content' => $prompt]
62        ]);
63
64        $this->assertStringContainsString('hello world', strtolower($reply));
65    }
66
67    public function testEmbedding()
68    {
69        $text = 'This is a test for embeddings.';
70
71        /** @var \helper_plugin_aichat $helper */
72        $helper = plugin_load('helper', 'aichat');
73        $model = $helper->getEmbeddingModel();
74        $embedding = $model->getEmbedding($text);
75        $this->assertIsArray($embedding);
76        $this->assertNotEmpty($embedding, 'Embedding should not be empty');
77        $this->assertIsFloat($embedding[0], 'Embedding should be an array of floats');
78    }
79}
80