1074b7701SAndreas Gohr<?php 2074b7701SAndreas Gohr 3074b7701SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Ollama; 4074b7701SAndreas Gohr 52e22aefbSAndreas Gohruse dokuwiki\plugin\aichat\Model\Generic\AbstractGenericModel; 6074b7701SAndreas Gohr 7074b7701SAndreas Gohr/** 84dd0657eSAndreas Gohr * Abstract Ollama Model 9074b7701SAndreas Gohr * 104dd0657eSAndreas Gohr * This class provides a basic interface to the Ollama API 11074b7701SAndreas Gohr */ 122e22aefbSAndreas Gohrabstract class AbstractOllama extends AbstractGenericModel 13074b7701SAndreas Gohr{ 14074b7701SAndreas Gohr 154dd0657eSAndreas Gohr /** @inheritdoc */ 16*7bd2bac6SAndreas Gohr protected function getHttpClient() 17*7bd2bac6SAndreas Gohr { 18*7bd2bac6SAndreas Gohr $http = parent::getHttpClient(); 19*7bd2bac6SAndreas Gohr 20*7bd2bac6SAndreas Gohr $apiKey = $this->getFromConf('apikey'); 21*7bd2bac6SAndreas Gohr $http->headers['Authorization'] = 'Bearer ' . $apiKey; 22*7bd2bac6SAndreas Gohr return $http; 23*7bd2bac6SAndreas Gohr } 24*7bd2bac6SAndreas Gohr 25*7bd2bac6SAndreas Gohr /** @inheritdoc */ 264dd0657eSAndreas Gohr function loadUnknownModelInfo(): array 274dd0657eSAndreas Gohr { 284dd0657eSAndreas Gohr $info = parent::loadUnknownModelInfo(); 294dd0657eSAndreas Gohr 302e22aefbSAndreas Gohr $url = $this->apiurl . '/show'; 314dd0657eSAndreas Gohr 324dd0657eSAndreas Gohr $result = $this->sendAPIRequest('POST', $url, ['model' => $this->modelName]); 334dd0657eSAndreas Gohr foreach($result['model_info'] as $key => $value) { 344dd0657eSAndreas Gohr if(str_ends_with($key, '.context_length')) { 354dd0657eSAndreas Gohr $info['inputTokens'] = $value; 364dd0657eSAndreas Gohr } 374dd0657eSAndreas Gohr if(str_ends_with($key, '.embedding_length')) { 384dd0657eSAndreas Gohr $info['dimensions'] = $value; 394dd0657eSAndreas Gohr } 404dd0657eSAndreas Gohr 414dd0657eSAndreas Gohr } 424dd0657eSAndreas Gohr 434dd0657eSAndreas Gohr return $info; 444dd0657eSAndreas Gohr } 454dd0657eSAndreas Gohr 462e22aefbSAndreas Gohr 47074b7701SAndreas Gohr 48074b7701SAndreas Gohr /** @inheritdoc */ 49074b7701SAndreas Gohr protected function parseAPIResponse($response) 50074b7701SAndreas Gohr { 51074b7701SAndreas Gohr if (isset($response['eval_count'])) { 52074b7701SAndreas Gohr $this->inputTokensUsed += $response['eval_count']; 53074b7701SAndreas Gohr } 54074b7701SAndreas Gohr 55074b7701SAndreas Gohr if (isset($response['error'])) { 56074b7701SAndreas Gohr $error = is_array($response['error']) ? $response['error']['message'] : $response['error']; 5742b2c6e8SAndreas Gohr throw new \Exception('Ollama API error: ' . $error, 3002); 58074b7701SAndreas Gohr } 59074b7701SAndreas Gohr 60074b7701SAndreas Gohr return $response; 61074b7701SAndreas Gohr } 62074b7701SAndreas Gohr} 63