model_metadata = $model_metadata; $this->provider_metadata = $provider_metadata; $this->config = new ModelConfig(); } /** * Model metadata. * * @return ModelMetadata */ public function metadata(): ModelMetadata { return $this->model_metadata; } /** * Provider metadata. * * @return ProviderMetadata */ public function providerMetadata(): ProviderMetadata { return $this->provider_metadata; } /** * Accept a configuration. * * @param ModelConfig $config Configuration. * @return void */ public function setConfig( ModelConfig $config ): void { $this->config = $config; } /** * The active configuration. * * @return ModelConfig */ public function getConfig(): ModelConfig { return $this->config; } /** * Embed each input, in order. * * The custom option "purpose" set to "query" embeds a search question rather than a * document, which Cohere models retrieve noticeably better with. * * @param MessagePart[] $inputs Text parts. * @return EmbeddingResult * @throws RuntimeException When the site blocks the call or Bedrock reports a failure. */ public function generateEmbeddingResult( array $inputs ): EmbeddingResult { $allowed = AI_Chat_Bedrock_Core_AI::can_generate(); if ( is_wp_error( $allowed ) ) { throw new RuntimeException( esc_html( $allowed->get_error_message() ) ); } if ( empty( $inputs ) || count( $inputs ) > self::MAX_INPUTS ) { /* translators: %d: most inputs one request embeds. */ throw new RuntimeException( esc_html( sprintf( __( 'Embed between 1 and %d inputs at a time.', 'ai-chat-for-amazon-bedrock' ), self::MAX_INPUTS ) ) ); } $custom = $this->config->getCustomOptions(); $purpose = isset( $custom['purpose'] ) && 'query' === $custom['purpose'] ? 'query' : 'document'; $dimensions = method_exists( $this->config, 'getDimensions' ) ? (int) $this->config->getDimensions() : 0; $model_id = $this->model_metadata->getId(); $aws = new AI_Chat_Bedrock_AWS(); $vectors = array(); foreach ( $inputs as $input ) { if ( ! $input instanceof MessagePart || null === $input->getText() ) { throw new RuntimeException( esc_html__( 'These embedding models take text only.', 'ai-chat-for-amazon-bedrock' ) ); } $vector = $aws->embed( $input->getText(), $model_id, $purpose, $dimensions ); if ( is_wp_error( $vector ) ) { throw new RuntimeException( esc_html( $vector->get_error_message() ) ); } $vectors[] = $vector; } return new EmbeddingResult( uniqid( 'aicfab_embedding_', false ), $vectors, count( $vectors[0] ), // Bedrock reports input tokens per call; the plugin's usage log has them. new TokenUsage( 0, 0, 0 ), $this->provider_metadata, $this->model_metadata ); } }