xref: /plugin/aichat/Model/AbstractModel.php (revision dce0dee5ef27bcbbc5570fc278f3e75f426c19c5)
1<?php
2
3namespace dokuwiki\plugin\aichat\Model;
4
5use dokuwiki\HTTP\DokuHTTPClient;
6
7/**
8 * Base class for all models
9 *
10 * Model classes also need to implement one of the following interfaces:
11 * - ChatInterface
12 * - EmbeddingInterface
13 *
14 * This class already implements most of the requirements for these interfaces.
15 *
16 * In addition to any missing interface methods, model implementations will need to
17 * extend the constructor to handle the plugin configuration and implement the
18 * parseAPIResponse() method to handle the specific API response.
19 */
20abstract class AbstractModel implements ModelInterface
21{
22    /** @var string The model name */
23    protected $modelName;
24    /** @var array The model info from the model.json file */
25    protected $modelInfo;
26
27    /** @var int input tokens used since last reset */
28    protected $inputTokensUsed = 0;
29    /** @var int output tokens used since last reset */
30    protected $outputTokensUsed = 0;
31    /** @var int total time spent in requests since last reset */
32    protected $timeUsed = 0;
33    /** @var int total number of requests made since last reset */
34    protected $requestsMade = 0;
35    /** @var int start time of the current request chain (may be multiple when retries needed) */
36    protected $requestStart = 0;
37
38    /** @var int How often to retry a request if it fails */
39    public const MAX_RETRIES = 3;
40
41    /** @var DokuHTTPClient */
42    protected $http;
43    /** @var bool debug API communication */
44    protected $debug = false;
45
46    // region ModelInterface
47
48    /** @inheritdoc */
49    public function __construct(string $name, array $config)
50    {
51        $this->modelName = $name;
52        $this->http = new DokuHTTPClient();
53        $this->http->timeout = 60;
54        $this->http->headers['Content-Type'] = 'application/json';
55
56        $reflect = new \ReflectionClass($this);
57        $json = dirname($reflect->getFileName()) . '/models.json';
58        if (!file_exists($json)) {
59            throw new \Exception('Model info file not found at ' . $json);
60        }
61        try {
62            $modelinfos = json_decode(file_get_contents($json), true, 512, JSON_THROW_ON_ERROR);
63        } catch (\JsonException $e) {
64            throw new \Exception('Failed to parse model info file: ' . $e->getMessage(), $e->getCode(), $e);
65        }
66
67        if ($this instanceof ChatInterface) {
68            if (!isset($modelinfos['chat'][$name])) {
69                throw new \Exception('Invalid chat model configured: ' . $name);
70            }
71            $this->modelInfo = $modelinfos['chat'][$name];
72        }
73
74        if ($this instanceof EmbeddingInterface) {
75            if (!isset($modelinfos['embedding'][$name])) {
76                throw new \Exception('Invalid embedding model configured: ' . $name);
77            }
78            $this->modelInfo = $modelinfos['embedding'][$name];
79        }
80    }
81
82    /** @inheritdoc */
83    public function getModelName()
84    {
85        return $this->modelName;
86    }
87
88    /**
89     * Reset the usage statistics
90     *
91     * Usually not needed when only handling one operation per request, but useful in CLI
92     */
93    public function resetUsageStats()
94    {
95        $this->tokensUsed = 0;
96        $this->timeUsed = 0;
97        $this->requestsMade = 0;
98    }
99
100    /**
101     * Get the usage statistics for this instance
102     *
103     * @return string[]
104     */
105    public function getUsageStats()
106    {
107
108        $cost = 0;
109        $cost += $this->inputTokensUsed * $this->getInputTokenPrice();
110        if ($this instanceof ChatInterface) {
111            $cost += $this->outputTokensUsed * $this->getOutputTokenPrice();
112        }
113
114        return [
115            'tokens' => $this->inputTokensUsed + $this->outputTokensUsed,
116            'cost' => round($cost / 1_000_000, 4),
117            'time' => round($this->timeUsed, 2),
118            'requests' => $this->requestsMade,
119        ];
120    }
121
122    /** @inheritdoc */
123    public function getMaxInputTokenLength(): int
124    {
125        return $this->modelInfo['inputTokens'];
126    }
127
128    /** @inheritdoc */
129    public function getInputTokenPrice(): float
130    {
131        return $this->modelInfo['inputTokenPrice'];
132    }
133
134    // endregion
135
136    // region EmbeddingInterface
137
138    /** @inheritdoc */
139    public function getDimensions(): int
140    {
141        return $this->modelInfo['dimensions'];
142    }
143
144    // endregion
145
146    // region ChatInterface
147
148    public function getMaxOutputTokenLength(): int
149    {
150        return $this->modelInfo['outputTokens'];
151    }
152
153    public function getOutputTokenPrice(): float
154    {
155        return $this->modelInfo['outputTokenPrice'];
156    }
157
158    // endregion
159
160    // region API communication
161
162    /**
163     * When enabled, the input/output of the API will be printed to STDOUT
164     *
165     * @param bool $debug
166     */
167    public function setDebug($debug = true)
168    {
169        $this->debug = $debug;
170    }
171
172    /**
173     * This method should check the response for any errors. If the API singalled an error,
174     * this method should throw an Exception with a meaningful error message.
175     *
176     * If the response returned any info on used tokens, they should be added to $this->tokensUsed
177     *
178     * The method should return the parsed response, which will be passed to the calling method.
179     *
180     * @param mixed $response the parsed JSON response from the API
181     * @return mixed
182     * @throws \Exception when the response indicates an error
183     */
184    abstract protected function parseAPIResponse($response);
185
186    /**
187     * Send a request to the API
188     *
189     * Model classes should use this method to send requests to the API.
190     *
191     * This method will take care of retrying and logging basic statistics.
192     *
193     * It is assumed that all APIs speak JSON.
194     *
195     * @param string $method The HTTP method to use (GET, POST, PUT, DELETE, etc.)
196     * @param string $url The full URL to send the request to
197     * @param array $data Payload to send, will be encoded to JSON
198     * @param int $retry How often this request has been retried, do not set externally
199     * @return array API response as returned by parseAPIResponse
200     * @throws \Exception when anything goes wrong
201     */
202    protected function sendAPIRequest($method, $url, $data, $retry = 0)
203    {
204        // init statistics
205        if ($retry === 0) {
206            $this->requestStart = microtime(true);
207        } else {
208            sleep($retry); // wait a bit between retries
209        }
210        $this->requestsMade++;
211
212        // encode payload data
213        try {
214            $json = json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
215        } catch (\JsonException $e) {
216            $this->timeUsed += $this->requestStart - microtime(true);
217            throw new \Exception('Failed to encode JSON for API:' . $e->getMessage(), $e->getCode(), $e);
218        }
219
220        if ($this->debug) {
221            echo 'Sending ' . $method . ' request to ' . $url . ' with payload:' . "\n";
222            print_r($json);
223        }
224
225        // send request and handle retries
226        $this->http->sendRequest($url, $json, $method);
227        $response = $this->http->resp_body;
228        if ($response === false || $this->http->error) {
229            if ($retry < self::MAX_RETRIES) {
230                return $this->sendAPIRequest($method, $url, $data, $retry + 1);
231            }
232            $this->timeUsed += microtime(true) - $this->requestStart;
233            throw new \Exception('API returned no response. ' . $this->http->error);
234        }
235
236        if ($this->debug) {
237            echo 'Received response:' . "\n";
238            print_r($response);
239        }
240
241        // decode the response
242        try {
243            $result = json_decode((string)$response, true, 512, JSON_THROW_ON_ERROR);
244        } catch (\JsonException $e) {
245            $this->timeUsed += microtime(true) - $this->requestStart;
246            throw new \Exception('API returned invalid JSON: ' . $response, 0, $e);
247        }
248
249        // parse the response, retry on error
250        try {
251            $result = $this->parseAPIResponse($result);
252        } catch (\Exception $e) {
253            if ($retry < self::MAX_RETRIES) {
254                return $this->sendAPIRequest($method, $url, $data, $retry + 1);
255            }
256            $this->timeUsed += microtime(true) - $this->requestStart;
257            throw $e;
258        }
259
260        $this->timeUsed += microtime(true) - $this->requestStart;
261        return $result;
262    }
263
264    // endregion
265}
266