$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( 'gemini_key' ); if ( empty( $key ) ) { return new WP_Error( 'aisc_no_key', __( 'No Gemini 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( 'gemini_model' ); $body = array( 'systemInstruction' => array( 'parts' => array( array( 'text' => AISC_Verdict::system_prompt() ), ), ), 'contents' => array( array( 'role' => 'user', 'parts' => array( array( 'text' => AISC_Verdict::user_prompt( $claims, $context ) ), ), ), ), // Google Search grounding. Note that Gemini does not allow a forced // JSON response mime type alongside a tool, which is why the parser // downstream has to be tolerant of surrounding prose. 'tools' => array( array( self::search_tool( $model ) => new stdClass() ), ), 'generationConfig' => array( 'temperature' => 0.1, ), ); $url = sprintf( self::ENDPOINT, rawurlencode( $model ) ); $cache_key = AISC_HTTP::cache_key( 'gem', array( $model, $claims ) ); $response = AISC_HTTP::request( $url, array( 'method' => 'POST', 'headers' => array( // Header rather than a query parameter, so the key stays out // of URLs and any logs that record them. 'x-goog-api-key' => $key, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $body ), ), $cache_key ); if ( is_wp_error( $response ) ) { return $response; } return self::parse( $response, $claims ); } /** * Name of the search-grounding tool for a given model. * * Gemini renamed it between generations: 1.5 uses google_search_retrieval, * 2.0 onwards uses google_search. The model field is free text so a user can * pick a newer ID than this plugin knows about, which means the wrong name * here is a plausible mistake rather than a theoretical one. * * @param string $model Model ID. * @return string */ private static function search_tool( $model ) { $tool = ( false !== stripos( (string) $model, '1.5' ) ) ? 'google_search_retrieval' : 'google_search'; /** * Filter the Gemini grounding tool name. * * @param string $tool Tool key. * @param string $model Model ID. */ return apply_filters( 'aisc_gemini_search_tool', $tool, $model ); } /** * Parse the Gemini 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 ) { if ( isset( $response['promptFeedback']['blockReason'] ) ) { return new WP_Error( 'aisc_blocked', sprintf( /* translators: %s: block reason from the API. */ __( 'Gemini declined to answer (%s).', 'ai-slop-checker' ), sanitize_text_field( $response['promptFeedback']['blockReason'] ) ) ); } $candidate = $response['candidates'][0] ?? array(); // A grounded answer can arrive across several parts; join them all. $content = ''; foreach ( ( $candidate['content']['parts'] ?? array() ) as $part ) { if ( isset( $part['text'] ) ) { $content .= $part['text']; } } if ( '' === trim( $content ) ) { return new WP_Error( 'aisc_empty', __( 'Gemini 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 Gemini response.', 'ai-slop-checker' ) ); } return AISC_Verdict::rows_to_verdicts( $data['results'], $claims, 'gemini', self::grounding_urls( $candidate ) ); } /** * Pull the sources Gemini actually retrieved during grounding. * * These are the honest citations — the model naming a source in its JSON is * a weaker signal than the search tool reporting what it fetched. * * The URI is a redirect through vertexaisearch.cloud.google.com rather than * the publisher's own address, so the label has to come from the accompanying * domain or title. Without it every citation displays as the same Google * hostname, which tells a reader nothing. * * @param array $candidate Candidate node from the response. * @return array */ private static function grounding_urls( $candidate ) { $sources = array(); foreach ( ( $candidate['groundingMetadata']['groundingChunks'] ?? array() ) as $chunk ) { $web = $chunk['web'] ?? array(); if ( empty( $web['uri'] ) ) { continue; } $sources[] = array( 'url' => $web['uri'], // domain is the cleanest when present; title is the usual case. 'label' => $web['domain'] ?? ( $web['title'] ?? '' ), ); } return $sources; } }