xref: /plugin/aichat/Model/Ollama/AbstractOllama.php (revision 42b2c6e864def16df42600c2885e21d4d148fd0c)
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/**
84dd0657eSAndreas Gohr * Abstract Ollama Model
9074b7701SAndreas Gohr *
104dd0657eSAndreas 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 === '') {
22*42b2c6e8SAndreas Gohr            throw new \Exception('Ollama base URL not configured', 3001);
23074b7701SAndreas Gohr        }
24074b7701SAndreas Gohr    }
25074b7701SAndreas Gohr
264dd0657eSAndreas Gohr    /** @inheritdoc */
274dd0657eSAndreas Gohr    function loadUnknownModelInfo(): array
284dd0657eSAndreas Gohr    {
294dd0657eSAndreas Gohr        $info = parent::loadUnknownModelInfo();
304dd0657eSAndreas Gohr
314dd0657eSAndreas Gohr        $url = $this->apiurl . 'show';
324dd0657eSAndreas Gohr
334dd0657eSAndreas Gohr        $result = $this->sendAPIRequest('POST', $url, ['model' => $this->modelName]);
344dd0657eSAndreas Gohr        foreach($result['model_info'] as $key => $value) {
354dd0657eSAndreas Gohr            if(str_ends_with($key, '.context_length')) {
364dd0657eSAndreas Gohr                $info['inputTokens'] = $value;
374dd0657eSAndreas Gohr            }
384dd0657eSAndreas Gohr            if(str_ends_with($key, '.embedding_length')) {
394dd0657eSAndreas Gohr                $info['dimensions'] = $value;
404dd0657eSAndreas Gohr            }
414dd0657eSAndreas Gohr
424dd0657eSAndreas Gohr        }
434dd0657eSAndreas Gohr
444dd0657eSAndreas Gohr        return $info;
454dd0657eSAndreas Gohr    }
464dd0657eSAndreas Gohr
47074b7701SAndreas Gohr    /**
484dd0657eSAndreas 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'];
70*42b2c6e8SAndreas Gohr            throw new \Exception('Ollama API error: ' . $error, 3002);
71074b7701SAndreas Gohr        }
72074b7701SAndreas Gohr
73074b7701SAndreas Gohr        return $response;
74074b7701SAndreas Gohr    }
75074b7701SAndreas Gohr}
76