xref: /plugin/aichat/Model/Ollama/EmbeddingModel.php (revision d2192bbac7011c00f04e23a1c78c073ab960d6a6)
1<?php
2
3namespace dokuwiki\plugin\aichat\Model\Ollama;
4
5use dokuwiki\plugin\aichat\Model\EmbeddingInterface;
6
7class EmbeddingModel extends AbstractOllama implements EmbeddingInterface
8{
9    /** @inheritdoc */
10    public function getEmbedding($text): array
11    {
12        $data = [
13            'model' => $this->getModelNameWithSuffix(),
14            'input' => $text,
15            'options' => [
16                'num_ctx' => $this->getMaxInputTokenLength()
17            ]
18        ];
19        $response = $this->request('api/embed', $data);
20        return $response['embeddings'][0] ?? [];
21    }
22
23    private function getModelNameWithSuffix(): string
24    {
25        $modelName = $this->getModelName();
26        return str_ends_with($modelName, ':latest') ? $modelName : $modelName . ':latest';
27    }
28}
29
30