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