'Nonce verification failed.')); return; } $post_title = isset($_POST['postTitle']) ? sanitize_text_field($_POST['postTitle']) : ''; // İstemci tarafından temizlenmiş post içeriği al $post_content = isset($_POST['postContent']) ? sanitize_textarea_field($_POST['postContent']) : ''; // OpenAI ve Eden AI API anahtarlarını al $aipstx_openai_key = get_option('aipstx_openai_key'); $aipstx_edenai_key = get_option('aipstx_edenai_key'); $prompt_engine = get_option('aipstx_prompt_engine', 'gpt-3.5-turbo'); $engine = isset($_POST['engine']) ? sanitize_text_field($_POST['engine']) : ''; // Eğer ilgili API anahtarı yoksa hata ver ve işlemi durdur if ($prompt_engine === 'eden_ai' && empty($aipstx_edenai_key)) { wp_send_json_error('No Eden AI API key provided'); wp_die(); } elseif ($prompt_engine !== 'eden_ai' && empty($aipstx_openai_key)) { wp_send_json_error('No OpenAI API key provided'); wp_die(); } $api_url = ''; $args = []; // Prompt engine değerine göre API isteğini yap if ($prompt_engine === 'eden_ai') { // Eden AI API isteği yap $api_url = 'https://api.edenai.run/v2/text/chat'; $args = array( 'method' => 'POST', 'timeout' => '60', 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $aipstx_edenai_key ), 'body' => wp_json_encode([ "providers" => "google", "text" => "You are a prompt engineer. Your task is to carefully analyze the following blog title and content. And create a prompt for the {$engine} model, enabling the generation of a visual that matches the text. This prompt should concisely capture the essence, main themes, and nuances of the provided blog post, aiming to facilitate the creation of the most accurate and engaging image that reflects the main message and context without being overly complex or detailed. You must create a different prompt each time a request is sent. You must generate only prompt. Prompt must be in English. Title: '{$post_title}', Content: '{$post_content}'" ]) ); } else { // OpenAI API isteği yap $api_url = 'https://api.openai.com/v1/chat/completions'; $args = array( 'method' => 'POST', 'timeout' => '60', 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $aipstx_openai_key ), 'body' => wp_json_encode([ "model" => $prompt_engine, "messages" => [ [ "role" => "system", "content" => "You are a prompt engineer. Your task is to carefully analyze the following blog title and content. And create a prompt for the {$engine} model, enabling the generation of a visual that matches the text. This prompt should concisely capture the essence, main themes, and nuances of the provided blog post, aiming to facilitate the creation of the most accurate and engaging image that reflects the main message and context without being overly complex or detailed. You must create a different prompt each time a request is sent. You must generate only prompt. Prompt must be in English." ], [ "role" => "user", "content" => "Title: '{$post_title}', Content: '{$post_content}'" ] ] ]) ); } // API isteğini yap if (!empty($api_url)) { $response = wp_remote_request($api_url, $args); // API yanıtını kontrol et ve ekrana yazdır if (is_wp_error($response)) { wp_send_json_error('No response from API'); } else { // API'den alınan yanıtı işle $decoded_response = json_decode(wp_remote_retrieve_body($response), true); $prompt = ''; if ($prompt_engine === 'eden_ai' && isset($decoded_response['google']['generated_text'])) { $prompt = $decoded_response['google']['generated_text']; } elseif (isset($decoded_response['choices'][0]['message']['content'])) { $prompt = $decoded_response['choices'][0]['message']['content']; } // ":" işaretinden sonraki metni al $colonPos = strpos($prompt, ':'); if ($colonPos !== false) { $prompt = trim(substr($prompt, $colonPos + 1)); } // Çift ve tek tırnak işaretlerini sil $prompt = str_replace(['"', "'", "*", ":"], '', $prompt); if (!empty($prompt)) { wp_send_json_success($prompt); } else { wp_send_json_error('Invalid response from API'); } } } else { wp_send_json_error('No valid API key provided'); } } } aipstx_finder::get_instance(); }