1<?php 2 3namespace dokuwiki\plugin\aichat\Model\Ollama; 4 5use dokuwiki\plugin\aichat\Model\Generic\AbstractGenericModel; 6 7/** 8 * Abstract Ollama Model 9 * 10 * This class provides a basic interface to the Ollama API 11 */ 12abstract class AbstractOllama extends AbstractGenericModel 13{ 14 15 /** @inheritdoc */ 16 protected function getHttpClient() 17 { 18 $http = parent::getHttpClient(); 19 20 $apiKey = $this->getFromConf('apikey'); 21 $http->headers['Authorization'] = 'Bearer ' . $apiKey; 22 return $http; 23 } 24 25 /** @inheritdoc */ 26 function loadUnknownModelInfo(): array 27 { 28 $info = parent::loadUnknownModelInfo(); 29 30 $url = $this->apiurl . '/show'; 31 32 $result = $this->sendAPIRequest('POST', $url, ['model' => $this->modelName]); 33 foreach($result['model_info'] as $key => $value) { 34 if(str_ends_with($key, '.context_length')) { 35 $info['inputTokens'] = $value; 36 } 37 if(str_ends_with($key, '.embedding_length')) { 38 $info['dimensions'] = $value; 39 } 40 41 } 42 43 return $info; 44 } 45 46 47 48 /** @inheritdoc */ 49 protected function parseAPIResponse($response) 50 { 51 if (isset($response['eval_count'])) { 52 $this->inputTokensUsed += $response['eval_count']; 53 } 54 55 if (isset($response['error'])) { 56 $error = is_array($response['error']) ? $response['error']['message'] : $response['error']; 57 throw new \Exception('Ollama API error: ' . $error, 3002); 58 } 59 60 return $response; 61 } 62} 63