diff --git a/src/Embeddings/MistralAIEmbeddingFunction.php b/src/Embeddings/MistralAIEmbeddingFunction.php new file mode 100644 index 0000000..1884351 --- /dev/null +++ b/src/Embeddings/MistralAIEmbeddingFunction.php @@ -0,0 +1,55 @@ + "Bearer $this->apiKey", + 'Content-Type' => 'application/json', + ]; + + + $this->client = new Client([ + 'base_uri' => 'https://api.mistral.ai/v1/', + 'headers' => $headers + ]); + } + + /** + * @inheritDoc + */ + public function generate(array $texts): array + { + try { + $response = $this->client->post('embeddings', [ + 'json' => [ + 'model' => $this->model, + 'input' => $texts, + ] + ]); + + $result = json_decode($response->getBody()->getContents(), true); + $embeddings = $result['data']; + usort($embeddings, fn($a, $b) => $a['index'] <=> $b['index']); + + return array_map(fn($embedding) => $embedding['embedding'], $embeddings); + } catch (ClientExceptionInterface $e) { + throw new \RuntimeException("Error calling MistralAI API: {$e->getMessage()}", 0, $e); + } + } +}