usingProviderModel( $target->get_provider(), $target->get_model() ); if ( null !== $target->get_dimensions() ) { $builder->usingDimensions( $target->get_dimensions() ); } $result = $builder->generateEmbeddingResult(); } catch ( Throwable $e ) { throw new Embedding_Client_Exception( esc_html( $e->getMessage() ), self::classify( $e ), $e ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } $vectors = array(); foreach ( $result->getEmbeddings() as $embedding ) { $vectors[] = array_map( 'floatval', array_values( $embedding->getValues() ) ); } return $vectors; } /** * Classifies an SDK failure. * * HTTP client and server exceptions carry the status code as their exception code. Retry-After * is not exposed by the SDK, so rate-limit delays are computed by {@see Provider_Backoff}. * * @since 1.5.0 * * @param \Throwable $error The failure. * @return string One of the Embedding_Client_Exception class constants. */ public static function classify( Throwable $error ): string { // ClientException extends the SDK's InvalidArgumentException, so it must be checked first. if ( $error instanceof ClientException ) { $status = (int) $error->getCode(); if ( 429 === $status ) { return Embedding_Client_Exception::RATE_LIMITED; } if ( 408 === $status ) { return Embedding_Client_Exception::TRANSIENT; } if ( in_array( $status, array( 401, 402, 403, 404 ), true ) ) { return Embedding_Client_Exception::PROVIDER; } return Embedding_Client_Exception::ITEM; } if ( $error instanceof TokenLimitReachedException ) { return Embedding_Client_Exception::ITEM; } if ( $error instanceof ServerException || $error instanceof NetworkException ) { return Embedding_Client_Exception::TRANSIENT; } // Unregistered provider, unknown model, unsupported capability: no request can succeed. if ( $error instanceof Ai_Invalid_Argument_Exception ) { return Embedding_Client_Exception::PROVIDER; } return Embedding_Client_Exception::TRANSIENT; } }