xref: /plugin/aichat/Model/Ollama/AbstractOllama.php (revision 2e22aefbcc83da0bc9ab21a2175b8ada4ba79826)
1074b7701SAndreas Gohr<?php
2074b7701SAndreas Gohr
3074b7701SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Ollama;
4074b7701SAndreas Gohr
5*2e22aefbSAndreas Gohruse dokuwiki\plugin\aichat\Model\Generic\AbstractGenericModel;
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 */
12*2e22aefbSAndreas Gohrabstract class AbstractOllama extends AbstractGenericModel
13074b7701SAndreas Gohr{
14074b7701SAndreas Gohr
154dd0657eSAndreas Gohr    /** @inheritdoc */
164dd0657eSAndreas Gohr    function loadUnknownModelInfo(): array
174dd0657eSAndreas Gohr    {
184dd0657eSAndreas Gohr        $info = parent::loadUnknownModelInfo();
194dd0657eSAndreas Gohr
20*2e22aefbSAndreas Gohr        $url = $this->apiurl . '/show';
214dd0657eSAndreas Gohr
224dd0657eSAndreas Gohr        $result = $this->sendAPIRequest('POST', $url, ['model' => $this->modelName]);
234dd0657eSAndreas Gohr        foreach($result['model_info'] as $key => $value) {
244dd0657eSAndreas Gohr            if(str_ends_with($key, '.context_length')) {
254dd0657eSAndreas Gohr                $info['inputTokens'] = $value;
264dd0657eSAndreas Gohr            }
274dd0657eSAndreas Gohr            if(str_ends_with($key, '.embedding_length')) {
284dd0657eSAndreas Gohr                $info['dimensions'] = $value;
294dd0657eSAndreas Gohr            }
304dd0657eSAndreas Gohr
314dd0657eSAndreas Gohr        }
324dd0657eSAndreas Gohr
334dd0657eSAndreas Gohr        return $info;
344dd0657eSAndreas Gohr    }
354dd0657eSAndreas Gohr
36*2e22aefbSAndreas Gohr
37074b7701SAndreas Gohr
38074b7701SAndreas Gohr    /** @inheritdoc */
39074b7701SAndreas Gohr    protected function parseAPIResponse($response)
40074b7701SAndreas Gohr    {
41074b7701SAndreas Gohr        if (isset($response['eval_count'])) {
42074b7701SAndreas Gohr            $this->inputTokensUsed += $response['eval_count'];
43074b7701SAndreas Gohr        }
44074b7701SAndreas Gohr
45074b7701SAndreas Gohr        if (isset($response['error'])) {
46074b7701SAndreas Gohr            $error = is_array($response['error']) ? $response['error']['message'] : $response['error'];
4742b2c6e8SAndreas Gohr            throw new \Exception('Ollama API error: ' . $error, 3002);
48074b7701SAndreas Gohr        }
49074b7701SAndreas Gohr
50074b7701SAndreas Gohr        return $response;
51074b7701SAndreas Gohr    }
52074b7701SAndreas Gohr}
53