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