1<?php 2 3namespace dokuwiki\plugin\aichat\Model\VoyageAI; 4 5use dokuwiki\plugin\aichat\Model\AbstractModel; 6use dokuwiki\plugin\aichat\Model\EmbeddingInterface; 7 8class EmbeddingModel extends AbstractModel implements EmbeddingInterface 9{ 10 /** @inheritdoc */ 11 public function __construct(string $name, array $config) 12 { 13 parent::__construct($name, $config); 14 15 if (empty($config['voyageai_apikey'])) { 16 throw new \Exception('Voyage AI API key not configured'); 17 } 18 19 $this->http->headers['Authorization'] = 'Bearer ' . $config['voyageai_apikey']; 20 } 21 22 /** @inheritdoc */ 23 public function getEmbedding($text): array 24 { 25 $data = [ 26 'model' => $this->getModelName(), 27 'input' => [$text], 28 ]; 29 $response = $this->request('embeddings', $data); 30 31 return $response['data'][0]['embedding']; 32 } 33 34 /** 35 * Send a request to the Voyage API 36 * 37 * @param string $endpoint 38 * @param array $data Payload to send 39 * @return array API response 40 * @throws \Exception 41 */ 42 protected function request($endpoint, $data) 43 { 44 $url = 'https://api.voyageai.com/v1/' . $endpoint; 45 return $this->sendAPIRequest('POST', $url, $data); 46 } 47 48 /** @inheritdoc */ 49 protected function parseAPIResponse($response) 50 { 51 if (isset($response['usage'])) { 52 $this->inputTokensUsed += $response['usage']['total_tokens']; 53 } 54 55 if (isset($response['error'])) { 56 throw new \Exception('OpenAI API error: ' . $response['error']['message']); 57 } 58 59 return $response; 60 } 61} 62