anthropic.php
8 months ago
chatml.php
7 months ago
core.php
8 months ago
factory.php
8 months ago
google.php
7 months ago
mistral.php
9 months ago
open-router.php
7 months ago
openai.php
7 months ago
perplexity.php
10 months ago
replicate.php
7 months ago
perplexity.php
199 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Engines_Perplexity extends Meow_MWAI_Engines_ChatML { |
| 4 | // Streaming |
| 5 | protected $streamInTokens = null; |
| 6 | protected $streamOutTokens = null; |
| 7 | protected $streamContent = null; |
| 8 | protected $streamBuffer = null; |
| 9 | protected $inCitations = null; |
| 10 | |
| 11 | public function __construct( $core, $env ) { |
| 12 | parent::__construct( $core, $env ); |
| 13 | } |
| 14 | |
| 15 | protected function set_environment() { |
| 16 | $env = $this->env; |
| 17 | $this->apiKey = $env['apikey'] ?? null; |
| 18 | } |
| 19 | |
| 20 | protected function get_service_name() { |
| 21 | return 'Perplexity'; |
| 22 | } |
| 23 | |
| 24 | public function get_models() { |
| 25 | return apply_filters( 'mwai_perplexity_models', MWAI_PERPLEXITY_MODELS ); |
| 26 | } |
| 27 | |
| 28 | public static function get_models_static() { |
| 29 | return MWAI_PERPLEXITY_MODELS; |
| 30 | } |
| 31 | |
| 32 | protected function build_url( $query, $endpoint = null ) { |
| 33 | $endpoint = apply_filters( 'mwai_perplexity_endpoint', 'https://api.perplexity.ai', $this->env ); |
| 34 | return rtrim( $endpoint, '/' ) . '/chat/completions'; |
| 35 | } |
| 36 | |
| 37 | protected function build_messages( $query ) { |
| 38 | $messages = parent::build_messages( $query ); |
| 39 | $filtered = []; |
| 40 | $haveSeenUser = false; |
| 41 | foreach ( $messages as $message ) { |
| 42 | if ( !$haveSeenUser ) { |
| 43 | if ( $message['role'] === 'assistant' ) { |
| 44 | continue; |
| 45 | } |
| 46 | if ( $message['role'] === 'user' ) { |
| 47 | $haveSeenUser = true; |
| 48 | } |
| 49 | $filtered[] = $message; |
| 50 | } |
| 51 | else { |
| 52 | $filtered[] = $message; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $filtered; |
| 57 | } |
| 58 | |
| 59 | protected function build_headers( $query ) { |
| 60 | if ( $query->apiKey ) { |
| 61 | $this->apiKey = $query->apiKey; |
| 62 | } |
| 63 | if ( empty( $this->apiKey ) ) { |
| 64 | throw new Exception( 'No Perplexity API Key provided. Check your settings.' ); |
| 65 | } |
| 66 | return [ |
| 67 | 'Content-Type' => 'application/json', |
| 68 | 'Authorization' => 'Bearer ' . $this->apiKey, |
| 69 | 'User-Agent' => 'AI Engine', |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | protected function build_body( $query, $streamCallback = null, $extra = null ) { |
| 74 | $body = parent::build_body( $query, $streamCallback, $extra ); |
| 75 | return $body; |
| 76 | } |
| 77 | |
| 78 | public function reset_stream() { |
| 79 | $this->inCitations = null; |
| 80 | return parent::reset_stream(); |
| 81 | } |
| 82 | |
| 83 | // Let's override the stream handler only to capture the citations |
| 84 | protected function stream_data_handler( $json ) { |
| 85 | if ( isset( $json['citations'] ) ) { |
| 86 | $this->inCitations = $json['citations']; |
| 87 | } |
| 88 | return parent::stream_data_handler( $json ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * In Perplexity, we intercept the final choices and insert citations |
| 93 | * as ([domain1](https://...), [domain2](https://...)) |
| 94 | * based on [1][2][3] references in the text. |
| 95 | */ |
| 96 | protected function finalize_choices( $choices, $responseData, $query ) { |
| 97 | $citations = isset( $responseData['citations'] ) ? $responseData['citations'] : null; |
| 98 | if ( empty( $citations ) && !empty( $this->inCitations ) ) { |
| 99 | $citations = $this->inCitations; |
| 100 | } |
| 101 | if ( empty( $citations ) ) { |
| 102 | return parent::finalize_choices( $choices, $responseData, $query ); |
| 103 | } |
| 104 | |
| 105 | foreach ( $choices as &$choice ) { |
| 106 | if ( isset( $choice['message']['content'] ) && is_string( $choice['message']['content'] ) ) { |
| 107 | $content = $choice['message']['content']; |
| 108 | $content = preg_replace_callback( '/\[(\d+)\](?:\s*\[(\d+)\])*/', function ( $matches ) use ( $citations ) { |
| 109 | preg_match_all( '/\[(\d+)\]/', $matches[0], $refs ); |
| 110 | $links = []; |
| 111 | foreach ( $refs[1] as $refNumber ) { |
| 112 | $index = (int) $refNumber - 1; |
| 113 | if ( isset( $citations[$index] ) ) { |
| 114 | $url = $citations[$index]; |
| 115 | $domain = parse_url( $url, PHP_URL_HOST ); |
| 116 | if ( !empty( $domain ) ) { |
| 117 | $domain = str_replace( 'www.', '', $domain ); |
| 118 | } |
| 119 | $links[] = '[' . $domain . '](' . $url . ')'; |
| 120 | } |
| 121 | } |
| 122 | // If we gathered at least one link, return them all in a parenthetical group |
| 123 | // e.g. " ([google.com](https://google.com), [othersite.io](https://othersite.io))" |
| 124 | // Otherwise, just return the original matches (fallback). |
| 125 | return !empty( $links ) ? ' (' . implode( ', ', $links ) . ')' : $matches[0]; |
| 126 | }, $content ); |
| 127 | $choice['message']['content'] = $content; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return $choices; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Connection check for Perplexity API |
| 136 | * Tests the API key by listing async chat completions |
| 137 | */ |
| 138 | public function connection_check() { |
| 139 | try { |
| 140 | // Build the URL for async completions list endpoint |
| 141 | $endpoint = apply_filters( 'mwai_perplexity_endpoint', 'https://api.perplexity.ai', $this->env ); |
| 142 | $url = rtrim( $endpoint, '/' ) . '/async/chat/completions?limit=1'; |
| 143 | |
| 144 | // Build headers with API key |
| 145 | if ( empty( $this->apiKey ) ) { |
| 146 | throw new Exception( 'No Perplexity API Key provided. Check your settings.' ); |
| 147 | } |
| 148 | |
| 149 | $headers = [ |
| 150 | 'Authorization' => 'Bearer ' . $this->apiKey, |
| 151 | 'User-Agent' => 'AI Engine', |
| 152 | ]; |
| 153 | |
| 154 | $options = [ |
| 155 | 'headers' => $headers, |
| 156 | 'method' => 'GET', |
| 157 | 'timeout' => 10, |
| 158 | 'sslverify' => false |
| 159 | ]; |
| 160 | |
| 161 | // Make the request |
| 162 | $response = wp_remote_get( $url, $options ); |
| 163 | |
| 164 | if ( is_wp_error( $response ) ) { |
| 165 | throw new Exception( $response->get_error_message() ); |
| 166 | } |
| 167 | |
| 168 | $body = wp_remote_retrieve_body( $response ); |
| 169 | $data = json_decode( $body, true ); |
| 170 | |
| 171 | // Check if response has expected structure |
| 172 | if ( !is_array( $data ) || !array_key_exists( 'requests', $data ) ) { |
| 173 | throw new Exception( 'Invalid response from Perplexity API' ); |
| 174 | } |
| 175 | |
| 176 | // Get available models from our constants |
| 177 | $models = $this->get_models(); |
| 178 | $modelNames = array_map( function( $model ) { |
| 179 | return $model['model'] ?? $model['name'] ?? 'unknown'; |
| 180 | }, array_slice( $models, 0, 5 ) ); |
| 181 | |
| 182 | return [ |
| 183 | 'success' => true, |
| 184 | 'service' => 'Perplexity', |
| 185 | 'message' => 'Connection successful', |
| 186 | 'details' => [ |
| 187 | 'endpoint' => $endpoint, |
| 188 | 'model_count' => count( $models ), |
| 189 | 'sample_models' => $modelNames |
| 190 | ] |
| 191 | ]; |
| 192 | } |
| 193 | catch ( Exception $e ) { |
| 194 | throw new Exception( 'Perplexity connection failed: ' . $e->getMessage() ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | } |
| 199 |