xref: /plugin/aichat/Model/AbstractModel.php (revision 7ebc78955c65af90e7ee0afbd07adc15271113ba)
1<?php
2
3namespace dokuwiki\plugin\aichat\Model;
4
5abstract class AbstractModel
6{
7    /** @var int total tokens used by this instance */
8    protected $tokensUsed = 0;
9    /** @var int total cost used by this instance (multiplied by 1000*10000) */
10    protected $costEstimate = 0;
11    /** @var int total time spent in requests by this instance */
12    protected $timeUsed = 0;
13    /** @var int total number of requests made by this instance */
14    protected $requestsMade = 0;
15
16
17    /**
18     * @param array $authConfig Any configuration this Model/Service may need to authenticate
19     * @throws \Exception
20     */
21    abstract public function __construct($authConfig);
22
23    /**
24     * Maximum size of chunks this model can handle
25     *
26     * @return int
27     */
28    abstract public function getMaxEmbeddingTokenLength();
29
30    /**
31     * Maximum number of tokens to use when creating context info. Should be smaller than the absolute
32     * token limit of the model, so that prompts and questions can be added.
33     *
34     * @return int
35     */
36    abstract public function getMaxContextTokenLength();
37
38    /**
39     * Maximum number of tokens to use as context when rephrasing a question. Should be smaller than the
40     * absolute token limit of the model, so that prompts and questions can be added.
41     *
42     * @return int
43     */
44    public function getMaxRephrasingTokenLength()
45    {
46        return $this->getMaxContextTokenLength();
47    }
48
49    /**
50     * Get the embedding vectors for a given text
51     *
52     * @param string $text
53     * @return float[]
54     * @throws \Exception
55     */
56    abstract public function getEmbedding($text);
57
58    /**
59     * Answer a given question.
60     *
61     * Any prompt, chat history, context etc. will already be included in the $messages array.
62     *
63     * @param array $messages Messages in OpenAI format (with role and content)
64     * @return string The answer
65     * @throws \Exception
66     */
67    abstract public function getAnswer($messages);
68
69    /**
70     * This is called to let the LLM rephrase a question using given context
71     *
72     * Any prompt, chat history, context etc. will already be included in the $messages array.
73     * This calls getAnswer() by default, but you may want to use a different model instead.
74     *
75     * @param array $messages Messages in OpenAI format (with role and content)
76     * @return string The new question
77     * @throws \Exception
78     */
79    public function getRephrasedQuestion($messages)
80    {
81        return $this->getAnswer($messages);
82    }
83
84    /**
85     * Reset the usage statistics
86     *
87     * Usually not needed when only handling one operation per request, but useful in CLI
88     */
89    public function resetUsageStats()
90    {
91        $this->tokensUsed = 0;
92        $this->costEstimate = 0;
93        $this->timeUsed = 0;
94        $this->requestsMade = 0;
95    }
96
97    /**
98     * Get the usage statistics for this instance
99     *
100     * @return string[]
101     */
102    public function getUsageStats()
103    {
104        return [
105            'tokens' => $this->tokensUsed,
106            'cost' => round($this->costEstimate / 1000 / 10000, 4),
107            'time' => round($this->timeUsed, 2),
108            'requests' => $this->requestsMade,
109        ];
110    }
111}
112