$claims Claim sentences. * @param array $context Optional context: title, published date. * @return array|WP_Error List of verdict arrays. */ public static function verify( array $claims, array $context = array() ) { $key = AISC_Settings::get( 'perplexity_key' ); if ( empty( $key ) ) { return new WP_Error( 'aisc_no_key', __( 'No Perplexity API key is configured.', 'ai-slop-checker' ) ); } $claims = array_values( array_filter( array_map( 'trim', $claims ) ) ); if ( empty( $claims ) ) { return array(); } $model = AISC_Settings::get( 'perplexity_model' ); $body = array( 'model' => $model, 'temperature' => 0.1, 'messages' => array( array( 'role' => 'system', 'content' => AISC_Verdict::system_prompt(), ), array( 'role' => 'user', 'content' => AISC_Verdict::user_prompt( $claims, $context ), ), ), ); $cache_key = AISC_HTTP::cache_key( 'pplx', array( $model, $claims ) ); $response = AISC_HTTP::request( self::ENDPOINT, array( 'method' => 'POST', 'headers' => array( 'Authorization' => 'Bearer ' . $key, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $body ), ), $cache_key ); if ( is_wp_error( $response ) ) { return $response; } return self::parse( $response, $claims ); } /** * Parse the model response into verdict rows. * * @param array $response Decoded API response. * @param array $claims Original claims, for re-pairing by index. * @return array|WP_Error */ private static function parse( $response, array $claims ) { $content = $response['choices'][0]['message']['content'] ?? ''; if ( '' === trim( (string) $content ) ) { return new WP_Error( 'aisc_empty', __( 'Perplexity returned an empty response.', 'ai-slop-checker' ) ); } $data = AISC_Verdict::decode_loose( $content ); if ( null === $data || ! isset( $data['results'] ) || ! is_array( $data['results'] ) ) { return new WP_Error( 'aisc_unparsable', __( 'Could not read a verdict list out of the Perplexity response.', 'ai-slop-checker' ) ); } return AISC_Verdict::rows_to_verdicts( $data['results'], $claims, 'perplexity', self::citations( $response ) ); } /** * Collect top-level citations, which vary by model generation. * * @param array $response Decoded API response. * @return array */ private static function citations( $response ) { $sources = array(); foreach ( array( 'search_results', 'citations' ) as $field ) { if ( empty( $response[ $field ] ) || ! is_array( $response[ $field ] ) ) { continue; } foreach ( $response[ $field ] as $item ) { if ( is_array( $item ) ) { $url = $item['url'] ?? ''; $label = $item['title'] ?? ''; } else { $url = $item; $label = ''; } if ( $url ) { $sources[] = array( 'url' => $url, 'label' => $label, ); } } } return $sources; } }