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