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