[ 'standard' => [ '1024x1024' => 0.040, '1024x1792' => 0.080, '1792x1024' => 0.080, ], 'hd' => [ '1024x1024' => 0.080, '1024x1792' => 0.120, '1792x1024' => 0.120, ], ], 'dall-e-2' => [ 'standard' => [ '1024x1024' => 0.020, '512x512' => 0.018, '256x256' => 0.016, ], ] ]; // Логирование для отладки //error_log("Model: $model, Quality: $quality, Resolution: $resolution"); // Если модель имеет фиксированную стоимость, возвращаем её if (isset($costs[strtolower($model)]) && !is_array($costs[strtolower($model)])) { return $costs[strtolower($model)]; } // Если модель DALL-E или DALL-E 2, возвращаем стоимость по разрешению и качеству if (isset($costs[strtolower($model)][strtolower($quality)][strtolower($resolution)])) { return $costs[strtolower($model)][strtolower($quality)][strtolower($resolution)]; } // Если ничего не найдено, возвращаем 0 return 0; } // расчет стоимости генерации текста function calculate_text_generation_cost($model, $input_tokens, $output_tokens) { $costs = [ // OpenAI модели 'gpt-4' => [ 'input' => 30 / 1000000, // $30 за 1 миллион входящих токенов 'output' => 60 / 1000000, // $60 за 1 миллион исходящих токенов ], 'gpt-4-turbo' => [ 'input' => 10 / 1000000, // $10 за 1 миллион входящих токенов 'output' => 30 / 1000000, // $30 за 1 миллион исходящих токенов ], 'gpt-4o' => [ 'input' => 2.50 / 1000000, // Цена без изменений 'output' => 10.00 / 1000000, // Цена без изменений ], 'gpt-4o-mini' => [ 'input' => 0.15 / 1000000, // Цена без изменений 'output' => 0.60 / 1000000, // Цена без изменений ], 'gpt-5-mini' => [ 'input' => 0.25 / 1000000, // $0.25 за 1M входящих токенов 'output' => 2.00 / 1000000, // $2.00 за 1M исходящих токенов ], 'gpt-3.5-turbo' => [ 'input' => 3 / 1000000, 'output' => 6 / 1000000, ], 'gpt-3.5-turbo-16k' => [ 'input' => 3 / 1000000, 'output' => 4 / 1000000, ], ]; // Расчёт стоимости $input_cost = isset($costs[$model]['input']) ? $costs[$model]['input'] * $input_tokens : 0; $output_cost = isset($costs[$model]['output']) ? $costs[$model]['output'] * $output_tokens : 0; return $input_cost + $output_cost; } add_action('wp_ajax_get_estimated_cost', 'ai_content_wizard_get_estimated_cost'); function ai_content_wizard_get_estimated_cost() { global $wpdb; // Получаем параметры из POST-запроса $text_model = isset($_POST['text_model']) ? sanitize_text_field($_POST['text_model']) : 'gpt-4o-mini'; $desired_text_length = isset($_POST['text_length']) ? intval($_POST['text_length']) : 500; $post_type = isset($_POST['post_type']) ? sanitize_text_field($_POST['post_type']) : 'Single'; // Параметры изображений $enable_image = isset($_POST['enable_image']) ? boolval($_POST['enable_image']) : false; $enable_thumbnail = isset($_POST['enable_thumbnail']) ? boolval($_POST['enable_thumbnail']) : false; $enable_additional_images = isset($_POST['enable_additional_images']) ? boolval($_POST['enable_additional_images']) : false; $internal_images = isset($_POST['internal_images']) ? intval($_POST['internal_images']) : 0; $image_model = isset($_POST['image_model']) ? sanitize_text_field($_POST['image_model']) : 'deepai-hd'; $image_quality = isset($_POST['image_quality']) ? sanitize_text_field($_POST['image_quality']) : 'hd'; $image_resolution = isset($_POST['image_resolution']) ? sanitize_text_field($_POST['image_resolution']) : '1024x1024'; $additional_images = isset($_POST['additional_images']) ? intval($_POST['additional_images']) : 0; // --- Расчёт текстовой стоимости --- if (strtolower($post_type) === 'longread') { $approx_tokens = $desired_text_length / 4; $input_tokens = $approx_tokens * 0.2; $output_tokens = $approx_tokens * 0.8; } else { $approx_tokens = $desired_text_length / 4; $input_tokens = $approx_tokens * 0.5; $output_tokens = $approx_tokens * 0.5; } $theoretical_text_cost = calculate_text_generation_cost($text_model, $input_tokens, $output_tokens); // Обеспечиваем минимальную стоимость текста (даже для дешёвых моделей) $min_text_cost = 0.012; // минимальная стоимость, можно скорректировать при необходимости if ($theoretical_text_cost < $min_text_cost) { $theoretical_text_cost = $min_text_cost; } $table_name = $wpdb->prefix . 'ai_content_wizard_posts_logs'; $avg_ratio_from_logs = $wpdb->get_var( $wpdb->prepare( "SELECT AVG(text_cost*1.0/text_length) FROM {$table_name} WHERE LOWER(text_model)=%s AND LOWER(type)=%s AND text_length > 0", strtolower($text_model), strtolower($post_type) ) ); $log_text_cost = null; if ($avg_ratio_from_logs && $avg_ratio_from_logs > 0) { $log_text_cost = $desired_text_length * $avg_ratio_from_logs; } $estimated_text_cost = $theoretical_text_cost; if ($log_text_cost !== null) { if ($log_text_cost > 0 && ($theoretical_text_cost > 0)) { $ratio = $log_text_cost / $theoretical_text_cost; if ($ratio > 0.1 && $ratio < 10) { $estimated_text_cost = ($theoretical_text_cost + $log_text_cost) / 2; } elseif ($ratio <= 0.1) { $estimated_text_cost = $log_text_cost; } else { $estimated_text_cost = $theoretical_text_cost; } } } // Гарантируем минимальную итоговую текстовую стоимость if ($estimated_text_cost < $min_text_cost) { $estimated_text_cost = $min_text_cost; } // --- Расчёт стоимости изображений --- $cost_per_image = calculate_image_generation_cost($image_model, $image_quality, $image_resolution); $total_image_count = 0; if ($enable_image) { // Если в параметрах миниатюры нет, считаем, что миниатюра включена по умолчанию if (!$enable_thumbnail) { $enable_thumbnail = true; } if ($enable_thumbnail) { $total_image_count += 1; } if ($enable_additional_images) { $total_image_count += $additional_images; } // Добавляем внутренние изображения (если заданы) if ($internal_images > 0) { $total_image_count += $internal_images; } } $estimated_image_cost = $cost_per_image * $total_image_count; // --- Итоговая стоимость --- $total_estimated_cost = $estimated_text_cost + $estimated_image_cost; $total_estimated_cost_formatted = '$' . number_format((float)$total_estimated_cost, 3, '.', ''); wp_send_json_success([ 'estimated_cost' => $total_estimated_cost_formatted, 'debug_info' => [] // лог отключён ]); wp_die(); }