xref: /plugin/aichat/Model/Ollama/AbstractOllama.php (revision 2e22aefbcc83da0bc9ab21a2175b8ada4ba79826)
1<?php
2
3namespace dokuwiki\plugin\aichat\Model\Ollama;
4
5use dokuwiki\plugin\aichat\Model\Generic\AbstractGenericModel;
6
7/**
8 * Abstract Ollama Model
9 *
10 * This class provides a basic interface to the Ollama API
11 */
12abstract class AbstractOllama extends AbstractGenericModel
13{
14
15    /** @inheritdoc */
16    function loadUnknownModelInfo(): array
17    {
18        $info = parent::loadUnknownModelInfo();
19
20        $url = $this->apiurl . '/show';
21
22        $result = $this->sendAPIRequest('POST', $url, ['model' => $this->modelName]);
23        foreach($result['model_info'] as $key => $value) {
24            if(str_ends_with($key, '.context_length')) {
25                $info['inputTokens'] = $value;
26            }
27            if(str_ends_with($key, '.embedding_length')) {
28                $info['dimensions'] = $value;
29            }
30
31        }
32
33        return $info;
34    }
35
36
37
38    /** @inheritdoc */
39    protected function parseAPIResponse($response)
40    {
41        if (isset($response['eval_count'])) {
42            $this->inputTokensUsed += $response['eval_count'];
43        }
44
45        if (isset($response['error'])) {
46            $error = is_array($response['error']) ? $response['error']['message'] : $response['error'];
47            throw new \Exception('Ollama API error: ' . $error, 3002);
48        }
49
50        return $response;
51    }
52}
53