1074b7701SAndreas Gohr<?php 2074b7701SAndreas Gohr 3074b7701SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Ollama; 4074b7701SAndreas Gohr 5074b7701SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel; 6074b7701SAndreas Gohr 7074b7701SAndreas Gohr/** 8*4dd0657eSAndreas Gohr * Abstract Ollama Model 9074b7701SAndreas Gohr * 10*4dd0657eSAndreas Gohr * This class provides a basic interface to the Ollama API 11074b7701SAndreas Gohr */ 12074b7701SAndreas Gohrabstract class AbstractOllama extends AbstractModel 13074b7701SAndreas Gohr{ 14074b7701SAndreas Gohr protected $apiurl = 'http://localhost:11434/api/'; 15074b7701SAndreas Gohr 16074b7701SAndreas Gohr /** @inheritdoc */ 17074b7701SAndreas Gohr public function __construct(string $name, array $config) 18074b7701SAndreas Gohr { 19074b7701SAndreas Gohr parent::__construct($name, $config); 20074b7701SAndreas Gohr $this->apiurl = rtrim($config['ollama_baseurl'] ?? '', '/'); 21074b7701SAndreas Gohr if ($this->apiurl === '') { 22074b7701SAndreas Gohr throw new \Exception('Ollama base URL not configured'); 23074b7701SAndreas Gohr } 24074b7701SAndreas Gohr } 25074b7701SAndreas Gohr 26*4dd0657eSAndreas Gohr /** @inheritdoc */ 27*4dd0657eSAndreas Gohr function loadUnknownModelInfo(): array 28*4dd0657eSAndreas Gohr { 29*4dd0657eSAndreas Gohr $info = parent::loadUnknownModelInfo(); 30*4dd0657eSAndreas Gohr 31*4dd0657eSAndreas Gohr $url = $this->apiurl . 'show'; 32*4dd0657eSAndreas Gohr 33*4dd0657eSAndreas Gohr $result = $this->sendAPIRequest('POST', $url, ['model' => $this->modelName]); 34*4dd0657eSAndreas Gohr foreach($result['model_info'] as $key => $value) { 35*4dd0657eSAndreas Gohr if(str_ends_with($key, '.context_length')) { 36*4dd0657eSAndreas Gohr $info['inputTokens'] = $value; 37*4dd0657eSAndreas Gohr } 38*4dd0657eSAndreas Gohr if(str_ends_with($key, '.embedding_length')) { 39*4dd0657eSAndreas Gohr $info['dimensions'] = $value; 40*4dd0657eSAndreas Gohr } 41*4dd0657eSAndreas Gohr 42*4dd0657eSAndreas Gohr } 43*4dd0657eSAndreas Gohr 44*4dd0657eSAndreas Gohr return $info; 45*4dd0657eSAndreas Gohr } 46*4dd0657eSAndreas Gohr 47074b7701SAndreas Gohr /** 48*4dd0657eSAndreas Gohr * Send a request to the Ollama API 49074b7701SAndreas Gohr * 50074b7701SAndreas Gohr * @param string $endpoint 51074b7701SAndreas Gohr * @param array $data Payload to send 52074b7701SAndreas Gohr * @return array API response 53074b7701SAndreas Gohr * @throws \Exception 54074b7701SAndreas Gohr */ 55074b7701SAndreas Gohr protected function request($endpoint, $data) 56074b7701SAndreas Gohr { 57074b7701SAndreas Gohr $url = $this->apiurl . '/' . ltrim($endpoint, '/'); 58074b7701SAndreas Gohr return $this->sendAPIRequest('POST', $url, $data); 59074b7701SAndreas Gohr } 60074b7701SAndreas Gohr 61074b7701SAndreas Gohr /** @inheritdoc */ 62074b7701SAndreas Gohr protected function parseAPIResponse($response) 63074b7701SAndreas Gohr { 64074b7701SAndreas Gohr if (isset($response['eval_count'])) { 65074b7701SAndreas Gohr $this->inputTokensUsed += $response['eval_count']; 66074b7701SAndreas Gohr } 67074b7701SAndreas Gohr 68074b7701SAndreas Gohr if (isset($response['error'])) { 69074b7701SAndreas Gohr $error = is_array($response['error']) ? $response['error']['message'] : $response['error']; 70074b7701SAndreas Gohr throw new \Exception('Ollama API error: ' . $error); 71074b7701SAndreas Gohr } 72074b7701SAndreas Gohr 73074b7701SAndreas Gohr return $response; 74074b7701SAndreas Gohr } 75074b7701SAndreas Gohr} 76