1<?php 2 3namespace dokuwiki\plugin\aichat\Model\Ollama; 4 5use dokuwiki\plugin\aichat\Model\AbstractModel; 6 7/** 8 * Abstract OpenAI Model 9 * 10 * This class provides a basic interface to the OpenAI API 11 */ 12abstract class AbstractOllama extends AbstractModel 13{ 14 protected $apiurl = 'http://localhost:11434/api/'; 15 16 /** @inheritdoc */ 17 public function __construct(string $name, array $config) 18 { 19 parent::__construct($name, $config); 20 $this->apiurl = rtrim($config['ollama_baseurl'] ?? '', '/'); 21 if ($this->apiurl === '') { 22 throw new \Exception('Ollama base URL not configured'); 23 } 24 } 25 26 /** 27 * Send a request to the OpenAI API 28 * 29 * @param string $endpoint 30 * @param array $data Payload to send 31 * @return array API response 32 * @throws \Exception 33 */ 34 protected function request($endpoint, $data) 35 { 36 $url = $this->apiurl . '/' . ltrim($endpoint, '/'); 37 return $this->sendAPIRequest('POST', $url, $data); 38 } 39 40 /** @inheritdoc */ 41 protected function parseAPIResponse($response) 42 { 43 if (isset($response['eval_count'])) { 44 $this->inputTokensUsed += $response['eval_count']; 45 } 46 47 if (isset($response['error'])) { 48 $error = is_array($response['error']) ? $response['error']['message'] : $response['error']; 49 throw new \Exception('Ollama API error: ' . $error); 50 } 51 52 return $response; 53 } 54} 55