api_key = $api_key; $this->model = $model; } public function embed(string $text, $retries = 1) { $cache_key = 'shoponix_emb_' . md5($text); $cached = get_transient($cache_key); if ($cached) { return $cached; } $response = wp_remote_post('https://api.mistral.ai/v1/embeddings', [ 'headers' => [ 'Authorization' => 'Bearer ' . $this->api_key, 'Content-Type' => 'application/json' ], 'body' => json_encode([ 'model' => $this->model, 'input' => $text ]), 'timeout' => 30 ]); if (is_wp_error($response)) { return false; } $status_code = wp_remote_retrieve_response_code($response); if ($status_code === 429 && $retries > 0) { sleep(2); // Wait 2 seconds before retrying return $this->embed($text, $retries - 1); } $body = json_decode(wp_remote_retrieve_body($response), true); $embedding = $body['data'][0]['embedding'] ?? false; if ($embedding) { set_transient($cache_key, $embedding, 5 * MINUTE_IN_SECONDS); } return $embedding; } }