name)) { $total_used_tokens += count(aimogen_encode($func_count['function']->name)); } if(isset($func_count['function']->description)) { $total_used_tokens += count(aimogen_encode($func_count['function']->description)); } if(isset($func_count['function']->parameters) && is_array($func_count['function']->parameters)) { foreach($func_count['function']->parameters as $fpar) { if(isset($fpar->name)) { $total_used_tokens += count(aimogen_encode($fpar->name)); } if(isset($fpar->description)) { $total_used_tokens += count(aimogen_encode($fpar->description)); } } } } $available_tokens = $available_tokens - $total_used_tokens; if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_CHAT) { do_action('aimogen_on_token_limit_warning', $available_tokens, $aicontent, $model, $assistant_id); if(is_string($aicontent)) { $string_len = strlen($aicontent); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent = aimogen_substr($aicontent, 0 - $string_len); $aicontent = trim($aicontent); if(empty($aicontent)) { $error = 'Incorrect chat prompt provided(2): ' . $aicontent; $error = apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } $query_token_count = count(aimogen_encode($aicontent)); $max_tokens = aimogen_get_max_tokens($model); $available_tokens = $max_tokens - $query_token_count; } else { $aitext = ''; foreach($aicontent as $aimess) { if(isset($aimess['content'])) { if(!is_array($aimess['content'])) { $aitext .= $aimess['content'] . '\n'; } else { foreach($aimess['content'] as $internalmess) { if($internalmess['type'] == 'text') { $aitext .= $internalmess['text'] . '\n'; } } } } } $max_tokens = aimogen_get_max_tokens($model); $query_token_count = count(aimogen_encode($aitext)); $available_tokens = aimogen_compute_available_tokens($model, $max_tokens, $aitext, $query_token_count); if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $startIndex = intdiv(count($aicontent), 2); $aicontent = array_slice($aicontent, $startIndex); $ars = array_keys($aicontent); $lastindex = end($ars); $string_len = strlen($aicontent[$lastindex]['content']); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent[$lastindex]['content'] = aimogen_substr($aicontent[$lastindex]['content'], 0, $string_len); $aicontent[$lastindex]['content'] = trim($aicontent[$lastindex]['content']); $aitext = ''; foreach($aicontent as $aimess) { if(isset($aimess['content'])) { if(!is_array($aimess['content'])) { $aitext .= $aimess['content'] . '\n'; } else { foreach($aimess['content'] as $internalmess) { if($internalmess['type'] == 'text') { $aitext .= $internalmess['text'] . '\n'; } } } } } $query_token_count = count(aimogen_encode($aitext)); $available_tokens = $max_tokens - $query_token_count; } } } } } } $available_tokens = apply_filters('aimogen_available_tokens_before_check', $available_tokens, $aicontent, $model, $assistant_id); if(isset($aimogen_Main_Settings['multiple_key']) && $aimogen_Main_Settings['multiple_key'] == 'on') { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); } if ( empty($token) ) { $error = esc_html__('Empty API key provided!', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } if(empty($user_question)) { if(is_array($aicontent)) { $arkey = array_keys($aicontent); $lastindex = end($arkey); if(isset($aicontent[$lastindex]['content'])) { $user_question = $aicontent[$lastindex]['content']; } } else { $user_question = $aicontent; } } if(empty($assistant_id)) { $assistant_id = ''; } $store_data = apply_filters('aimogen_should_store_data', $store_data, $aicontent, $model, $assistant_id); if(!empty(trim($assistant_id)) && !aimogen_is_aimogen_key($token) && !(aimogen_check_if_azure_or_others($aimogen_Main_Settings))) { if(!aimogen_is_vision_model('', $assistant_id) && $vision_file != '') { $vision_file = ''; } if(!empty($vision_file)) { do_action('aimogen_before_vision_file_process', $vision_file, $aicontent, $model, $assistant_id); } try { $local_assistant_id = ''; if(is_numeric($assistant_id)) { $assistant_id_temp = get_post_meta($assistant_id, '_assistant_id', true); if(!empty($assistant_id_temp)) { $local_assistant_id = $assistant_id; $assistant_id = $assistant_id_temp; } } if(empty($local_assistant_id)) { $local_assist = aimogen_find_local_assistant_id($assistant_id); if($local_assist !== false) { $local_assistant_id = $local_assist; } } do_action('aimogen_before_assistant_ai_query', $token, $assistant_id, $local_assistant_id, $role, $user_question, $thread_id, $no_internet, $no_embeddings, $env, $retry_count, $embedding_namespace, $stream, $function_result, $vision_file, $file_data); $response_ai = aimogen_generate_text_assistant($token, $assistant_id, $local_assistant_id, $role, $user_question, $thread_id, $no_internet, $no_embeddings, $env, $retry_count, $embedding_namespace, $stream, $function_result, $vision_file, $file_data); do_action('aimogen_after_assistant_ai_query', $response_ai, $token, $assistant_id, $local_assistant_id, $role, $user_question, $thread_id, $no_internet, $no_embeddings, $env, $retry_count, $embedding_namespace, $stream, $function_result, $vision_file, $file_data); if(isset($response_ai['content'][0]['text']['value'])) { $response_text = $response_ai['content'][0]['text']['value']; } else { throw new Exception('Cannot parse AI response: ' . json_encode($response_ai)); } } catch(Exception $e) { $error = 'Error in AI (' . $assistant_id . '): ' . $e->getMessage(); apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } if($response_text === false || empty($response_text)) { $error = apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } if($is_chat == false) { $response_text = aimogen_clean_language_model_texts($response_text); $response_text= trim($response_text); } } else { if(!aimogen_is_vision_model($model, '') && $vision_file != '') { $vision_file = ''; } if(!empty($vision_file)) { do_action('aimogen_before_vision_file_process', $vision_file, $aicontent, $model, ''); } if(aimogen_is_chatgpt_model($model) || aimogen_is_chatgpt_turbo_model($model) || aimogen_is_perplexity_model($model) || aimogen_is_google_chat_model($model) || aimogen_is_groq_model($model) || aimogen_is_nvidia_model($model) || aimogen_is_xai_model($model) || aimogen_is_chatgpt_o_model($model) || aimogen_is_o1_model($model) || aimogen_is_huggingface_model($model) || aimogen_is_ollama_model($model) || aimogen_is_openrouter_model($model) || aimogen_is_custom_model($model, $aimogen_Main_Settings)) { if(is_array($aicontent)) { $chatgpt_obj = $aicontent; } else { $role = 'user'; $chatgpt_obj = array(); $chatgpt_obj[] = array("role" => $role, "content" => $aicontent); $additional_tokens = count(aimogen_encode($role . ': ')); if(aimogen_check_if_available_token_recalc_needed($model, $aimogen_Main_Settings)) { if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_CHAT) { do_action('aimogen_on_token_limit_warning', $available_tokens, $aicontent, $model, $assistant_id); $string_len = strlen($aicontent); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent = aimogen_substr($aicontent, 0 - $string_len); $aicontent = trim($aicontent); if(empty($aicontent)) { $error = 'Incorrect chat prompt provided: ' . $aicontent; $error = apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } $query_token_count = count(aimogen_encode($aicontent)); $max_tokens = aimogen_get_max_tokens($model); $available_tokens = $max_tokens - $query_token_count; $chatgpt_obj = array(); $chatgpt_obj[] = array("role" => $role, "content" => $aicontent); } } if($available_tokens - $additional_tokens <= 0) { $error = 'Not enough tokens for the call: ' . $aicontent; $error = apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } else { if(aimogen_check_if_available_token_recalc_needed($model, $aimogen_Main_Settings)) { $available_tokens = $available_tokens - $additional_tokens; } } } do_action('aimogen_before_chat_ai_query', $token, $model, $chatgpt_obj, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, $no_internet, $no_embeddings, $functions, $stream, $vision_file, false, $user_question, $embedding_namespace, $function_result, false, $store_data); $response_text = aimogen_generate_text_chat($token, $model, $chatgpt_obj, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, $no_internet, $no_embeddings, $functions, $stream, $vision_file, false, $user_question, $embedding_namespace, $function_result, false, $store_data, $enabled_mcp_servers); do_action('aimogen_after_chat_ai_query', $response_text, $token, $model, $chatgpt_obj, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, $no_internet, $no_embeddings, $functions, $stream, $vision_file, false, $user_question, $embedding_namespace, $function_result, false, $store_data); if($response_text === false || empty($response_text)) { $error = apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } if(stristr($response_text, ']*>([\s\S]*?)<\s*\/body>/i", $response_text, $matches); if(isset($matches[1][0])) { $response_text = trim($matches[1][0]); } } if($is_chat == false) { $response_text = aimogen_clean_language_model_texts($response_text); $response_text= trim($response_text); } } else { if(is_array($aicontent)) { $aitext = ''; foreach($aicontent as $aimess) { if(isset($aimess['content'])) { if(!is_array($aimess['content'])) { $aitext .= $aimess['content'] . '\n'; } else { foreach($aimess['content'] as $internalmess) { if($internalmess['type'] == 'text') { $aitext .= $internalmess['text'] . '\n'; } } } } } $aicontent = $aitext; } if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($aicontent); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent = aimogen_substr($aicontent, 0, $string_len); $aicontent = trim($aicontent); $query_token_count = count(aimogen_encode($aicontent)); $max_tokens = aimogen_get_max_tokens($model); $available_tokens = $max_tokens - $query_token_count; } do_action('aimogen_before_completion_ai_query', $token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, $no_internet, $no_embeddings, $stream, $user_question, $embedding_namespace, $vision_file); $response_text = aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, $no_internet, $no_embeddings, $stream, $user_question, $embedding_namespace, $vision_file); do_action('aimogen_after_chat_ai_query', $response_text, $token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, $no_internet, $no_embeddings, $stream, $user_question, $embedding_namespace, $vision_file); if($response_text === false || empty($response_text)) { $error = apply_filters('aimogen_modify_ai_error', $error); do_action('aimogen_on_error', $error, $aicontent, $model, $assistant_id); return false; } } } $response_text = aimogen_sanitize_ai_result($response_text); $response_text = apply_filters( 'aimogen_modify_ai_reply', $response_text, $aicontent ); if (!isset($aimogen_Main_Settings['no_pre_code_remove']) || $aimogen_Main_Settings['no_pre_code_remove'] != 'on') { $response_text = aimogen_pre_code_remove($response_text); } $response_text = preg_replace('/```html([\s\S]*?)```/', '$1', $response_text); if ($parse_markdown == true && isset($aimogen_Main_Settings['markdown_parse']) && $aimogen_Main_Settings['markdown_parse'] == 'on') { if(aimogen_containsMarkdown($response_text)) { $response_text = aimogen_parse_markdown($response_text); } } $response_text = str_replace(array('???','???'), '-', $response_text); $response_text = str_replace(array('???','???'), '\'', $response_text); $response_text = str_replace(array('???','???'), '"', $response_text); $response_text = apply_filters('aimogen_final_response_text', $response_text, $aicontent, $model, $assistant_id); do_action('aimogen_on_successful_response', $response_text, $aicontent, $model, $assistant_id); return $response_text; } function aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, &$finish_reason, &$error, $no_internet = false, $no_embeddings = false, $stream = false, $user_question = '', $embedding_namespace = '', $vision_file = '') { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $enabled_mcp_servers = ''; $embeddings_enabled = false; $internet_enabled = false; $max_tokens = aimogen_get_max_tokens($model); $particular_embedding_namespace = ''; aimogen_get_internet_embeddings_result($aimogen_Main_Settings, $env, $embeddings_enabled, $internet_enabled, $particular_embedding_namespace); if(empty($embedding_namespace) && !empty($particular_embedding_namespace)) { $embedding_namespace = $particular_embedding_namespace; } $content_tokens = count(aimogen_encode($aicontent)); $total_tokens = $content_tokens + $available_tokens; if($total_tokens >= $max_tokens && !aimogen_is_new_token_window_model($model)) { $available_tokens = $max_tokens - $content_tokens; if($available_tokens < AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($aicontent); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent = aimogen_substr($aicontent, 0, $string_len); $aicontent = trim($aicontent); if(empty($aicontent)) { $error = 'Empty prompt returned after content trimming!'; return false; } $query_token_count = count(aimogen_encode($aicontent)); $available_tokens = $max_tokens - $query_token_count; } } $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'text'; $maxResults = 1; $query = new Aimogen_lite_Query($aicontent, $available_tokens, $model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, '', ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = $ok; return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating AI completion text using model: ' . $model . ' and prompt: ' . $aicontent); } $delay = ''; if (isset($aimogen_Main_Settings['request_delay']) && $aimogen_Main_Settings['request_delay'] != '') { if(stristr($aimogen_Main_Settings['request_delay'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay'])); } } } if($delay != '' && is_numeric($delay)) { usleep(intval($delay) * 1000); } if($temperature < 0 || $temperature > 1) { $temperature = 1; } if($top_p < 0 || $top_p > 1) { $top_p = 1; } if($presence_penalty < -2 || $presence_penalty > 2) { $presence_penalty = 0; } if($frequency_penalty < -2 || $frequency_penalty > 2) { $frequency_penalty = 0; } if($temperature == '' || (empty($temperature) && $temperature !== 0)) { $temperature = 1; } if($top_p == '' || (empty($top_p) && $top_p !== 0)) { $top_p = 1; } if($presence_penalty == '' || (empty($presence_penalty) && $presence_penalty !== 0)) { $presence_penalty = 0; } if($frequency_penalty == '' || (empty($frequency_penalty) && $frequency_penalty !== 0)) { $frequency_penalty = 0; } if(aimogen_is_aimogen_key($token)) { $pargs = array(); $api_url = 'https://aiomaticapi.com/apis/ai/v1/text/'; $pargs['model'] = trim($model); $pargs['temperature'] = $temperature; $pargs['top_p'] = $top_p; $pargs['presence_penalty'] = $presence_penalty; $pargs['frequency_penalty'] = $frequency_penalty; $pargs['prompt'] = trim($aicontent); if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { $pargs['max_tokens'] = $available_tokens; } } $api_headers = array( 'Authorization' => 'Bearer ' . trim($token) ); $ai_response = aimogen_get_web_page_api($api_url, $pargs, $api_headers); if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get AimogenAPI response!'; return false; } } $ai_json = json_decode($ai_response); if($ai_json === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to decode AimogenAPI response: ' . $ai_response; return false; } } if(isset($ai_json->error)) { if (stristr($ai_json->error, 'Your subscription expired, please renew it.') === false && stristr($ai_json->error, '[RATE LIMITED]') === false && isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after error failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error while processing AI response: ' . $ai_json->error; return false; } } if(!isset($ai_json->result)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after parse failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to parse AimogenAPI response: ' . $ai_response; return false; } } if(isset($ai_json->remainingtokens)) { set_transient('aimogenapi_tokens', $ai_json->remainingtokens, 86400); } $ai_json = apply_filters( 'aimogen_ai_reply_raw', $ai_json, $aicontent ); apply_filters( 'aimogen_ai_reply', $ai_json->result, $query ); return $ai_json->result; } elseif (aimogen_is_claude_model($model)) { if(in_array($model, AIMOGEN_CLAUDE_MODELS) === false) { $error = 'This model is not currently supported by Claude API: ' . $model; return false; } if (!isset($aimogen_Main_Settings['app_id_claude']) || trim($aimogen_Main_Settings['app_id_claude']) == '') { aimogen_log_to_file('You need to enter an Anthropic Claude API key in the plugin settings for this feature to work!'); return false; } $appids_claude = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_claude'])); $appids_claude = array_filter($appids_claude); $token_claude = $appids_claude[array_rand($appids_claude)]; if($retry_count > 0) { $token_claude = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_claude']), $token); } $result = aimogen_generate_text_local_claude($token_claude, $model, $aicontent, $temperature, $top_p, $vision_file, $available_tokens, $stream, $is_chat, $enabled_mcp_servers, $error); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Claude chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token_claude, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial Claude chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_openrouter_model($model)) { if (!isset($aimogen_Main_Settings['app_id_openrouter']) || trim($aimogen_Main_Settings['app_id_openrouter']) == '') { aimogen_log_to_file('You need to enter an OpenRouter API key in the plugin settings for this feature to work!'); return false; } $appids_openrouter = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_openrouter'])); $appids_openrouter = array_filter($appids_openrouter); $token_openrouter = $appids_openrouter[array_rand($appids_openrouter)]; if($retry_count > 0) { $token_openrouter = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_openrouter']), $token); } $result = aimogen_generate_text_openrouter($token_openrouter, $model, $aicontent, $temperature, $top_p, false, $available_tokens, $stream, $error); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') OpenRouter API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token_openrouter, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial OpenRouter API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_perplexity_model($model)) { if (!isset($aimogen_Main_Settings['app_id_perplexity']) || trim($aimogen_Main_Settings['app_id_perplexity']) == '') { aimogen_log_to_file('You need to enter an PerplexityAI API key in the plugin settings for this feature to work!'); return false; } $appids_perplexity = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_perplexity'])); $appids_perplexity = array_filter($appids_perplexity); $token_perplexity = $appids_perplexity[array_rand($appids_perplexity)]; if($retry_count > 0) { $token_perplexity = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); } $result = aimogen_generate_text_perplexity($token_perplexity, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, $available_tokens, $stream, $retry_count, $query, false, '', $user_question, $env, $is_chat, $error, ''); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') PerplexityAI API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token_perplexity, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial PerplexityAI API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_groq_model($model)) { if (!isset($aimogen_Main_Settings['app_id_groq']) || trim($aimogen_Main_Settings['app_id_groq']) == '') { aimogen_log_to_file('You need to enter an Groq API key in the plugin settings for this feature to work!'); return false; } $appids_groq = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_groq'])); $appids_groq = array_filter($appids_groq); $token_groq = $appids_groq[array_rand($appids_groq)]; if($retry_count > 0) { $token_groq = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_groq']), $token); } $result = aimogen_generate_text_groq($token_groq, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, $available_tokens, $stream, $retry_count, $query, false, '', $user_question, $env, $is_chat, $error, ''); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Groq API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token_groq, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial Groq API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_nvidia_model($model)) { if (!isset($aimogen_Main_Settings['app_id_nvidia']) || trim($aimogen_Main_Settings['app_id_nvidia']) == '') { aimogen_log_to_file('You need to enter an Nvidia API key in the plugin settings for this feature to work!'); return false; } $appids_nvidia = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_nvidia'])); $appids_nvidia = array_filter($appids_nvidia); $token_nvidia = $appids_nvidia[array_rand($appids_nvidia)]; if($retry_count > 0) { $token_nvidia = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_nvidia']), $token); } $result = aimogen_generate_text_nvidia($token_nvidia, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, $available_tokens, $stream, $retry_count, $query, false, '', $user_question, $env, $is_chat, $error, ''); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Nvidia API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token_nvidia, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial Nvidia API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_xai_model($model)) { if (!isset($aimogen_Main_Settings['app_id_xai']) || trim($aimogen_Main_Settings['app_id_xai']) == '') { aimogen_log_to_file('You need to enter an xAI API key in the plugin settings for this feature to work!'); return false; } $appids_xai = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_xai'])); $appids_xai = array_filter($appids_xai); $token_xai = $appids_xai[array_rand($appids_xai)]; if($retry_count > 0) { $token_xai = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_xai']), $token); } $result = aimogen_generate_text_xai($token_xai, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, $available_tokens, $stream, $retry_count, $query, false, '', $user_question, $env, $is_chat, $error, ''); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') xAI API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token_xai, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial xAI API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_google_model($model)) { if(in_array($model, AIMOGEN_GOOGLE_MODELS) === false) { $error = 'This model is not currently supported by Google API: ' . $model; return false; } if (!isset($aimogen_Main_Settings['app_id_google']) || trim($aimogen_Main_Settings['app_id_google']) == '') { aimogen_log_to_file('You need to enter an Google Vertex API key in the plugin settings for this feature to work!'); return false; } $appids_google = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_google'])); $appids_google = array_filter($appids_google); $token_google = $appids_google[array_rand($appids_google)]; if($retry_count > 0) { $token_google = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_google']), $token); } $result = aimogen_generate_text_google_completion($token_google, $model, $aicontent, $stream, $error); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Google chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token_google, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial Google chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_check_if_azure($aimogen_Main_Settings)) { if (!isset($aimogen_Main_Settings['azure_endpoint']) || trim($aimogen_Main_Settings['azure_endpoint']) == '') { $error = 'You need to enter an Azure Endpoint for this to work!'; return false; } if(in_array($model, AIMOGEN_AZURE_MODELS) === false) { $error = 'This model is not currently supported by Azure API: ' . $model; return false; } if(aimogen_is_trained_model($model)) { $error = 'Fine-tuned models are not supported for Azure API'; return false; } $localAzureDeployments = array(); if($retry_count > 0) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); } $depl_arr = aimogen_get_deployments($token); if(is_array($depl_arr)) { foreach($depl_arr as $dar) { if(empty($dar)) { continue; } if(is_string($dar)) { $localAzureDeployments[trim($dar)] = trim($dar); } else { $localAzureDeployments[trim($dar->model)] = trim($dar->id); } } } $azureDeployment = ''; foreach ( $localAzureDeployments as $dmodel => $dname ) { if ( $dmodel === str_replace('.', '', $model) || $dmodel === $model ) { $azureDeployment = $dname; break; } } if ( $azureDeployment == '' ) { $new_dep = aimogen_update_deployments_azure($token); if($new_dep !== false) { $localAzureDeployments = array(); foreach($new_dep as $dar) { if(empty($dar)) { continue; } if(is_string($dar)) { $localAzureDeployments[trim($dar)] = trim($dar); } else { $localAzureDeployments[trim($dar->model)] = trim($dar->id); } } foreach ( $localAzureDeployments as $dmodel => $dname ) { if ( $dmodel === str_replace('.', '', $model) || $dmodel === $model ) { $azureDeployment = $dname; break; } } } if ( $azureDeployment == '' ) { $error = 'No added Azure deployment found for completion model: ' . $model . ' - you need to add this model in your Azure Portal as a Deployment'; return false; } } if (isset($aimogen_Main_Settings['azure_api_selector']) && $aimogen_Main_Settings['azure_api_selector'] != '' && $aimogen_Main_Settings['azure_api_selector'] != 'default') { $api_ver = '?api-version=' . $aimogen_Main_Settings['azure_api_selector']; } else { $api_ver = AIMOGEN_AZURE_API_VERSION; } $apiurl = trailingslashit(trim($aimogen_Main_Settings['azure_endpoint'])) . 'openai/deployments/' . $azureDeployment . '/completions' . $api_ver; $base_params = [ 'model' => str_replace('.', '', $model), 'prompt' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty ]; if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { $base_params['max_tokens'] = $available_tokens; } } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( $apiurl, array( 'headers' => array( 'Content-Type' => 'application/json', 'api-key' => $token ), 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if($stream === false && $result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to decode initial API response: ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your OpenAI quota limit. To fix this, if you are using a free OpenAI account, you need to add a VISA card to your account, as OpenAI heavily limits free accounts. Please check details here: https://platform.openai.com/docs/guides/rate-limits'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the Azure completions API, result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: An error occurred when initially calling OpenAI API: ' . json_encode($result); return false; } } } if(!$stream) { if(!isset($result->choices[0]->text)) { delete_option('aimogen_deployments_list'); if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial API result: ' . json_encode($result); return false; } } } else { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->text, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { $finish_reason = $result->choices[0]->finish_details->type; } if($is_chat == true) { $chat_max_characters = 16000; $max_continue_characters = 4000; if($finish_reason == 'stop') { if (empty($result->choices[0]->text) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { return $result->choices[0]->text; } } else { $return_text = $result->choices[0]->text; $aicontent .= $return_text; $complet_retry_count = 0; while($finish_reason != 'stop' && strlen($return_text) < $chat_max_characters) { if (isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $complet_retry_count) { break; } $complet_retry_count++; if(strlen($aicontent) > $max_continue_characters) { $aicontent = aimogen_substr($aicontent, 0, (0 - $max_continue_characters)); } $aicontent = trim($aicontent); if(empty($aicontent)) { break; } $query_token_count = count(aimogen_encode($aicontent)); $available_tokens = $max_tokens - $query_token_count; if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($aicontent); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent = aimogen_substr($aicontent, 0, $string_len); $aicontent = trim($aicontent); if(empty($aicontent)) { break; } $query_token_count = count(aimogen_encode($aicontent)); $available_tokens = $max_tokens - $query_token_count; } $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { aimogen_log_to_file('Rate limited: ' . $ok); break; } $aierror = ''; $generated_text = aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, 0, $finish_reason, $aierror, true, true, $stream, $user_question, $embedding_namespace, $vision_file); if($generated_text === false) { aimogen_log_to_file('Chat response completion error: ' . $aierror); break; } else { $return_text .= $generated_text; $aicontent .= $generated_text; } } return $return_text; } } else { return $result->choices[0]->text; } } } else { return $result; } } } else { $base_params = [ 'model' => $model, 'prompt' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty ]; if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { $base_params['max_tokens'] = $available_tokens; } } if (isset($aimogen_Main_Settings['ai_seed']) && $aimogen_Main_Settings['ai_seed'] != '') { $base_params['seed'] = intval($aimogen_Main_Settings['ai_seed']); } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $xh = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ); if (isset($aimogen_Main_Settings['openai_organization']) && $aimogen_Main_Settings['openai_organization'] != '') { $xh['OpenAI-Organization'] = $aimogen_Main_Settings['openai_organization']; } if (isset($aimogen_Main_Settings['api_selector']) && trim($aimogen_Main_Settings['api_selector']) == 'custom' && isset($aimogen_Main_Settings['custom_endpoint']) && trim($aimogen_Main_Settings['custom_endpoint'], ' /') != '') { $api_endpoint = trim($aimogen_Main_Settings['custom_endpoint'], ' /'); } else { $api_endpoint = 'https://api.openai.com'; } $api_call = wp_remote_post( $api_endpoint . '/v1/completions', array( 'headers' => $xh, 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to get initial API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: Failed to decode initial API response: ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your OpenAI quota limit for this API key. To fix this, if you are using a free OpenAI account, you need to add a VISA card to your account, as OpenAI heavily limits free accounts. Please check details here: https://platform.openai.com/docs/guides/rate-limits'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the completions API, result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { $error = 'Error: An error occurred when initially calling OpenAI API: ' . json_encode($result); return false; } } } if(!$stream) { if(!isset($result->choices[0]->text)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial API result: ' . json_encode($result); return false; } } } else { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->text, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { $finish_reason = $result->choices[0]->finish_details->type; } if($is_chat == true) { $chat_max_characters = 16000; $max_continue_characters = 4000; if($finish_reason == 'stop') { if (empty($result->choices[0]->text) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $stream, $user_question, $embedding_namespace, $vision_file); } else { return $result->choices[0]->text; } } else { $return_text = $result->choices[0]->text; $aicontent .= $return_text; $complet_retry_count = 0; while($finish_reason != 'stop' && strlen($return_text) < $chat_max_characters) { if (isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $complet_retry_count) { break; } $complet_retry_count++; if(strlen($aicontent) > $max_continue_characters) { $aicontent = aimogen_substr($aicontent, 0, (0 - $max_continue_characters)); } $aicontent = trim($aicontent); if(empty($aicontent)) { break; } $query_token_count = count(aimogen_encode($aicontent)); $available_tokens = $max_tokens - $query_token_count; if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($aicontent); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent = aimogen_substr($aicontent, 0, $string_len); $aicontent = trim($aicontent); if(empty($aicontent)) { break; } $query_token_count = count(aimogen_encode($aicontent)); $available_tokens = $max_tokens - $query_token_count; } $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { aimogen_log_to_file('Rate limited: ' . $ok); break; } $aierror = ''; $generated_text = aimogen_generate_text_completion($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, 0, $finish_reason, $aierror, true, true, $stream, $user_question, $embedding_namespace, $vision_file); if($generated_text === false) { aimogen_log_to_file('Chat response completion error: ' . $aierror); break; } else { $return_text .= $generated_text; $aicontent .= $generated_text; } } return $return_text; } } else { return $result->choices[0]->text; } } } else { return $result; } } } $error = 'Failed to finish API call correctly.'; return false; } function aimogen_generate_response_api($token, $model, $input, $available_tokens = null, $temperature = 1.0, $top_p = 1.0, $stream = false, $instructions = null, $max_output_tokens = null, $store_data = false, $functions = false, $query = null, $retry_count = 0, $enabled_mcp_servers = '', &$error = '') { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if(is_array($input)) { foreach($input as $inputvar => $za_val) { if(isset($input[$inputvar]['content']) && is_array($input[$inputvar]['content'])) { foreach($input[$inputvar]['content'] as $cntind => $cnt_val) { if(isset($input[$inputvar]['content'][$cntind]['type'])) { if($input[$inputvar]['content'][$cntind]['type'] == 'text') { $input[$inputvar]['content'][$cntind]['type'] = 'input_text'; } elseif($input[$inputvar]['content'][$cntind]['type'] == 'image_url') { $input[$inputvar]['content'][$cntind]['type'] = 'input_image'; if(isset($input[$inputvar]['content'][$cntind]['image_url']) && is_array($input[$inputvar]['content'][$cntind]['image_url'])&& isset($input[$inputvar]['content'][$cntind]['image_url']['url']) ) { $input[$inputvar]['content'][$cntind]['image_url'] = $input[$inputvar]['content'][$cntind]['image_url']['url']; } } } } } } } foreach($input as $inputvar => $za_val) { if(!isset($input[$inputvar]['role'])) { unset($input[$inputvar]); continue; } if(isset($input[$inputvar]['run_id'])) { unset($input[$inputvar]['run_id']); } if(isset($input[$inputvar]['thread_id'])) { unset($input[$inputvar]['thread_id']); } } foreach($input as $inputvar => $za_val) { if(isset($input[$inputvar]['role']) && $input[$inputvar]['role'] == 'tool') { $input[$inputvar]['role'] = 'developer'; if(isset($input[$inputvar]['tool_call_id'])) { unset($input[$inputvar]['tool_call_id']); } } } $input = array_values($input); $base_params = [ 'model' => $model, 'input' => $input, 'temperature' => floatval($temperature), 'top_p' => floatval($top_p), 'truncation' => 'auto', 'parallel_tool_calls' => false ]; if ($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!empty($instructions)) { $base_params['instructions'] = $instructions; } if ($max_output_tokens !== null && (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on')) { $base_params['max_output_tokens'] = intval($max_output_tokens); } if ((isset($aimogen_Main_Settings['store_data']) && $aimogen_Main_Settings['store_data'] == 'on') || ($store_data === true || $store_data === 'on' || $store_data === '1')) { if (isset($aimogen_Main_Settings['api_selector']) && trim($aimogen_Main_Settings['api_selector']) == 'custom' && isset($aimogen_Main_Settings['custom_endpoint']) && trim($aimogen_Main_Settings['custom_endpoint'], ' /') != '') {//don't store results for custom endpoints } else { $base_params['store'] = true; } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { //currently tool choice is not supported in o1 models if(!aimogen_is_o1_model($model)) { $converted_functions = array(); foreach($functions['functions'] as $indx => $funcx) { if(!isset($functions['functions'][$indx]['name']) && isset($functions['functions'][$indx]['function']->name)) { $functions['functions'][$indx]['name'] = $functions['functions'][$indx]['function']->name; } $function_obj = $funcx['function']; $add_func = new stdClass(); $add_func->type = 'function'; $add_func->name = $funcx['name'] ?? $function_obj->name; $add_func->description = $function_obj->description; $add_func->parameters = new stdClass(); $add_func->parameters->type = 'object'; $add_func->parameters->properties = new stdClass(); $required = []; if (!empty($function_obj->parameters)) { foreach ($function_obj->parameters as $param) { $param_name = $param->name; $param_desc = $param->description; $param_type = $param->type; $add_func->parameters->properties->$param_name = [ 'type' => $param_type, 'description' => $param_desc, ]; if (!empty($param->required)) { $required[] = $param_name; } } } if (!empty($required)) { $add_func->parameters->required = $required; } $converted_functions[] = $add_func; } $base_params['tools'] = $converted_functions; $base_params['tool_choice'] = 'auto'; } } if(!aimogen_is_o1_model($model)) { $mcp_servers = get_option('aimogen_mcp_servers', array()); if (!empty($mcp_servers)) { $enabled_ids = array(); if (!empty($enabled_mcp_servers)) { $enabled_ids = array_map('trim', explode(',', $enabled_mcp_servers)); } foreach ($mcp_servers as $id => $server) { if ($server['global'] != 'on' && !in_array($id, $enabled_ids)) { continue; } $server_label = $server['name'] . '_' . $id; $server_label = preg_replace('/[^a-zA-Z0-9_]/', '', $server_label); $server_label = preg_replace('/_{2,}/', '_', $server_label); if (!preg_match('/^[a-zA-Z]/', $server_label)) { $server_label = 'mcp_' . $server_label; } $mcp_tool = array( 'type' => 'mcp', 'server_label' => $server_label, 'server_description' => $server['description'], 'server_url' => $server['url'], 'require_approval' => 'never' ); if (!empty($server['token'])) { $mcp_tool['headers'] = array( 'Authorization' => 'Bearer ' . $server['token'], ); } if (!isset($base_params['tools'])) { $base_params['tools'] = array(); } $base_params['tools'][] = $mcp_tool; } if (!isset($base_params['tool_choice'])) { $base_params['tool_choice'] = 'auto'; } } } if(aimogen_is_deep_research_model($model)) { if(!isset($base_params['tools']) || !is_array($base_params['tools']) || empty($base_params['tools'])) { $base_params['tools'][] = array('type' => 'web_search'); } } try { $send_json = aimogen_safe_json_encode($base_params); } catch (Exception $e) { $error = 'Error: Exception in API payload encoding: ' . $e->getMessage(); return false; } if ($send_json === false) { $error = 'Error: Failed to encode API payload: ' . json_encode($base_params); return false; } $xh = [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ]; if (isset($aimogen_Main_Settings['openai_organization']) && !empty($aimogen_Main_Settings['openai_organization'])) { $xh['OpenAI-Organization'] = $aimogen_Main_Settings['openai_organization']; } if ($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( 'https://api.openai.com/v1/responses', array( 'headers' => $xh, 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if ($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if (is_wp_error($api_call)) { if (isset($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); return aimogen_generate_response_api($token, $model, $input, $available_tokens, $temperature, $top_p, $stream, $instructions, $max_output_tokens, $store_data, $functions, $query, intval($retry_count) + 1, $enabled_mcp_servers, $error); } else { $error = 'Error: Failed to get initial API response: ' . json_encode($api_call); return false; } } if (!$stream) { $result = json_decode($api_call['body']); if ($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); return aimogen_generate_response_api($token, $model, $input, $available_tokens, $temperature, $top_p, $stream, $instructions, $max_output_tokens, $store_data, $functions, $query, intval($retry_count) + 1, $enabled_mcp_servers, $error); } else { $error = 'Error: Failed to decode initial API response: ' . json_encode($api_call); return false; } } if (isset($result->error)) { $error_data = $result->error; if ($error_data->type == 'insufficient_quota') { $error = 'Error: You exceeded your OpenAI quota limit for this API key. Please check details here: https://platform.openai.com/docs/guides/rate-limits'; return false; } elseif ($error_data->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the responses API: ' . json_encode($error_data); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); if (isset($error_data->code) && $error_data->code == 'rate_limit_exceeded') { preg_match('#Please try again in ([\d.]*?)s#i', $error_data->message, $match); $sleep_time = isset($match[1]) ? ceil(floatval($match[1])) : pow(2, $retry_count); aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); sleep($sleep_time); } else { sleep(pow(2, $retry_count)); } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after error: ' . json_encode($api_call['body'])); return aimogen_generate_response_api($token, $model, $input, $available_tokens, $temperature, $top_p, $stream, $instructions, $max_output_tokens, $store_data, $functions, $query, intval($retry_count) + 1, $enabled_mcp_servers, $error); } else { $error = 'Error: An error occurred when calling OpenAI Responses API: ' . json_encode($error_data); return false; } } } $current_cont = 0; if(isset($result->output[$current_cont]->type) && $result->output[$current_cont]->type == 'reasoning') { $current_cont++; } if (!isset($result->output[$current_cont]->content[0]->text) && !isset($result->output[$current_cont]->type)) { if (isset($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after output failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); return aimogen_generate_response_api($token, $model, $input, $available_tokens, $temperature, $top_p, $stream, $instructions, $max_output_tokens, $store_data, $functions, $query, intval($retry_count) + 1, $enabled_mcp_servers, $error); } else { $error = 'Error: Output text not found in API result: ' . json_encode($result); return false; } } $result = apply_filters('aimogen_ai_responses_reply_raw', $result, $input); return $result; } else { $result = ''; return $result; } } function aimogen_generate_text_assistant($token, $assistant_id, $local_assistant_id, $role, $content, &$thread_id, $no_internet = false, $no_embeddings = false, $env = '', $retry_count = 0, $embedding_namespace = '', $stream = false, $function_result = '', $vision_file = '', $file_data = '') { $error = ''; $aicontent = ''; try { if(empty($content)) { throw new Exception('Empty array submitted to AI'); } require_once (dirname(__FILE__) . "/res/aimogen-assistants-api.php"); try { if (!empty($assistant_id)) { $assistant = aimogen_openai_retrieve_assistant($token, $assistant_id); if(!isset($assistant['id'])) { throw new Exception('Incorrect response from assistant grabbing assistant ID ' . $assistant_id . ': ' . $thread_id); } else { $model = $assistant['model']; } } } catch(Exception $e) { throw new Exception('Failed to query assistant: ' . $e->getMessage()); } $count_vision = false; if(!aimogen_is_vision_model('', $assistant_id)) { $vision_file = ''; } else { $count_vision = true; } if(empty($function_result) || $function_result == 'disabled') { $embeddings_enabled = false; $internet_enabled = false; $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $particular_embedding_namespace = ''; aimogen_get_internet_embeddings_result($aimogen_Main_Settings, $env, $embeddings_enabled, $internet_enabled, $particular_embedding_namespace); if(empty($embedding_namespace) && !empty($particular_embedding_namespace)) { $embedding_namespace = $particular_embedding_namespace; } $last_message = false; try { if (empty($thread_id)) { $assistant_first_message = ''; if(!empty($local_assistant_id)) { $assistant_first_message = get_post_meta($local_assistant_id, '_assistant_first_message', true); } if(!empty($assistant_first_message)) { $simulate_conv = array(); $simulate_conv[] = array("role" => 'assistant', "content" => $assistant_first_message); } else { $simulate_conv = []; } $thread = aimogen_openai_create_thread($token, $simulate_conv, $file_data); if(!isset($thread['id'])) { throw new Exception('Invalid thread format: ' . json_encode($thread)); } $thread_id = $thread['id']; } } catch(Exception $e) { throw new Exception('Failed to create thread: ' . $e->getMessage()); } $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'text'; $maxResults = 1; $temperature = 1; $max_tokens = aimogen_get_max_tokens($model); if(aimogen_check_if_available_token_recalc_needed($model, $aimogen_Main_Settings)) { $prompt_tokens = count(aimogen_encode($content)); $available_tokens = $max_tokens - $prompt_tokens; } else { $available_tokens = $max_tokens; } $query = new Aimogen_lite_Query($content, $available_tokens, $model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, '', $assistant_id); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { throw new Exception($ok); } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating chat AI text using assistant: ' . $assistant_id . ' and prompt: ' . $content); } $delay = ''; if (isset($aimogen_Main_Settings['request_delay']) && $aimogen_Main_Settings['request_delay'] != '') { if(stristr($aimogen_Main_Settings['request_delay'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay'])); } } } if($delay != '' && is_numeric($delay)) { usleep(intval($delay) * 1000); } try { $message = aimogen_openai_create_message($token, $thread_id, $role, $content, null, $vision_file); if(!isset($message['id'])) { throw new Exception('Invalid message format: ' . json_encode($message)); } } catch(Exception $e) { throw new Exception('Failed to create message: ' . $e->getMessage()); } if($stream === false) { try { $run = aimogen_openai_create_run($token, $thread_id, $assistant_id); if(!isset($run['id'])) { throw new Exception('Invalid run format: ' . json_encode($run)); } $run_id = $run['id']; } catch(Exception $e) { throw new Exception('Failed to run thread: ' . $e->getMessage()); } try { if (isset($aimogen_Main_Settings['max_timeout']) && $aimogen_Main_Settings['max_timeout'] != '') { $timeout = intval($aimogen_Main_Settings['max_timeout']); } else { $timeout = 300; } $start_time = time(); do { $run_status = aimogen_openai_retrieve_run($token, $thread_id, $run_id); if(!isset($run_status['id'])) { throw new Exception('Invalid run_status format: ' . json_encode($run_status)); } if ($run_status['status'] === 'requires_action' && $run_status['required_action']['type'] === 'submit_tool_outputs') { $last_message_tool = ''; try { $tool_outputs = aimogen_call_required_tool($run_status['required_action']); } catch(Exception $e) { throw new Exception('Failed to call required tool: ' . $e->getMessage()); } foreach($tool_outputs as $jinx => $to) { if(isset($to['echo'])) { $last_message_tool .= $to['echo'] . ' '; unset($to['echo']); } } $last_message_tool = trim($last_message_tool); if(!empty($last_message_tool)) { $last_message_tool_ret = array(); $last_message_tool_ret['content'][0]['text']['value'] = $last_message_tool; return $last_message_tool_ret; } $run_status = aimogen_openai_submit_tool_outputs_to_run($token, $thread_id, $run_id, $tool_outputs); } if($run_status['status'] !== 'completed' && $run_status['status'] !== 'failed') { $now_time = time(); if($start_time + $timeout < $now_time) { throw new Exception('Timeout in retrive run polling (s): ' . $timeout); } sleep(1); } } while ($run_status['status'] !== 'completed' && $run_status['status'] !== 'failed'); } catch(Exception $e) { throw new Exception('Failed to poll thread: ' . $e->getMessage()); } if ($run_status['status'] === 'completed') { try { $messages = aimogen_openai_list_messages($token, $thread_id); if(!isset($messages['data'][0]['id'])) { throw new Exception('Invalid messages format: ' . json_encode($messages)); } } catch(Exception $e) { throw new Exception('Failed to get message: ' . $e->getMessage()); } $last_message = reset($messages['data']); $last_message = apply_filters( 'aimogen_ai_reply_raw', $last_message, $content ); apply_filters( 'aimogen_ai_reply', $last_message, $query ); return $last_message; } else { throw new Exception('Run failed or did not complete. ' . json_encode($run_status)); } } else { try { aimogen_openai_create_stream_run($token, $thread_id, $assistant_id); $last_message = ''; } catch(Exception $e) { throw new Exception('Failed to run thread: ' . $e->getMessage()); } } } else { try { $run_id = ''; $tool_outputs = array(); if(is_array($function_result)) { foreach($function_result as $fr) { if(isset($fr['run_id'])) { $run_id = $fr['run_id']; } if(isset($fr['thread_id'])) { $thread_id = $fr['thread_id']; } $tool_outputs[] = [ 'tool_call_id' => $fr['tool_call_id'], 'output' => $fr['content'] ]; } } if(empty($tool_outputs)) { throw new Exception('Failed to get results: ' . json_encode($function_result)); } if(empty($run_id)) { throw new Exception('Failed to get run_id: ' . json_encode($function_result)); } if(empty($thread_id)) { throw new Exception('Failed to get thread_id: ' . json_encode($function_result)); } aimogen_openai_submit_tool_outputs_to_stream_run($token, $thread_id, $run_id, $tool_outputs); $last_message = ''; } catch(Exception $e) { throw new Exception('Failed to run thread: ' . $e->getMessage()); } } } catch(Exception $e) { $is_error = true; if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') assistants API call after failure: ' . $e->getMessage()); sleep(pow(2, $retry_count)); try { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); $assistant_id = apply_filters('aimogen_assistant_id_fallback', $assistant_id, $error, $aicontent); $local_assistant_id = apply_filters('aimogen_local_assistant_id_id_fallback', $local_assistant_id, $error, $aicontent); $content = apply_filters('aimogen_content_try_fix', $content, $error, $model); $thread_id = apply_filters('aimogen_thread_id_try_fix', $thread_id, $error, $model); $last_message = aimogen_generate_text_assistant($token, $assistant_id, $local_assistant_id, $role, $content, $thread_id, $no_internet, $no_embeddings, $env, intval($retry_count) + 1, $embedding_namespace, $stream, $function_result, $vision_file, $file_data); $is_error = false; } catch(Exception $e) { aimogen_log_to_file('Failed to generate text using Assistants API for retry (' . $retry_count . ': ' . $e->getMessage()); } } if($is_error === true) { throw $e; } } return $last_message; } function aimogen_normalise_context( array &$messages, string $model, int $reserve = 256 ) { $max_ctx = aimogen_get_max_tokens( $model ); $encoder = 'aimogen_encode'; $prompt_tokens = 0; foreach ( $messages as $m ) { $prompt_tokens += count( $encoder( $m['role'] ?? '' ) ); if ( isset( $m['content'] ) && is_array( $m['content'] ) ) { foreach ( $m['content'] as $part ) { $prompt_tokens += count( $encoder( $part['text'] ?? '' ) ); } } else { $prompt_tokens += count( $encoder( $m['content'] ?? '' ) ); } } if ( $prompt_tokens + $reserve <= $max_ctx ) { return $max_ctx - $prompt_tokens; } for ( $i = 0; $i < count( $messages ); $i++ ) { if ( $messages[$i]['role'] === 'system' ) { $messages[$i]['content'] = aimogen_strip_to_token_count( $messages[$i]['content'], max( 50, $reserve / 2 ) ); } else { unset( $messages[$i] ); } $messages = array_values( $messages ); $prompt_tokens = 0; foreach ( $messages as $m ) { $prompt_tokens += count( $encoder( $m['role'] ?? '' ) ); $prompt_tokens += is_array( $m['content'] ) ? array_sum( array_map( fn($p) => count( $encoder( $p['text'] ?? '' ) ), $m['content'] ) ) : count( $encoder( $m['content'] ?? '' ) ); } if ( $prompt_tokens + $reserve <= $max_ctx ) { return $max_ctx - $prompt_tokens; } } $last = count( $messages ) - 1; $messages[$last]['content'] = aimogen_strip_to_token_count( $messages[$last]['content'], $max_ctx - $reserve - 10 ); return $reserve; } function aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, &$finish_reason, &$error, $no_internet = false, $no_embeddings = false, $functions = false, $stream = false, $vision_file = '', $dont_add_vision = false, $user_question = '', $embedding_namespace = '', $function_result = '', $dump_result = false, $store_data = false, $enabled_mcp_servers = '') { if(!is_array($aicontent)) { $error = 'Only arrays are supported for chat text: ' . $aicontent; return false; } if(empty($aicontent)) { $error = 'Empty array submitted to AI chat'; return false; } //remove this after the o1 models will not be any more in beta: https://platform.openai.com/docs/guides/reasoning/beta-limitations if(aimogen_is_o1_model($model)) { foreach($aicontent as $zind => $checkme) { if($aicontent[$zind]['role'] == 'system') { $aicontent[$zind]['role'] = 'user'; } } $temperature = 1; $top_p = 1; $presence_penalty = 0; $frequency_penalty = 0; } if(aimogen_is_gpt5_model($model)) { $temperature = 1; $top_p = 1; $presence_penalty = 0; $frequency_penalty = 0; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $embeddings_enabled = false; $internet_enabled = false; $max_tokens = aimogen_get_max_tokens($model); $particular_embedding_namespace = ''; aimogen_get_internet_embeddings_result($aimogen_Main_Settings, $env, $embeddings_enabled, $internet_enabled, $particular_embedding_namespace); if(empty($embedding_namespace) && !empty($particular_embedding_namespace)) { $embedding_namespace = $particular_embedding_namespace; } $aitext = ''; if(!empty($function_result) && $function_result != 'disabled') { if($is_chat) { remove_filter('aimogen_ai_functions', 'aimogen_add_god_mode', 999); $functions = apply_filters('aimogen_ai_functions', false); } } if(!empty($function_result) && is_array($function_result) && $retry_count == 0) { $already_added = false; foreach($function_result as $find => $fr) { if(isset($fr['assistant_message']) && !empty($fr['assistant_message'])) { $objectJson = json_encode($fr['assistant_message']); $associativeArray = json_decode($objectJson, true); if($associativeArray !== null) { $associativeArray = aimogen_convertIntToStrings($associativeArray); if(is_array($associativeArray)) { if(isset($associativeArray['tool_calls']) && is_array($associativeArray['tool_calls'])) { for($j = 0; $j < count($associativeArray['tool_calls']); $j++) { if(isset($associativeArray['tool_calls'][$j]['function']['arguments'])) { $associativeArray['tool_calls'][$j]['function']['arguments'] = json_encode($associativeArray['tool_calls'][$j]['function']['arguments']); } } } if(!$already_added) { $already_added = true; $aicontent[] = $associativeArray; } } } unset($function_result[$find]['assistant_message']); unset($fr['assistant_message']); } elseif(isset($fr['assistant_message']) && empty($fr['assistant_message'])) { unset($function_result[$find]['assistant_message']); unset($fr['assistant_message']); } $aicontent[] = aimogen_convertIntToStrings($fr); } $available_tokens = aimogen_normalise_context( $aicontent, $model, AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS ); } foreach($aicontent as $aimess) { if(isset($aimess['content'])) { if(!is_array($aimess['content'])) { $aitext .= $aimess['content'] . '\n'; } else { foreach($aimess['content'] as $internalmess) { if($internalmess['type'] == 'text') { $aitext .= $internalmess['text'] . '\n'; } } } } } $aitext = rtrim($aitext); $prompt_modified = false; if($prompt_modified == true) { $aitext = ''; foreach($aicontent as $aimess) { if(isset($aimess['content'])) { if(!is_array($aimess['content'])) { $aitext .= $aimess['content'] . '\n'; } else { foreach($aimess['content'] as $internalmess) { if($internalmess['type'] == 'text') { $aitext .= $internalmess['text'] . '\n'; } } } } } } $aitext = rtrim($aitext); if(aimogen_check_if_available_token_recalc_needed($model, $aimogen_Main_Settings)) { $content_tokens = count(aimogen_encode($aitext)); $total_tokens = $content_tokens + $available_tokens; if($total_tokens >= $max_tokens && !aimogen_is_new_token_window_model($model)) { $available_tokens = $max_tokens - $content_tokens; if($available_tokens < AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($aicontent[0]['content']); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $aicontent[0]['content'] = aimogen_substr($aicontent[0]['content'], 0, $string_len); $aicontent[0]['content'] = trim($aicontent[0]['content']); $aitext = ''; foreach($aicontent as $aimess) { if(isset($aimess['content'])) { if(!is_array($aimess['content'])) { $aitext .= $aimess['content'] . '\n'; } else { foreach($aimess['content'] as $internalmess) { if($internalmess['type'] == 'text') { $aitext .= $internalmess['text'] . '\n'; } } } } } $query_token_count = count(aimogen_encode($aitext)); $available_tokens = $max_tokens - $query_token_count; } } } $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'text'; $maxResults = 1; $query = new Aimogen_lite_Query($aitext, $available_tokens, $model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, '', ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = $ok; return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating chat AI text using model: ' . $model . ' and prompt: ' . $aitext); } $delay = ''; if (isset($aimogen_Main_Settings['request_delay']) && $aimogen_Main_Settings['request_delay'] != '') { if(stristr($aimogen_Main_Settings['request_delay'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay'])); } } } if($delay != '' && is_numeric($delay)) { usleep(intval($delay) * 1000); } if($temperature < 0 || $temperature > 1) { $temperature = 1; } if($top_p < 0 || $top_p > 1) { $top_p = 1; } if($presence_penalty < -2 || $presence_penalty > 2) { $presence_penalty = 0; } if($frequency_penalty < -2 || $frequency_penalty > 2) { $frequency_penalty = 0; } if($temperature == '' || (empty($temperature) && $temperature !== 0)) { $temperature = 1; } if($top_p == '' || (empty($top_p) && $top_p !== 0)) { $top_p = 1; } if($presence_penalty == '' || (empty($presence_penalty) && $presence_penalty !== 0)) { $presence_penalty = 0; } if($frequency_penalty == '' || (empty($frequency_penalty) && $frequency_penalty !== 0)) { $frequency_penalty = 0; } $count_vision = false; if(aimogen_check_if_azure($aimogen_Main_Settings, $model) || aimogen_is_vision_groq_model($model) || aimogen_is_vision_xai_model($model) || (!aimogen_check_if_azure_or_others($aimogen_Main_Settings, $model) && aimogen_is_vision_model($model, '') && !aimogen_is_ollama_model($model))) { if($dont_add_vision == false && $vision_file != '' && $retry_count == 0) { $base64_vision = ''; foreach(array_reverse($aicontent, true) as $ind => $indval) { if($indval['role'] != 'user') { continue; } $xcopy = $aicontent[$ind]['content']; if(!empty($base64_vision)) { $xacontent = [ [ "type" => "text", "text" => $xcopy ], [ "type" => "image_url", "image_url" => [ "url" => "data:image/jpeg;base64," . $base64_vision, "detail" => "low" ] ] ]; } else { $xacontent = [ [ "type" => "text", "text" => $xcopy ], [ "type" => "image_url", "image_url" => [ "url" => $vision_file, "detail" => "low" ] ] ]; } $aicontent[$ind]['content'] = $xacontent; $count_vision = true; break; } } } if(aimogen_is_aimogen_key($token)) { $pargs = array(); $api_url = 'https://aiomaticapi.com/apis/ai/v1/chat/'; $pargs['model'] = trim($model); $pargs['temperature'] = $temperature; $pargs['top_p'] = $top_p; $pargs['presence_penalty'] = $presence_penalty; $pargs['frequency_penalty'] = $frequency_penalty; $pargs['messages'] = $aicontent; if($model == 'gpt-4o-mini-search-preview' || $model == 'gpt-4o-search-preview') { unset($pargs['temperature']); unset($pargs['top_p']); unset($pargs['presence_penalty']); unset($pargs['frequency_penalty']); } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $pargs['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { if(aimogen_is_o1_model($model)) { $pargs['max_completion_tokens'] = $available_tokens; } else { $pargs['max_tokens'] = $available_tokens; } } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { //currently tool choice is not supported in o1 models if(!aimogen_is_o1_model($model)) { $pargs['tools'] = (array) $functions['functions']; $pargs['tool_choice'] = 'auto'; } } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } $api_headers = array( 'Authorization' => 'Bearer ' . trim($token) ); $ai_response = aimogen_get_web_page_api($api_url, $pargs, $api_headers); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get AimogenAPI response!'; return false; } } $ai_json = json_decode($ai_response); if(!$stream) { if($ai_json === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to decode AimogenAPI response: ' . $ai_response; return false; } } } if(isset($ai_json->error)) { if (stristr($ai_json->error, 'Your subscription expired, please renew it.') === false && stristr($ai_json->error, '[RATE LIMITED]') === false && isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after error failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error while processing AI response: ' . $ai_json->error; return false; } } if(!$stream) { if((isset($ai_json->finish_reason) && $ai_json->finish_reason == 'tool_calls') || (isset($ai_json->finish_details->type) && $ai_json->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $ai_json->result->content = $functions['message']; } else { $ai_json->result->content = 'OK'; } } if(!isset($ai_json->result->content)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after parse failure: ' . $api_url); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to parse AimogenAPI response: ' . $ai_response; return false; } } if(isset($ai_json->remainingtokens)) { set_transient('aimogenapi_tokens', $ai_json->remainingtokens, 86400); } $ai_json = apply_filters( 'aimogen_ai_reply_raw', $ai_json, $aicontent ); apply_filters( 'aimogen_ai_reply', $ai_json->result, $query ); return $ai_json->result->content; } else { return ''; } } elseif (aimogen_is_claude_model($model)) { if(in_array($model, AIMOGEN_CLAUDE_MODELS) === false) { $error = 'This model is not currently supported by Claude API: ' . $model; return false; } if (!isset($aimogen_Main_Settings['app_id_claude']) || trim($aimogen_Main_Settings['app_id_claude']) == '') { aimogen_log_to_file('You need to enter an Anthropic Claude API key in the plugin settings for this feature to work!'); return false; } $appids_claude = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_claude'])); $appids_claude = array_filter($appids_claude); $token_claude = $appids_claude[array_rand($appids_claude)]; if($retry_count > 0) { $token_claude = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_claude']), $token); } $result = aimogen_generate_text_local_claude($token_claude, $model, $aicontent, $temperature, $top_p, $vision_file, $available_tokens, $stream, $is_chat, $enabled_mcp_servers, $error); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Claude chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_claude, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial Claude chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif(aimogen_is_huggingface_model($model)) { if(!isset($aimogen_Main_Settings['app_id_huggingface']) || $aimogen_Main_Settings['app_id_huggingface'] == '') { $error = apply_filters('aimogen_modify_ai_error', 'You need to add a HuggingFace API key for this to work.'); return false; } else { $appids_hugging = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_huggingface'])); $appids_hugging = array_filter($appids_hugging); $token_hugging = $appids_hugging[array_rand($appids_hugging)]; $error = ''; $available_tokens = 2000; if($retry_count > 0) { $token_hugging = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_huggingface']), $token); } $response_text = aimogen_generate_text_huggingface($token_hugging, $model, $aicontent, $env, $temperature, $top_p, $available_tokens, $stream, $error); if(empty($response_text)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') HuggingFace chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_hugging, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = apply_filters('aimogen_modify_ai_error', 'Error in HuggingFace: ' . $error); return false; } } else { if($response_text === false || empty($response_text)) { $error = apply_filters('aimogen_modify_ai_error', 'Response empty ' . $error); return false; } return $response_text; } } } elseif(aimogen_is_ollama_model($model)) { if(!isset($aimogen_Main_Settings['ollama_url']) || $aimogen_Main_Settings['ollama_url'] == '') { $error = apply_filters('aimogen_modify_ai_error', 'You need to add an Ollama API URL in plugin settings, for this to work.'); return false; } else { $ollama_url = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['ollama_url'])); $ollama_url = array_filter($ollama_url); $ollama_url = $ollama_url[array_rand($ollama_url)]; $ollama_url = rtrim(trim($ollama_url), '/'); $error = ''; $available_tokens = 4000; $response_text = aimogen_generate_text_ollama($ollama_url, $model, $aicontent, $env, $temperature, $top_p, $functions, $vision_file, $available_tokens, $stream, $error); if(!$stream) { if(empty($response_text)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Ollama chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = apply_filters('aimogen_modify_ai_error', 'Error in Ollama: ' . $error); return false; } } else { if($response_text === false || empty($response_text)) { $error = apply_filters('aimogen_modify_ai_error', 'Response empty ' . $error); return false; } return $response_text; } } else { return $response_text; } } } elseif (aimogen_is_openrouter_model($model)) { if (!isset($aimogen_Main_Settings['app_id_openrouter']) || trim($aimogen_Main_Settings['app_id_openrouter']) == '') { aimogen_log_to_file('You need to enter an OpenRouter API key in the plugin settings for this feature to work!'); return false; } $appids_openrouter = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_openrouter'])); $appids_openrouter = array_filter($appids_openrouter); $token_openrouter = $appids_openrouter[array_rand($appids_openrouter)]; if($retry_count > 0) { $token_openrouter = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_openrouter']), $token); } $result = aimogen_generate_text_openrouter($token_openrouter, $model, $aicontent, $temperature, $top_p, $vision_file, $available_tokens, $stream, $error); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') OpenRouter chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_openrouter, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial OpenRouter chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_perplexity_model($model)) { if (!isset($aimogen_Main_Settings['app_id_perplexity']) || trim($aimogen_Main_Settings['app_id_perplexity']) == '') { aimogen_log_to_file('You need to enter an PerplexityAI API key in the plugin settings for this feature to work!'); return false; } $appids_perplexity = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_perplexity'])); $appids_perplexity = array_filter($appids_perplexity); $token_perplexity = $appids_perplexity[array_rand($appids_perplexity)]; if($retry_count > 0) { $token_perplexity = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); } $result = aimogen_generate_text_perplexity($token_perplexity, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') PerplexityAI chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_perplexity, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial PerplexityAI chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_groq_model($model)) { if (!isset($aimogen_Main_Settings['app_id_groq']) || trim($aimogen_Main_Settings['app_id_groq']) == '') { aimogen_log_to_file('You need to enter an Groq API key in the plugin settings for this feature to work!'); return false; } $appids_groq = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_groq'])); $appids_groq = array_filter($appids_groq); $token_groq = $appids_groq[array_rand($appids_groq)]; if($retry_count > 0) { $token_groq = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_groq']), $token); } $result = aimogen_generate_text_groq($token_groq, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Groq chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_groq, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial Groq chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_nvidia_model($model)) { if (!isset($aimogen_Main_Settings['app_id_nvidia']) || trim($aimogen_Main_Settings['app_id_nvidia']) == '') { aimogen_log_to_file('You need to enter an Nvidia API key in the plugin settings for this feature to work!'); return false; } $appids_nvidia = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_nvidia'])); $appids_nvidia = array_filter($appids_nvidia); $token_nvidia = $appids_nvidia[array_rand($appids_nvidia)]; if($retry_count > 0) { $token_nvidia = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_nvidia']), $token); } $result = aimogen_generate_text_nvidia($token_nvidia, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Nvidia chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_nvidia, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial Nvidia chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_xai_model($model)) { if (!isset($aimogen_Main_Settings['app_id_xai']) || trim($aimogen_Main_Settings['app_id_xai']) == '') { aimogen_log_to_file('You need to enter an xAI API key in the plugin settings for this feature to work!'); return false; } $appids_xai = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_xai'])); $appids_xai = array_filter($appids_xai); $token_xai = $appids_xai[array_rand($appids_xai)]; if($retry_count > 0) { $token_xai = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_xai']), $token); } $result = aimogen_generate_text_xai($token_xai, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') xAI chat API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_xai, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial xAI chat API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_is_google_model($model)) { if(in_array($model, AIMOGEN_GOOGLE_MODELS) === false) { $error = 'This model is not currently supported by Google API: ' . $model; return false; } if (!isset($aimogen_Main_Settings['app_id_google']) || trim($aimogen_Main_Settings['app_id_google']) == '') { aimogen_log_to_file('You need to enter an Google AI Studio API key in the plugin settings for this feature to work!'); return false; } $appids_google = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_google'])); $appids_google = array_filter($appids_google); $token_google = $appids_google[array_rand($appids_google)]; if($retry_count > 0) { $token_google = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_google']), $token); } $result = aimogen_generate_text_google_chat($token_google, $model, $aicontent, $temperature, $top_p, $vision_file, $functions, $function_result, $available_tokens, $stream, $error); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Google API call after initial failure: ' . json_encode($error)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token_google, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial Google API response: ' . json_encode($error); return false; } } else { if($stream === false) { $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result, $query ); $finish_reason = 'stop'; return $result; } else { return $result; } } } elseif (aimogen_check_if_azure($aimogen_Main_Settings)) { if (!isset($aimogen_Main_Settings['azure_endpoint']) || trim($aimogen_Main_Settings['azure_endpoint']) == '') { $error = 'You need to enter an Azure Endpoint for this to work!'; return false; } if(in_array($model, AIMOGEN_AZURE_MODELS) === false) { $error = 'This model is not currently supported by Azure API: ' . $model; return false; } if(aimogen_is_trained_model($model)) { $error = 'Fine-tuned models are not supported for Azure API'; return false; } if($retry_count > 0) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); } $localAzureDeployments = array(); $depl_arr = aimogen_get_deployments($token); if(is_array($depl_arr)) { foreach($depl_arr as $dar) { if(empty($dar)) { continue; } if(is_string($dar)) { $localAzureDeployments[trim($dar)] = trim($dar); } else { $localAzureDeployments[trim($dar->model)] = trim($dar->id); } } } $azureDeployment = ''; foreach ( $localAzureDeployments as $dmodel => $dname ) { if ( $dmodel === str_replace('.', '', $model) || $dmodel === $model ) { $azureDeployment = $dname; break; } } if ( $azureDeployment == '' ) { $new_dep = aimogen_update_deployments_azure($token); if($new_dep !== false) { $localAzureDeployments = array(); foreach($new_dep as $dar) { if(empty($dar)) { continue; } if(is_string($dar)) { $localAzureDeployments[trim($dar)] = trim($dar); } else { $localAzureDeployments[trim($dar->model)] = trim($dar->id); } } foreach ( $localAzureDeployments as $dmodel => $dname ) { if ( $dmodel === str_replace('.', '', $model) || $dmodel === $model ) { $azureDeployment = $dname; break; } } } if ( $azureDeployment == '' ) { $error = 'No added Azure deployment found for chat model: ' . $model . ' - you need to add this model in your Azure Portal as a Deployment'; return false; } } if (isset($aimogen_Main_Settings['azure_api_selector']) && $aimogen_Main_Settings['azure_api_selector'] != '' && $aimogen_Main_Settings['azure_api_selector'] != 'default') { $api_ver = '?api-version=' . $aimogen_Main_Settings['azure_api_selector']; } else { $api_ver = AIMOGEN_AZURE_API_VERSION; } $apiurl = trailingslashit(trim($aimogen_Main_Settings['azure_endpoint'])) . 'openai/deployments/' . $azureDeployment . '/chat/completions' . $api_ver; $base_params = [ 'model' => str_replace('.', '', $model), 'messages' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty ]; if($model == 'gpt-4o-mini-search-preview' || $model == 'gpt-4o-search-preview') { unset($base_params['temperature']); unset($base_params['top_p']); unset($base_params['presence_penalty']); unset($base_params['frequency_penalty']); } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { if(aimogen_is_o1_model($model)) { $base_params['max_completion_tokens'] = $available_tokens; } else { $base_params['max_tokens'] = $available_tokens; } } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { //currently tool choice is not supported in o1 models if(!aimogen_is_o1_model($model)) { $base_params['tools'] = $functions['functions']; $base_params['tool_choice'] = 'auto'; } } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode chat API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( $apiurl, array( 'headers' => array( 'Content-Type' => 'application/json', 'api-key' => $token ), 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Azure chat API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if(!aimogen_check_if_azure($aimogen_Main_Settings)) { if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after decode failure(1): ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to decode initial chat API response(1): ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your Azure OpenAI quota limit, please wait a period for the quota to refill (chat initial call).'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the Azure chat API, result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Azure chat API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: An error occurred when initially calling OpenAI chat API: ' . json_encode($result); return false; } } } } if(!$stream) { if((isset($result->choices[0]->finish_reason) && $result->choices[0]->finish_reason == 'tool_calls') || (isset($result->choices[0]->finish_details->type) && $result->choices[0]->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $result->choices[0]->message->content = $functions['message']; } else { $result->choices[0]->message->content = 'OK'; } } if(!isset($result->choices[0]->message->content)) { delete_option('aimogen_deployments_list'); if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial Azure chat API result: ' . json_encode($result); return false; } } } else { if(isset($result->choices[0]->message->tool_calls)) { $result->tool_calls = $result->choices[0]->message->tool_calls; } $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->message->content, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { $finish_reason = $result->choices[0]->finish_details->type; } if($is_chat == true) { if (empty($result->choices[0]->message->content) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(1) ' . json_encode($result); return false; } else { if($dump_result === true) { return $result; } return $result->choices[0]->message->content; } } } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(2) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } } else { return $result; } } } else { //using the new Responses API o1-pro supported only in responses if(( aimogen_is_o1_pro_model($model) || (isset($aimogen_Main_Settings['responses_api']) && $aimogen_Main_Settings['responses_api'] == 'on')) && (!isset($aimogen_Main_Settings['api_selector']) || trim($aimogen_Main_Settings['api_selector']) == 'openai') ) { $instructions = null; $result = aimogen_generate_response_api($token, $model, $aicontent, null, $temperature, $top_p, $stream, $instructions, null, $store_data, $functions, $query, 0, $enabled_mcp_servers, $error); if($result === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') responses API call'); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Responses API failed: ' . $error; return false; } } if(!$stream) { if(isset($result->output) && is_array($result->output)) { foreach($result->output as $rkey => $rvalue) { if((isset($result->output[$rkey]->finish_reason) && $result->output[$rkey]->finish_reason == 'tool_calls') || (isset($result->output[$rkey]->finish_details->type) && $result->output[$rkey]->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $result->output[$rkey]->content[0]->text = $functions['message']; } else { $result->output[$rkey]->content[0]->text = 'OK'; } } } $current_cont = 0; while(isset($result->output[$current_cont]->type) && $result->output[$current_cont]->type != 'message') { $current_cont++; } if($current_cont > count($result->output) && !isset($result->output[$current_cont])) { $error = 'No textual response returned by the API.'; return false; } if(!isset($result->output[$current_cont]->content[0]->text) && !isset($result->aimogen_tool_direct_message) && !isset($result->aimogen_tool_end_conversation) && !isset($result->aimogen_tool_results)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') responses API call after output failure'); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: output not found in initial OpenAI responses API result: ' . json_encode($result); return false; } } } else { if(isset($result->output[$current_cont]->content[0]->tool_calls)) { $result->tool_calls = $result->output[$current_cont]->content[0]->tool_calls; } $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); if(isset($result->output[$current_cont]->content[0]->text)) { apply_filters( 'aimogen_ai_reply', $result->output[$current_cont]->content[0]->text, $query ); } $finish_reason = 'completed'; if($is_chat == true) { if($dump_result === true) { return $result; } if (empty($result->output[$current_cont]->content[0]->text) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') responses API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { if (!isset($result->aimogen_tool_direct_message) && !isset($result->aimogen_tool_end_conversation) && !isset($result->aimogen_tool_results) && empty($result->output[$current_cont]->content[0]->text)) { $error = 'Error: No content found in the response(3) ' . json_encode($result); return false; } else { if(isset($result->aimogen_tool_direct_message) && empty($function_result)) { $my_message = ''; foreach($result->aimogen_tool_direct_message as $dm) { $my_message .= $dm['content'] . ' '; } $my_message = trim($my_message); return $my_message; } if(isset($result->aimogen_tool_end_conversation) && empty($function_result)) { return 'AimogenConversationEnded~!@#$%^&*()'; } if(isset($result->aimogen_tool_results) && empty($function_result)) { //recalling with function result return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, true, true, $functions, $stream, $vision_file, $dont_add_vision, $user_question, $embedding_namespace, $result->aimogen_tool_results, $dump_result, $store_data, $enabled_mcp_servers); } return $result->output[$current_cont]->content[0]->text; } } } else { if (empty($result->output[$current_cont]->content[0]->text)) { $error = 'Error: No content found in the response(4) ' . json_encode($result); return false; } else { return $result->output[$current_cont]->content[0]->text; } } } } else { $error = 'Error: Unexpected result format ' . json_encode($result); return false; } } else { return $result; } } $base_params = [ 'model' => $model, 'messages' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty, ]; if($model == 'gpt-4o-mini-search-preview' || $model == 'gpt-4o-search-preview') { unset($base_params['temperature']); unset($base_params['top_p']); unset($base_params['presence_penalty']); unset($base_params['frequency_penalty']); } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if (isset($aimogen_Main_Settings['api_selector']) && trim($aimogen_Main_Settings['api_selector']) == 'custom' && isset($aimogen_Main_Settings['custom_endpoint']) && trim($aimogen_Main_Settings['custom_endpoint'], ' /') == 'https://api.mistral.ai') {//don't add max completion for mistralai } else { if((int)$available_tokens > 0) { $base_params['max_completion_tokens'] = $available_tokens; } } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { //currently tool choice is not supported in o1 models if(!aimogen_is_o1_model($model)) { $base_params['tools'] = $functions['functions']; $base_params['tool_choice'] = 'auto'; } } if (isset($aimogen_Main_Settings['ai_seed']) && $aimogen_Main_Settings['ai_seed'] != '') { $base_params['seed'] = intval($aimogen_Main_Settings['ai_seed']); } if ((isset($aimogen_Main_Settings['store_data']) && $aimogen_Main_Settings['store_data'] == 'on') || ($store_data === true || $store_data === 'on' || $store_data === '1')) { if (isset($aimogen_Main_Settings['api_selector']) && trim($aimogen_Main_Settings['api_selector']) == 'custom' && isset($aimogen_Main_Settings['custom_endpoint']) && trim($aimogen_Main_Settings['custom_endpoint'], ' /') != '') {//don't store results for custom endpoints } else { $base_params['store'] = true; } } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode chat API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $xh = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ); if (isset($aimogen_Main_Settings['openai_organization']) && $aimogen_Main_Settings['openai_organization'] != '') { $xh['OpenAI-Organization'] = $aimogen_Main_Settings['openai_organization']; } if (isset($aimogen_Main_Settings['api_selector']) && trim($aimogen_Main_Settings['api_selector']) == 'custom' && isset($aimogen_Main_Settings['custom_endpoint']) && trim($aimogen_Main_Settings['custom_endpoint'], ' /') != '') { $api_endpoint = trim($aimogen_Main_Settings['custom_endpoint'], ' /'); } else { $api_endpoint = 'https://api.openai.com'; } $api_call = wp_remote_post( $api_endpoint . '/v1/chat/completions', array( 'headers' => $xh, 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if($stream === false && $result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after decode failure(2): ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: Failed to decode initial chat API response(2): ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your OpenAI chat quota limit. To fix this, if you are using a free OpenAI account, you need to add a VISA card to your account, as OpenAI heavily limits free accounts. Please check details here: https://platform.openai.com/docs/guides/rate-limits.'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the chat API, result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { $error = 'Error: An error occurred when initially calling OpenAI chat API: ' . json_encode($result); return false; } } } if(!$stream) { if((isset($result->choices[0]->finish_reason) && $result->choices[0]->finish_reason == 'tool_calls') || (isset($result->choices[0]->finish_details->type) && $result->choices[0]->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $result->choices[0]->message->content = $functions['message']; } else { $result->choices[0]->message->content = 'OK'; } } if(!isset($result->choices[0]->message->content)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial OpenAI chat API result: ' . json_encode($result); return false; } } } else { if(isset($result->choices[0]->message->tool_calls)) { $result->tool_calls = $result->choices[0]->message->tool_calls; } $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->message->content, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { $finish_reason = $result->choices[0]->finish_details->type; } if($is_chat == true) { if($dump_result === true) { return $result; } if (empty($result->choices[0]->message->content) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, intval($retry_count) + 1, $finish_reason, $error, true, true, $functions, $stream, $vision_file, true, $user_question, $embedding_namespace, $function_result, $dump_result, $store_data, $enabled_mcp_servers); } else { if (!isset($result->aimogen_tool_direct_message) && !isset($result->aimogen_tool_end_conversation) && !isset($result->aimogen_tool_results) && empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(5) ' . json_encode($result); return false; } else { if(isset($result->aimogen_tool_direct_message) && empty($function_result)) { $my_message = ''; foreach($result->aimogen_tool_direct_message as $dm) { $my_message .= $dm['content'] . ' '; } $my_message = trim($my_message); return $my_message; } if(isset($result->aimogen_tool_end_conversation) && empty($function_result)) { return 'AimogenConversationEnded~!@#$%^&*()'; } if(isset($result->aimogen_tool_results) && empty($function_result)) { //recalling with function result return aimogen_generate_text_chat($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, $is_chat, $env, $retry_count, $finish_reason, $error, true, true, $functions, $stream, $vision_file, $dont_add_vision, $user_question, $embedding_namespace, $result->aimogen_tool_results, $dump_result, $store_data, $enabled_mcp_servers); } return $result->choices[0]->message->content; } } } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(6) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } } else { return $result; } } } $error = 'Failed to finish chat API call correctly.'; return false; } function aimogen_generate_text_google_completion($token, $model, $aicontent, $stream, &$error) { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $token = apply_filters('aimogen_google_api_key', $token); if(!aimogen_is_google_model($model)) { $model = 'text-bison-001'; } if(aimogen_is_google_chat_model($model)) { $error = 'Error: incorrect model type provided: ' . $model; return false; } if($stream === true && !in_array($model, AIMOGEN_GOOGLE_STREAMING_MODELS)) { $stream = false; } $prompt_tokens = count(aimogen_encode($aicontent)); if(aimogen_is_google_model($model)) { if($prompt_tokens > aimogen_get_max_input_tokens($model)) { $aicontent = aimogen_strip_to_token_count($aicontent, aimogen_get_max_input_tokens($model), true); } } else { $error = 'Error: incorrect model provided: ' . $model; return false; } //this is needed for streaming $ch = curl_init();// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init if($ch === false) { $error = 'Error: failed to init curl in Google AI API'; return false; } $postData = ''; switch ($model) { case 'text-bison-001': $url = "https://generativelanguage.googleapis.com/v1beta3/models/text-bison-001:generateText?key=" . $token; $postData = json_encode(array("prompt" => array("text" => $aicontent))); break; case 'chat-bison-001': $url = "https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=" . $token; $postData = json_encode(array("prompt" => array("messages" => array(array("content" => $aicontent))))); break; default: $error = 'Error: Google AI model not recognized: ' . $model; return false; } if (isset($aimogen_Main_Settings['proxy_url']) && $aimogen_Main_Settings['proxy_url'] != '' && $aimogen_Main_Settings['proxy_url'] != 'disable' && $aimogen_Main_Settings['proxy_url'] != 'disabled') { $prx = explode(',', $aimogen_Main_Settings['proxy_url']); $randomness = array_rand($prx); curl_setopt( $ch, CURLOPT_PROXY, trim($prx[$randomness]));// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt if (isset($aimogen_Main_Settings['proxy_auth']) && $aimogen_Main_Settings['proxy_auth'] != '') { $prx_auth = explode(',', $aimogen_Main_Settings['proxy_auth']); if(isset($prx_auth[$randomness]) && trim($prx_auth[$randomness]) != '') { curl_setopt( $ch, CURLOPT_PROXYUSERPWD, trim($prx_auth[$randomness]));// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt } } } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_URL, $url);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_POST, true);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt if (isset($aimogen_Main_Settings['max_timeout']) && $aimogen_Main_Settings['max_timeout'] != '') { $ztime = intval($aimogen_Main_Settings['max_timeout']); } else { $ztime = 300; } curl_setopt($ch, CURLOPT_TIMEOUT, $ztime);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt if($stream === true && in_array($model, AIMOGEN_GOOGLE_STREAMING_MODELS)) { curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($curl_info, $data)// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt { $normalizedText = '[' . trim($data, " ,[]") . ']'; $decodedResponse = json_decode($normalizedText, true); if($decodedResponse === null || isset($decodedResponse[0]['error']['message'])) { echo "data: [ERROR]\n\n"; if (ob_get_length()) { ob_flush(); } flush(); aimogen_log_to_file('Google AI streaming error: ' . $decodedResponse[0]['error']['message']); } else { if(isset($decodedResponse[0]['candidates'][0]['content']['parts'][0]['text'])) { echo "data: " . esc_html($decodedResponse[0]['candidates'][0]['content']['parts'][0]['text']) . "\n\n"; if (ob_get_length()) { ob_flush(); } flush(); } else { echo "data: [DONE]\n\n"; if (ob_get_length()) { ob_flush(); } flush(); } } return strlen($data); }); } //cur_exec needs to be kept, because of the streaming option, even if we could use wp_remote_post for non-streaming $response = curl_exec($ch);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec if (curl_errno($ch)) // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_errno { $error = 'Curl error: ' . curl_error($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_error curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close return false; } if($response === false) { $error = 'Failed to get Google API response'; curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close return false; } if($stream === true && in_array($model, AIMOGEN_GOOGLE_STREAMING_MODELS)) { return ''; } else { curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close } $decodedResponse = json_decode($response, true); if(isset($decodedResponse[0])) { $decodedResponse = $decodedResponse[0]; } if (isset($decodedResponse['error'])) { $errorMsg = isset($decodedResponse['error']['message']) ? $decodedResponse['error']['message'] : 'Unknown error from Google AI Studio API'; $error = 'Error: ' . $errorMsg . ' data: ' . $postData; return false; } elseif (isset($decodedResponse[0]['error'])) { $errorMsg = isset($decodedResponse['error']['message']) ? $decodedResponse[0]['error']['message'] : 'Unknown error from Google AI Studio API'; $error = 'Error: ' . $errorMsg . ' data: ' . $postData; return false; } elseif (empty($decodedResponse)) { $error = 'No data found in the response ' . json_encode($response); return false; } if (isset($decodedResponse['candidates'][0]['content']['parts'][0]['text'])) { $generatedText = $decodedResponse['candidates'][0]['content']['parts'][0]['text']; return $generatedText; } else { if (isset($decodedResponse['candidates'][0]['content']) && is_string($decodedResponse['candidates'][0]['content'])) { $generatedText = $decodedResponse['candidates'][0]['content']; return $generatedText; } else { if (isset($decodedResponse['candidates'][0]['output']) && is_string($decodedResponse['candidates'][0]['output'])) { $generatedText = $decodedResponse['candidates'][0]['output']; return $generatedText; } else { $error = 'No valid content found in the API response ' . json_encode($decodedResponse); return false; } } } return $decodedResponse; } function aimogen_generate_image_google($prompt, $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL, $env = '', $returnbase64 = false, &$error = '', $nolocal = false) { $is_allowed = apply_filters('aimogen_is_ai_image_allowed', true, $prompt); if ( $is_allowed !== true ) { $error = is_string( $is_allowed ) ? $is_allowed : esc_html__('You are not allowed to do this query', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $prompt = apply_filters('aimogen_modify_ai_image_query', $prompt); $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if(in_array($model, AIMOGEN_GOOGLE_IMAGE_MODELS) === false) { $error = 'This model is not currently supported by Google Images API: ' . $model; return false; } if (!isset($aimogen_Main_Settings['app_id_google']) || trim($aimogen_Main_Settings['app_id_google']) == '') { aimogen_log_to_file('You need to enter an Google Vertex API key in the plugin settings for this feature to work!'); return false; } $appids_google = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_google'])); $appids_google = array_filter($appids_google); $token = $appids_google[array_rand($appids_google)]; $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'geminiImage'; $maxResults = 1; $available_tokens = 1000; $temperature = 1; if(!isset($aimogen_Main_Settings['no_img_translate']) || $aimogen_Main_Settings['no_img_translate'] != 'on') { $text_trans = aimogen_translate_stability($prompt); if($text_trans != $prompt && !empty($text_trans)) { aimogen_log_to_file('Google Gemini prompt translated from: "' . $prompt . '" to: "' . $text_trans . '"'); $prompt = $text_trans; } } if(strlen($prompt) > 2000) { $prompt = aimogen_substr($prompt, 0, 2000); } $query = new Aimogen_lite_Query($prompt, $available_tokens, $model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, '800x600', ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = 'Image generator is rate limited: ' . $ok; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Generating Google Gemini Image using prompt: ' . $prompt); } $token = apply_filters('aimogen_google_api_key', $token); $prompt = apply_filters('aimogen_modify_ai_image_query', trim($prompt)); if (empty($prompt)) { $error = __('Empty prompt provided for Gemini image generation', 'aimogen'); return false; } if (stristr($model, 'gemini') !== false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent?key={$token}"; $aicontent = [ [ 'role' => 'user', 'content' => $prompt ] ]; $gemini_contents = array_map(function ($message) { $parts = []; if (isset($message['content'])) { $parts = [["text" => $message['content']]]; } elseif (isset($message['function_call'])) { $parts = [["functionCall" => ["name" => $message['function_call']['name']]]]; } elseif (isset($message['function_data'])) { $parts = [[ "functionResponse" => [ "name" => $message['function_data']['name'], "response" => [ "name" => $message['function_data']['name'], "content" => $message['function_data']['content'] ] ] ]]; } if (!empty($parts)) { return [ "role" => $message['role'], "parts" => $parts ]; } }, $aicontent); $gemini_contents = array_values(array_filter($gemini_contents)); $gemini_functions = []; $js_arr = [ "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "stopSequences" => [], "response_modalities" => ["TEXT", "IMAGE"] ], "safetySettings" => [ [ "category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE" ] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => ["mode" => "auto"] ] ]; if (empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch (Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if (empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } $headers = [ 'Content-Type' => 'application/json' ]; $args = [ 'method' => 'POST', 'headers' => $headers, 'body' => $postData, 'timeout' => !empty($aimogen_Main_Settings['max_timeout']) ? intval($aimogen_Main_Settings['max_timeout']) : 300, 'sslverify' => false ]; if (!empty($aimogen_Main_Settings['proxy_url']) && !in_array($aimogen_Main_Settings['proxy_url'], ['disable','disabled'], true)) { $proxies = array_map('trim', explode(',', $aimogen_Main_Settings['proxy_url'])); $idx = array_rand($proxies); $args['proxy'] = $proxies[$idx]; if (!empty($aimogen_Main_Settings['proxy_auth'])) { $authList = array_map('trim', explode(',', $aimogen_Main_Settings['proxy_auth'])); if (!empty($authList[$idx])) { $args['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($authList[$idx]); } } } $response = wp_remote_post($url, $args); if (is_wp_error($response)) { $error = 'Error: ' . esc_html($response->get_error_message()); return false; } $body = wp_remote_retrieve_body($response); if (!$body) { $error = 'Failed to get Google API response'; return false; } $decodedResponse = json_decode($body, true); if (isset($decodedResponse[0])) { $decodedResponse = $decodedResponse[0]; } if (isset($decodedResponse['error'])) { $msg = isset($decodedResponse['error']['message']) ? $decodedResponse['error']['message'] : 'Unknown error'; $error = 'Error: ' . $msg . ' data: ' . $postData; return false; } if (empty($decodedResponse)) { $error = 'No data found in the response ' . json_encode($body); return false; } $image_data = ''; if (isset($decodedResponse['candidates'][0]['content']['parts'][0]['inlineData']['data'])) { $image_data = $decodedResponse['candidates'][0]['content']['parts'][0]['inlineData']['data']; } else { $error = 'No image generated. Response: ' . json_encode($decodedResponse); return false; } apply_filters('aimogen_ai_reply', $image_data, $query); if ($returnbase64 === true) { return $image_data; } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($prompt, 4); $filename = str_replace(' ', '-', $filename); if (empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1, 99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $rezi = aimogen_base64_to_jpeg($image_data, $file, $ret_path); if ($rezi !== false && $rezi !== '') { if ($nolocal === false && isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $prompt); if ($localpath !== false) { wp_delete_file($rezi[0]); $rezi = [$localpath[1], $localpath[0]]; } } } return $decodedResponse; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:predict?key=" . urlencode($token); $payload = [ "instances" => [[ "prompt" => $prompt ]], "parameters" => [ "sampleCount" => 1 ] ]; $args = [ 'method' => 'POST', 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => wp_json_encode($payload), 'timeout' => 300, 'sslverify' => false ]; if (!empty($aimogen_Main_Settings['proxy_url']) && !in_array($aimogen_Main_Settings['proxy_url'], ['disable','disabled'], true)) { $prx = array_map('trim', explode(',', $aimogen_Main_Settings['proxy_url'])); $idx = array_rand($prx); $args['proxy'] = $prx[$idx]; if (!empty($aimogen_Main_Settings['proxy_auth'])) { $auth = array_map('trim', explode(',', $aimogen_Main_Settings['proxy_auth'])); if (!empty($auth[$idx])) { $args['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($auth[$idx]); } } } $response = wp_remote_post($url, $args); if (is_wp_error($response)) { $error = 'Request error: ' . esc_html($response->get_error_message()); return false; } $body = wp_remote_retrieve_body($response); $decoded = json_decode($body, true); if (isset($decoded['error'])) { $error = 'Gemini API Error: ' . $decoded['error']['message']; return false; } $image_data = isset($decoded['predictions'][0]['bytesBase64Encoded']) ? $decoded['predictions'][0]['bytesBase64Encoded'] : ''; apply_filters('aimogen_ai_reply', $image_data, $query); if ($returnbase64 === true) { return $image_data; } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($prompt, 4); $filename = str_replace(' ', '-', $filename); if (empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1, 99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $rezi = aimogen_base64_to_jpeg($image_data, $file, $ret_path); if ($rezi !== false && $rezi !== '') { if ($nolocal === false && isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $prompt); if ($localpath !== false) { wp_delete_file($rezi[0]); $rezi = [$localpath[1], $localpath[0]]; } } } } return $rezi; } function aimogen_list_models_google_gemini(&$error = '') { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if (empty($aimogen_Main_Settings['app_id_google'])) { aimogen_log_to_file('You need to enter a Google Vertex API key in the plugin settings for this feature to work!'); return false; } $appids_google = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id_google'])); $appids_google = array_filter($appids_google); $token = apply_filters('aimogen_google_api_key', $appids_google[array_rand($appids_google)]); $url = "https://generativelanguage.googleapis.com/v1beta/models?pageSize=1000&key=" . urlencode($token); $args = [ 'method' => 'GET', 'headers' => [ 'Content-Type' => 'application/json' ], 'timeout' => 30, 'sslverify' => false ]; if (!empty($aimogen_Main_Settings['proxy_url']) && !in_array($aimogen_Main_Settings['proxy_url'], ['disable','disabled'], true)) { $proxies = array_map('trim', explode(',', $aimogen_Main_Settings['proxy_url'])); $idx = array_rand($proxies); $args['proxy'] = $proxies[$idx]; if (!empty($aimogen_Main_Settings['proxy_auth'])) { $auth = array_map('trim', explode(',', $aimogen_Main_Settings['proxy_auth'])); if (!empty($auth[$idx])) { $args['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($auth[$idx]); } } } $response = wp_remote_get($url, $args); if (is_wp_error($response)) { $error = 'Request error: ' . esc_html($response->get_error_message()); return false; } $body = wp_remote_retrieve_body($response); $decoded = json_decode($body, true); if (isset($decoded['error'])) { $error = 'Gemini API Error: ' . $decoded['error']['message']; return false; } return $decoded; } function aimogen_generate_text_google_chat($token, $model, $aicontent, $temperature, $top_p, $vision_file, $functions, $function_result, $max_tokens, $stream, &$error) { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $token = apply_filters('aimogen_google_api_key', $token); if(!aimogen_is_google_model($model)) { $model = 'gemini-2.5-flash'; } if(!aimogen_is_google_chat_model($model)) { $error = 'Error: incorrect model type provided: ' . $model; return false; } if(is_string($aicontent)) { $prompt_tokens = count(aimogen_encode($aicontent)); if(aimogen_is_google_model($model)) { if($prompt_tokens > aimogen_get_max_input_tokens($model)) { $aicontent = aimogen_strip_to_token_count($aicontent, aimogen_get_max_input_tokens($model), true); } } else { $error = 'Error: incorrect model provided: ' . $model; return false; } } if(!empty($function_result) && is_array($function_result) && is_array($aicontent)) { foreach($function_result as $fname => $fdata) { $fonedata = array("role" => 'model', "function_call" => ['name' => $fname]); $aicontent[] = $fonedata; $ftwodata = array("role" => 'user', "function_data" => ['name' => $fname, 'content' => $fdata]); $aicontent[] = $ftwodata; } } $gemini_contents = array_map(function ($message) { $parts = []; if(isset($message['content'])) { $parts = [["text" => $message['content']]]; } elseif(isset($message['function_call'])) { $parts = [["functionCall" => ["name" => $message['function_call']['name']]]]; } elseif(isset($message['function_data'])) { $parts = [["functionResponse" => ["name" => $message['function_data']['name'], "response" => ["name" => $message['function_data']['name'], "content" => $message['function_data']['content']]]]]; } if(!empty($parts)) { return [ "role" => $message['role'], "parts" => $parts ]; } else { return; } }, $aicontent); $gemini_contents = array_filter($gemini_contents); $gemini_contents = array_values($gemini_contents); if($stream === true && !in_array($model, AIMOGEN_GOOGLE_STREAMING_MODELS)) { $stream = false; } $gemini_functions = array(); if(isset($functions['functions']) && is_array($functions['functions']) && !empty($functions['functions'])) { $gemini_functions['function_declarations'] = array(); foreach($functions['functions'] as $my_func) { if($my_func['type'] == 'function') { $func_details = $my_func['function']; $fbuild = array(); $fbuild['name'] = $func_details->name; $fbuild['description'] = $func_details->description; $fbuild['parameters'] = array('type' => 'object'); if(isset($func_details->parameters) && is_array($func_details->parameters)) { $req_arr = array(); $fbuild['parameters']['properties'] = array(); foreach($func_details->parameters as $pp) { $fbuild['parameters']['properties'][$pp->name] = array("type" => $pp->type, "description" => $pp->description); if(isset($pp->required) && $pp->required == '1') { $req_arr[] = $pp->name; } } $fbuild['parameters']['required'] = $req_arr; } $gemini_functions['function_declarations'][] = $fbuild; } } } $postData = ''; switch ($model) { case 'gemini-2.0-flash-thinking-exp-01-21': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-thinking-exp-01-21:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-thinking-exp-01-21:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-pro-exp-03-25': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro-exp-03-25:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro-exp-03-25:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-3-pro-preview': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-pro-preview:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-pro-preview:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-flash-lite-preview-06-17': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite-preview-06-17:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite-preview-06-17:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-pro-preview-06-05': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro-preview-06-05:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro-preview-06-05:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-pro': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-flash': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-flash-lite': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-flash-preview-05-20': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.5-flash-preview-04-17': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-04-17:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-04-17:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemma-3-27b-it': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemma-3-27b-it:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemma-3-27b-it:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.0-pro-exp-02-05': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-pro-exp-02-05:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-pro-exp-02-05:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.0-flash-exp': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.0-flash': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; case 'gemini-2.0-flash-lite': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aimogen_get_base64_from_url($vision_file); if (!empty($base64_vision)) { $gemini_contents[0]['parts'][]['inline_data'] = [ 'mime_type' => 'image/jpeg', 'data' => $base64_vision ]; } } $js_arr = array( "contents" => $gemini_contents, "generationConfig" => [ "temperature" => $temperature, "topK" => 1, "topP" => $top_p, "stopSequences" => [] ], "safetySettings" => [ ["category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"] ], "tools" => $gemini_functions, "tool_config" => [ "function_calling_config" => [ "mode" => "auto" ] ] ); if((int)$max_tokens > 0) { $js_arr['generationConfig']['maxOutputTokens'] = $max_tokens; } if(empty($gemini_functions)) { unset($js_arr['tool_config']); } try { $postData = aimogen_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . $e->getMessage(); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . json_encode($js_arr); return false; } break; default: $error = 'Error: Google AI model not recognized: ' . $model; return false; } $ch = curl_init();// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init if($ch === false) { $error = 'Error: failed to init curl in Google AI API'; return false; } if (isset($aimogen_Main_Settings['proxy_url']) && $aimogen_Main_Settings['proxy_url'] != '' && $aimogen_Main_Settings['proxy_url'] != 'disable' && $aimogen_Main_Settings['proxy_url'] != 'disabled') { $prx = explode(',', $aimogen_Main_Settings['proxy_url']); $randomness = array_rand($prx); curl_setopt( $ch, CURLOPT_PROXY, trim($prx[$randomness]));// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt if (isset($aimogen_Main_Settings['proxy_auth']) && $aimogen_Main_Settings['proxy_auth'] != '') { $prx_auth = explode(',', $aimogen_Main_Settings['proxy_auth']); if(isset($prx_auth[$randomness]) && trim($prx_auth[$randomness]) != '') { curl_setopt( $ch, CURLOPT_PROXYUSERPWD, trim($prx_auth[$randomness]));// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt } } } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_URL, $url);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_POST, true);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt if (isset($aimogen_Main_Settings['max_timeout']) && $aimogen_Main_Settings['max_timeout'] != '') { $ztime = intval($aimogen_Main_Settings['max_timeout']); } else { $ztime = 300; } curl_setopt($ch, CURLOPT_TIMEOUT, $ztime);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt if($stream === true && in_array($model, AIMOGEN_GOOGLE_STREAMING_MODELS)) { curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($curl_info, $data)// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt { $normalizedText = '[' . trim($data, " ,[]") . ']'; $decodedResponse = json_decode($normalizedText, true); if($decodedResponse === null || isset($decodedResponse[0]['error']['message'])) { echo "data: [ERROR]\n\n"; if (ob_get_length()) { ob_flush(); } flush(); aimogen_log_to_file('Google AI streaming error: ' . json_encode($decodedResponse)); } else { if(isset($decodedResponse[0]['candidates'][0]['content']['parts'][0]['text'])) { echo "data: " . esc_html($decodedResponse[0]['candidates'][0]['content']['parts'][0]['text']) . "\n\n"; if (ob_get_length()) { ob_flush(); } flush(); } elseif(isset($decodedResponse[0]['candidates'][0]['content']['parts'][0]['functionCall'])) { if (ob_get_length()) { ob_flush(); } flush(); } else { echo "data: [DONE]\n\n"; if (ob_get_length()) { ob_flush(); } flush(); } } return strlen($data); }); } //curl_exec needs to be kept because of the streaming option, even if we could use wp API functions, as they don't support streaming yet. $response = curl_exec($ch);// phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec if (curl_errno($ch)) // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_errno { $error = 'Curl error: ' . curl_error($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_error curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close return false; } if($response === false) { $error = 'Failed to get Google API response'; curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close return false; } if($stream === true && in_array($model, AIMOGEN_GOOGLE_STREAMING_MODELS)) { return ''; } else { curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close } $decodedResponse = json_decode($response, true); if(isset($decodedResponse[0])) { $decodedResponse = $decodedResponse[0]; } if (isset($decodedResponse['error'])) { $errorMsg = isset($decodedResponse['error']['message']) ? $decodedResponse['error']['message'] : 'Unknown error from Google AI Studio API'; $error = 'Error: ' . $errorMsg . ' data: ' . $postData; return false; } elseif (isset($decodedResponse[0]['error'])) { $errorMsg = isset($decodedResponse['error']['message']) ? $decodedResponse[0]['error']['message'] : 'Unknown error from Google AI Studio API'; $error = 'Error: ' . $errorMsg . ' data: ' . $postData; return false; } elseif (empty($decodedResponse)) { $error = 'No data found in the response ' . json_encode($response); return false; } $response_text = false; if (isset($decodedResponse['candidates'][0]['content']['parts'][0])) { foreach($decodedResponse['candidates'][0]['content']['parts'] as $current_data) { if(isset($current_data['text'])) { $response_text = $current_data['text']; } elseif(isset($current_data['functionCall'])) { $filter_tool_object = new stdClass(); $filter_tool_object->tool_calls = array(); $tool_oarr = new stdClass(); $tool_oarr->type = 'function'; $tool_oarr->id = uniqid(); $tool_oarr->function = new stdClass(); $tool_oarr->function->name = $current_data['functionCall']['name']; $tool_oarr->function->arguments = (object) $current_data['functionCall']['args']; $filter_tool_object->tool_calls[] = $tool_oarr; $filter_tool_object = apply_filters('aimogen_google_tools_ai_reply_raw', $filter_tool_object); if(isset($filter_tool_object->aimogen_tool_direct_message) && isset($filter_tool_object->aimogen_tool_direct_message[0]) && isset($filter_tool_object->aimogen_tool_direct_message[0]['content']) && !empty($filter_tool_object->aimogen_tool_direct_message[0]['content'])) { return $filter_tool_object->aimogen_tool_direct_message[0]['content']; } if(isset($filter_tool_object->aimogen_tool_end_conversation) && isset($filter_tool_object->aimogen_tool_end_conversation[0]) && isset($filter_tool_object->aimogen_tool_end_conversation[0]['content']) && !empty($filter_tool_object->aimogen_tool_end_conversation[0]['content'])) { return 'AimogenConversationEnded~!@#$%^&*()'; } if(isset($filter_tool_object->aimogen_tool_results) && isset($filter_tool_object->aimogen_tool_results[0]) && isset($filter_tool_object->aimogen_tool_results[0]['content']) && !empty($filter_tool_object->aimogen_tool_results[0]['content'])) { remove_filter('aimogen_ai_functions', 'aimogen_add_god_mode', 999); $function_back_in = array($tool_oarr->function->name => $filter_tool_object->aimogen_tool_results[0]['content']); return aimogen_generate_text_google_chat($token, $model, $aicontent, $temperature, $top_p, $vision_file, false, $function_back_in, $max_tokens, $stream, $error); } } } if($response_text !== false) { return $response_text; } else { $error = 'No valid response found ' . json_encode($decodedResponse); return false; } } else { if (isset($decodedResponse['candidates'][0]['content']) && is_string($decodedResponse['candidates'][0]['content'])) { $generatedText = $decodedResponse['candidates'][0]['content']; return $generatedText; } else { if (isset($decodedResponse['candidates'][0]['output']) && is_string($decodedResponse['candidates'][0]['output'])) { $generatedText = $decodedResponse['candidates'][0]['output']; return $generatedText; } else { $error = 'No valid content found in the response ' . json_encode($decodedResponse); return false; } } } return $decodedResponse; } function aimogen_create_openai_video( $prompt, $model = 'sora-2', $size = '1280x720', $seconds = '4', $env = 'aimogen' ) { try { if(!in_array($model, AIMOGEN_MODELS_VIDEO)) { throw new Exception('Invalid model specified.'); } $possible_sizes = AIMOGEN_MODELS_VIDEO_PARAMS[$model]['durations']; if(!in_array($seconds, $possible_sizes)) { throw new Exception('Invalid duration specified for the selected model.'); } $resolutions = AIMOGEN_MODELS_VIDEO_PARAMS[$model]['resolutions']; if(!array_key_exists($size, $resolutions)) { throw new Exception('Invalid resolution specified for the selected model.'); } if(empty($env)) { throw new Exception('Environment ID is required.'); } if(empty($prompt)) { throw new Exception('Prompt is required to generate a video.'); } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if (!isset($aimogen_Main_Settings['app_id']) || trim($aimogen_Main_Settings['app_id']) == '') { throw new Exception('You need to add an API key in plugin settings for this shortcode to work.'); } else { $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_openai_api_key', $token); if(empty($token)) { throw new Exception('Invalid API key submitted'); } if(aimogen_is_aimogen_key($token)) { throw new Exception('Currently only OpenAI API is supported for video processing.'); } } $response = wp_remote_post( 'https://api.openai.com/v1/videos', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ], 'body' => wp_json_encode([ 'model' => $model, 'prompt' => $prompt, 'seconds' => $seconds, 'size' => $size ]), 'timeout' => 120 ]); if ( is_wp_error( $response ) ) { throw new Exception( 'OpenAI: ' . $response->get_error_message() ); } $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $response_body['error'] ) ) { throw new Exception( $response_body['error']['message'] ?? 'A generic error occurred' ); } if ( isset( $response_body['id'] ) ) { set_transient( 'aimogen_openai_video_' . $response_body['id'], [ 'model' => $model, 'env_id' => $env, 'created_at' => time(), 'prompt' => $prompt, 'size' => $size, 'seconds' => $seconds, 'user_id' => get_current_user_id() ], DAY_IN_SECONDS ); } else { throw new Exception( 'Video ID not found in the response.' ); } $pending_videos = get_user_meta(get_current_user_id(), 'aimogen_pending_videos', true) ?: []; $pending_videos[] = $response_body['id']; update_user_meta(get_current_user_id(), 'aimogen_pending_videos', array_unique($pending_videos)); return $response_body['id']; } catch ( Exception $e ) { return ['error' => true, 'message' => 'Exception occurred in video creation: ' . $e->getMessage()]; } } function aimogen_check_openai_video_status( $video_ids) { try { if ( empty( $video_ids ) ) { throw new Exception( 'Empty video ids provided' ); } if ( !is_array( $video_ids ) ) { $video_ids = [ $video_ids ]; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if (!isset($aimogen_Main_Settings['app_id']) || trim($aimogen_Main_Settings['app_id']) == '') { throw new Exception('You need to add an API key in plugin settings for this shortcode to work.'); } else { $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_openai_api_key', $token); if(empty($token)) { throw new Exception('Invalid API key submitted'); } if(aimogen_is_aimogen_key($token)) { throw new Exception('Currently only OpenAI API is supported for video processing.'); } } $videos = []; foreach ( $video_ids as $video_id ) { $response = wp_remote_get( 'https://api.openai.com/v1/videos/' . $video_id, [ 'headers' => [ 'Authorization' => 'Bearer ' . $token ], 'timeout' => 120 ] ); if ( !is_wp_error( $response ) ) { $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( !isset( $body['error'] ) ) { if ( $body['status'] === 'completed' && empty( get_transient( 'aimogen_video_attach_id_' . $video_id ) ) ) { $metadata = get_transient( 'aimogen_openai_video_' . $video_id ); $ai_metadata = []; if ( $metadata ) { $ai_metadata = [ 'model' => $metadata['model'] ?? null, 'prompt' => $metadata['prompt'] ?? null, 'size' => $metadata['size'] ?? null, 'seconds' => $metadata['seconds'] ?? null, 'env_id' => $metadata['env_id'] ?? null, 'processing_time' => isset( $metadata['created_at'] ) ? ( time() - $metadata['created_at'] ) : null ]; } $attachment_id = aimogen_download_and_save_video( $video_id, $token, $ai_metadata ); if ( $attachment_id ) { $body['attachment_id'] = $attachment_id; $file_path = get_attached_file( $attachment_id ); $upload_dir = wp_upload_dir(); $body['url'] = str_replace( $upload_dir['basedir'], $upload_dir['baseurl'], $file_path ); set_transient( 'aimogen_video_attach_id_' . $video_id, $attachment_id, DAY_IN_SECONDS ); delete_transient( 'aimogen_openai_video_' . $video_id ); $pending_videos = get_user_meta(get_current_user_id(), 'aimogen_pending_videos', true) ?: []; $pending_videos = array_diff($pending_videos, [$video_id]); update_user_meta(get_current_user_id(), 'aimogen_pending_videos', $pending_videos); } } else if ( $body['status'] === 'completed' ) { $attachment_id = get_transient( 'aimogen_video_attach_id_' . $video_id ); if ( $attachment_id ) { $body['attachment_id'] = $attachment_id; $file_path = get_attached_file( $attachment_id ); $upload_dir = wp_upload_dir(); $body['url'] = str_replace( $upload_dir['basedir'], $upload_dir['baseurl'], $file_path ); } } else if ($body['status'] === 'failed') { $pending_videos = get_user_meta(get_current_user_id(), 'aimogen_pending_videos', true) ?: []; $pending_videos = array_diff($pending_videos, [$video_id]); update_user_meta(get_current_user_id(), 'aimogen_pending_videos', $pending_videos); $pending_videos = get_user_meta(get_current_user_id(), 'aimogen_pending_videos', true) ?: []; $pending_videos = array_diff($pending_videos, [$video_id]); update_user_meta(get_current_user_id(), 'aimogen_pending_videos', $pending_videos); } $videos[] = $body; } else { aimogen_log_to_file( 'Video generation failed for AI video ID ' . $video_id . ': ' . json_encode( $body['error'] ) ); $videos[] = [ 'id' => $video_id, 'status' => 'failed', 'error' => $body['error'] ]; $pending_videos = get_user_meta(get_current_user_id(), 'aimogen_pending_videos', true) ?: []; $pending_videos = array_diff($pending_videos, [$video_id]); update_user_meta(get_current_user_id(), 'aimogen_pending_videos', $pending_videos); } } else { aimogen_log_to_file( 'Failed to check video status for AI video ID ' . $video_id . ': ' . $response->get_error_message() ); $videos[] = [ 'id' => $video_id, 'status' => 'failed', 'error' => [ 'message' => $response->get_error_message() ] ]; } } return $videos; } catch ( Exception $e ) { return ['error' => true, 'message' => $e->getMessage()]; } } function aimogen_download_and_save_video( $video_id, $api_key, $ai_metadata = [] ) { try { $response = wp_remote_get( 'https://api.openai.com/v1/videos/' . $video_id . '/content', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key ], 'timeout' => 120 ] ); if ( is_wp_error( $response ) ) { return ['error' => true, 'message' => 'Error downloading the AI video: ' . $response->get_error_message()]; } $video_data = wp_remote_retrieve_body( $response ); if ( empty( $video_data ) ) { return ['error' => true, 'message' => 'Empty video in the response']; } $filename = $video_id . '.mp4'; $upload_dir = wp_upload_dir(); $file_path = $upload_dir['path'] . '/' . $filename; global $wp_filesystem; if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){ require_once ABSPATH . 'wp-admin/includes/file.php'; $creds = request_filesystem_credentials( site_url() ); wp_filesystem($creds); } $wp_filesystem->put_contents( $file_path, $video_data ); $attachment = [ 'post_mime_type' => 'video/mp4', 'post_title' => $video_id, 'post_content' => '', 'post_status' => 'inherit', 'post_type' => 'aimogen_video', 'post_author' => get_current_user_id() ]; $attachment_id = wp_insert_post( $attachment ); if ( is_wp_error( $attachment_id ) ) { return ['error' => true, 'message' => 'Error creating local AI video: ' . $attachment_id->get_error_message()]; } update_attached_file( $attachment_id, $file_path ); require_once ABSPATH . 'wp-admin/includes/image.php'; $attach_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); wp_update_attachment_metadata( $attachment_id, $attach_data ); if ( !empty( $ai_metadata['model'] ) ) { update_post_meta( $attachment_id, 'ai_video_model', sanitize_text_field( $ai_metadata['model'] ) ); } if ( !empty( $ai_metadata['processing_time'] ) ) { update_post_meta( $attachment_id, 'ai_video_processing_time', floatval( $ai_metadata['processing_time'] ) ); } if ( !empty( $ai_metadata['seconds'] ) ) { update_post_meta( $attachment_id, 'seconds', floatval( $ai_metadata['seconds'] ) ); } if ( !empty( $ai_metadata['size'] ) ) { update_post_meta( $attachment_id, 'size', sanitize_text_field( $ai_metadata['size'] ) ); } if ( !empty( $ai_metadata['prompt'] ) ) { update_post_meta( $attachment_id, 'prompt', sanitize_text_field( $ai_metadata['prompt'] ) ); } if ( !empty( $ai_metadata['env_id'] ) ) { update_post_meta( $attachment_id, 'ai_video_env_id', sanitize_text_field( $ai_metadata['env_id'] ) ); } return $attachment_id; } catch ( Exception $e ) { return ['error' => true, 'message' => $e->getMessage()]; } } function aimogen_download_video( $video_id, $api_key ) { try { if ( empty( $api_key ) ) { throw new Exception( 'OpenAI API key is required' ); } if ( empty( $video_id ) ) { throw new Exception( 'A video_id is required' ); } $temp_file = wp_tempnam( $video_id . '.mp4' ); $response = wp_remote_get( 'https://api.openai.com/v1/videos/' . $video_id . '/content', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key ], 'timeout' => 120, 'stream' => true, 'filename' => $temp_file ] ); if ( is_wp_error( $response ) ) { throw new Exception( $response->get_error_message() ); } $file_data = file_get_contents( $temp_file ); $base64 = base64_encode( $file_data ); wp_delete_file( $temp_file ); return $base64; } catch ( Exception $e ) { return ['error' => true, 'message' => $e->getMessage()]; } } function aimogen_delete_video( $video_id, $api_key ) { try { if ( empty( $api_key ) ) { throw new Exception( 'OpenAI API key not found.' ); } if ( empty( $video_id ) ) { throw new Exception( 'A video_id is required' ); } $response = wp_remote_request( 'https://api.openai.com/v1/videos/' . $video_id, [ 'method' => 'DELETE', 'headers' => [ 'Authorization' => 'Bearer ' . $api_key ], 'timeout' => 120 ] ); if ( is_wp_error( $response ) ) { throw new Exception( $response->get_error_message() ); } return true; } catch ( Exception $e ) { return ['error' => true, 'message' => $e->getMessage()]; } } function aimogen_save_video_to_library( $video_id, $filename, $api_key ) { try { if ( !preg_match( '/\.mp4$/i', $filename ) ) { $filename .= '.mp4'; } if ( empty( $api_key ) ) { throw new Exception( 'OpenAI API key is required' ); } if ( empty( $video_id ) ) { throw new Exception( 'video_id is required' ); } $response = wp_remote_get( 'https://api.openai.com/v1/videos/' . $video_id . '/content', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key ], 'timeout' => 120 ] ); if ( is_wp_error( $response ) ) { throw new Exception( $response->get_error_message() ); } $title = sanitize_file_name($video_id); $video_data = wp_remote_retrieve_body( $response ); $upload_dir = wp_upload_dir(); $file_path = $upload_dir['path'] . '/' . $filename; global $wp_filesystem; if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){ require_once ABSPATH . 'wp-admin/includes/file.php'; $creds = request_filesystem_credentials( site_url() ); wp_filesystem($creds); } $wp_filesystem->put_contents( $file_path, $video_data ); $attachment = [ 'post_mime_type' => 'video/mp4', 'post_title' => $title, 'post_content' => '', 'post_status' => 'inherit' ]; $attach_id = wp_insert_attachment( $attachment, $file_path ); require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path ); wp_update_attachment_metadata( $attach_id, $attach_data ); return $attach_id; } catch ( Exception $e ) { return ['error' => true, 'message' => $e->getMessage()]; } } function aimogen_delete_video_from_library( $post_id ) { try { if ( empty( $post_id ) ) { throw new Exception( 'Post ID is required!' ); } $post = get_post( $post_id ); if ( ! $post ) { throw new Exception( 'Post not found.' ); } $post_type = get_post_type( $post_id ); if ( $post_type === 'attachment' ) { $deleted = wp_delete_attachment( $post_id, true ); if ( ! $deleted ) { throw new Exception( 'Failed to delete attachment.' ); } } else { $deleted = wp_delete_post( $post_id, true ); if ( ! $deleted ) { throw new Exception( 'Failed to delete custom post type.' ); } } return true; } catch ( Exception $e ) { return ['error' => true, 'message' => $e->getMessage()]; } } function aimogen_generate_text_perplexity($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, &$error, $function_result) { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $token = apply_filters('aimogen_perplexity_api_key', $token); if($temperature == 2) { $temperature = 1.9; } if($frequency_penalty <= 0) { $frequency_penalty = 1; } if(is_array($aicontent)) { if(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'system') { if(isset($aicontent[1]['role']) && $aicontent[1]['role'] == 'assistant') { array_splice( $aicontent, 1, 0, array(array('role' => 'user', 'content' => ' ')) ); } } elseif(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'assistant') { array_splice( $aicontent, 0, 0, array(array('role' => 'user', 'content' => ' ')) ); } } $base_params = [ 'model' => $model, 'messages' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty, ]; if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { $base_params['max_tokens'] = $available_tokens; } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { //currently tool choice is not supported in o1 models if(!aimogen_is_o1_model($model)) { $base_params['tools'] = $functions['functions']; $base_params['tool_choice'] = 'auto'; } } if (isset($aimogen_Main_Settings['ai_seed']) && $aimogen_Main_Settings['ai_seed'] != '') { $base_params['seed'] = intval($aimogen_Main_Settings['ai_seed']); } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode chat API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( 'https://api.perplexity.ai/chat/completions', array( 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); return aimogen_generate_text_perplexity($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after decode failure(3): ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); return aimogen_generate_text_perplexity($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to decode initial chat API response(3): ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your PerplexityAI chat quota limit.'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the chat API (2), result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_perplexity($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: An error occurred when initially calling PerplexityAI chat API: ' . json_encode($result); return false; } } } if(!$stream) { if((isset($result->choices[0]->finish_reason) && $result->choices[0]->finish_reason == 'tool_calls') || (isset($result->choices[0]->finish_details->type) && $result->choices[0]->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $result->choices[0]->message->content = $functions['message']; } else { $result->choices[0]->message->content = 'OK'; } } if(!isset($result->choices[0]->message->content)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_perplexity($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial PerplexityAI chat API result: ' . json_encode($result); return false; } } } else { if(isset($result->choices[0]->message->tool_calls)) { $result->tool_calls = $result->choices[0]->message->tool_calls; } $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->message->content, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { $finish_reason = $result->choices[0]->finish_details->type; } if($is_chat == true) { if (empty($result->choices[0]->message->content) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_perplexity($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(7) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } else { if (empty($result->choices[0]->message->content) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_perplexity']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_perplexity($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(8) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } } } else { return $result; } } } function aimogen_generate_text_groq($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, &$error, $function_result) { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $token = apply_filters('aimogen_groq_api_key', $token); if($temperature == 2) { $temperature = 1.9; } if($frequency_penalty <= 0) { $frequency_penalty = 1; } if(is_array($aicontent)) { if(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'system') { if(isset($aicontent[1]['role']) && $aicontent[1]['role'] == 'assistant') { array_splice( $aicontent, 1, 0, array(array('role' => 'user', 'content' => ' ')) ); } } elseif(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'assistant') { array_splice( $aicontent, 0, 0, array(array('role' => 'user', 'content' => ' ')) ); } } if(!empty($vision_file) && $aicontent[0]['role'] == 'system') { $aicontent[0]['role'] = 'user'; } $base_params = [ 'model' => $model, 'messages' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty, ]; if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { $base_params['max_tokens'] = $available_tokens; } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { //currently tool choice is not supported in o1 models if(!aimogen_is_o1_model($model)) { $base_params['tools'] = $functions['functions']; $base_params['tool_choice'] = 'auto'; } } if (isset($aimogen_Main_Settings['ai_seed']) && $aimogen_Main_Settings['ai_seed'] != '') { $base_params['seed'] = intval($aimogen_Main_Settings['ai_seed']); } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode chat API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( 'https://api.groq.com/openai/v1/chat/completions', array( 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_groq']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_groq($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_groq']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after decode failure(3): ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_groq($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to decode initial chat API response(4): ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your PerplexityAI chat quota limit.'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the chat API (2), result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_groq']), $token); if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_groq($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: An error occurred when initially calling PerplexityAI chat API: ' . json_encode($result); return false; } } } if(!$stream) { if((isset($result->choices[0]->finish_reason) && $result->choices[0]->finish_reason == 'tool_calls') || (isset($result->choices[0]->finish_details->type) && $result->choices[0]->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $result->choices[0]->message->content = $functions['message']; } else { $result->choices[0]->message->content = 'OK'; } } if(!isset($result->choices[0]->message->content)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_groq']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_groq($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial Groq chat API result: ' . json_encode($result); return false; } } } else { if(isset($result->choices[0]->message->tool_calls)) { $result->tool_calls = $result->choices[0]->message->tool_calls; } $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->message->content, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { $finish_reason = $result->choices[0]->finish_details->type; } if($is_chat == true) { if (empty($result->choices[0]->message->content) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_groq']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_groq($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(9) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(10) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } } else { return $result; } } } function aimogen_generate_text_nvidia($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, &$error, $function_result) { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $token = apply_filters('aimogen_nvidia_api_key', $token); if($temperature == 2) { $temperature = 1.9; } if($frequency_penalty <= 0) { $frequency_penalty = 1; } if(is_array($aicontent)) { if(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'system') { if(isset($aicontent[1]['role']) && $aicontent[1]['role'] == 'assistant') { array_splice( $aicontent, 1, 0, array(array('role' => 'user', 'content' => ' ')) ); } } elseif(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'assistant') { array_splice( $aicontent, 0, 0, array(array('role' => 'user', 'content' => ' ')) ); } } if(!empty($vision_file) && $aicontent[0]['role'] == 'system') { $aicontent[0]['role'] = 'user'; } $base_params = [ 'model' => $model, 'messages' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty, ]; if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { $base_params['max_tokens'] = $available_tokens; } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { //currently tool choice is not supported in o1 models if(!aimogen_is_o1_model($model)) { $base_params['tools'] = $functions['functions']; $base_params['tool_choice'] = 'auto'; } } if (isset($aimogen_Main_Settings['ai_seed']) && $aimogen_Main_Settings['ai_seed'] != '') { $base_params['seed'] = intval($aimogen_Main_Settings['ai_seed']); } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode chat API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( 'https://integrate.api.nvidia.com/v1/chat/completions', array( 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_nvidia']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_nvidia($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_nvidia']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after decode failure(3): ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_nvidia($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to decode initial chat API response(4): ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your PerplexityAI chat quota limit.'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the chat API (2), result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_nvidia']), $token); if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_nvidia($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: An error occurred when initially calling PerplexityAI chat API: ' . json_encode($result); return false; } } } if(!$stream) { if((isset($result->choices[0]->finish_reason) && $result->choices[0]->finish_reason == 'tool_calls') || (isset($result->choices[0]->finish_details->type) && $result->choices[0]->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $result->choices[0]->message->content = $functions['message']; } else { $result->choices[0]->message->content = 'OK'; } } if(!isset($result->choices[0]->message->content)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_nvidia']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_nvidia($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial Nvidia chat API result: ' . json_encode($result); return false; } } } else { if(isset($result->choices[0]->message->tool_calls)) { $result->tool_calls = $result->choices[0]->message->tool_calls; } $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->message->content, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { if(isset($result->choices[0]->finish_details)) { $finish_reason = $result->choices[0]->finish_details->type; } else { $finish_reason = 'stop'; } } if($is_chat == true) { if (empty($result->choices[0]->message->content) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_nvidia']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_nvidia($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(11) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(12) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } } else { return $result; } } } function aimogen_generate_text_xai($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, $retry_count, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, &$error, $function_result) { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $token = apply_filters('aimogen_xai_api_key', $token); if($temperature == 2) { $temperature = 1.9; } if($frequency_penalty <= 0) { $frequency_penalty = 1; } if(is_array($aicontent)) { if(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'system') { if(isset($aicontent[1]['role']) && $aicontent[1]['role'] == 'assistant') { array_splice( $aicontent, 1, 0, array(array('role' => 'user', 'content' => ' ')) ); } } elseif(isset($aicontent[0]['role']) && $aicontent[0]['role'] == 'assistant') { array_splice( $aicontent, 0, 0, array(array('role' => 'user', 'content' => ' ')) ); } } if(!empty($vision_file) && $aicontent[0]['role'] == 'system') { $aicontent[0]['role'] = 'user'; } $base_params = [ 'model' => $model, 'messages' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'presence_penalty' => $presence_penalty, 'frequency_penalty' => $frequency_penalty, ]; if($model == 'grok-3-mini' || $model == 'grok-3-fast' || $model == 'grok-3-mini-fast' || $model == 'grok-4-0709' || $model == 'grok-4-fast-non-reasoning' || $model == 'grok-4-fast-reasoning' || $model == 'grok-code-fast-1') { unset($base_params['presence_penalty']); unset($base_params['frequency_penalty']); } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { if((int)$available_tokens > 0) { $base_params['max_tokens'] = $available_tokens; } } if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions'])) { $base_params['tools'] = $functions['functions']; $base_params['tool_choice'] = 'auto'; } if (isset($aimogen_Main_Settings['ai_seed']) && $aimogen_Main_Settings['ai_seed'] != '') { $base_params['seed'] = intval($aimogen_Main_Settings['ai_seed']); } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode chat API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterCurlForStream'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( 'https://api.x.ai/v1/chat/completions', array( 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterCurlForStream'); } if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_xai']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after initial failure: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_xai($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } } else { $result = json_decode( $api_call['body'] ); if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_xai']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after decode failure(3): ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_xai($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: Failed to decode initial chat API response(4): ' . json_encode($api_call); return false; } } $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'Error: You exceeded your PerplexityAI chat quota limit.'; return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the chat API (2), result: ' . json_encode($result); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_xai']), $token); if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after type failure: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_xai($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { $error = 'Error: An error occurred when initially calling PerplexityAI chat API: ' . json_encode($result); return false; } } } if(!$stream) { if((isset($result->choices[0]->finish_reason) && $result->choices[0]->finish_reason == 'tool_calls') || (isset($result->choices[0]->finish_details->type) && $result->choices[0]->finish_details->type == 'tool_calls')) { if(isset($functions['message'])) { $result->choices[0]->message->content = $functions['message']; } else { $result->choices[0]->message->content = 'OK'; } } if(!isset($result->choices[0]->message->content)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_xai']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after choices failure: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_xai($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if(isset($result->code) && $result->code == 'content_filter') { $error = 'Error: The response was filtered by our content management policy.'; return false; } else { $error = 'Error: Choices not found in initial xAI chat API result: ' . json_encode($result); return false; } } } else { if(isset($result->choices[0]->message->tool_calls)) { $result->tool_calls = $result->choices[0]->message->tool_calls; } $result = apply_filters( 'aimogen_ai_reply_raw', $result, $aicontent ); apply_filters( 'aimogen_ai_reply', $result->choices[0]->message->content, $query ); if(isset($result->choices[0]->finish_reason)) { $finish_reason = $result->choices[0]->finish_reason; } else { $finish_reason = $result->choices[0]->finish_details->type; } if($is_chat == true) { if (empty($result->choices[0]->message->content) && isset($aimogen_Main_Settings['max_chat_retry']) && $aimogen_Main_Settings['max_chat_retry'] != '' && is_numeric($aimogen_Main_Settings['max_chat_retry']) && intval($aimogen_Main_Settings['max_chat_retry']) > $retry_count) { $token = aimogen_try_new_key(trim($aimogen_Main_Settings['app_id_xai']), $token); aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') chat API call after AI writer ended conversation.'); $model = apply_filters('aimogen_model_fallback', $model, $error, $aicontent); $aicontent = apply_filters('aimogen_aicontent_try_fix', $aicontent, $error, $model); $available_tokens = apply_filters('aimogen_available_tokens_try_fix', $available_tokens, $error, $model); return aimogen_generate_text_xai($token, $model, $aicontent, $temperature, $top_p, $presence_penalty, $frequency_penalty, $functions, $available_tokens, $stream, intval($retry_count) + 1, $query, $count_vision, $vision_file, $user_question, $env, $is_chat, $error, $function_result); } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(13) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } else { if (empty($result->choices[0]->message->content)) { $error = 'Error: No content found in the response(14) ' . json_encode($result); return false; } else { return $result->choices[0]->message->content; } } } } else { return $result; } } } function aimogen_generate_text_local_claude($token, $model, $aicontent, $temperature, $top_p, $vision_file, $max_tokens, $stream, $is_chat, $enabled_mcp_servers, &$error) { $token = apply_filters('aimogen_anthropic_api_key', $token); if(!aimogen_is_claude_3_model($model)) { return aimogen_generate_text_local_claude_completion($token, $model, $aicontent, $temperature, $top_p, $max_tokens, $stream, $is_chat, $error); } else { return aimogen_generate_text_local_claude_chat($token, $model, $aicontent, $temperature, $top_p, $vision_file, $max_tokens, $stream, $is_chat, $enabled_mcp_servers, $error); } } function aimogen_generate_text_local_claude_completion($token, $model, $aicontent, $temperature, $top_p, $max_tokens, $stream, $is_chat, &$error) { if(!aimogen_claude_local_extension_is_claude_model($model)) { $model = 'claude-sonnet-4-5-20250929'; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $prompt_tokens = count(aimogen_encode($aicontent)); if(aimogen_is_claude_model_200k($model)) { if(isset($aimogen_Main_Settings['claude_context_limit_200k']) && $aimogen_Main_Settings['claude_context_limit_200k'] != '') { if($prompt_tokens > intval($aimogen_Main_Settings['claude_context_limit_200k'])) { $aicontent = aimogen_strip_to_token_count($aicontent, $aimogen_Main_Settings['claude_context_limit_200k'], true); } } elseif($prompt_tokens > aimogen_get_max_input_tokens($model)) { $aicontent = aimogen_strip_to_token_count($aicontent, aimogen_get_max_input_tokens($model), true); } } elseif(aimogen_is_claude_model($model)) { if(isset($aimogen_Main_Settings['claude_context_limit']) && $aimogen_Main_Settings['claude_context_limit'] != '') { if($prompt_tokens > intval($aimogen_Main_Settings['claude_context_limit'])) { $aicontent = aimogen_strip_to_token_count($aicontent, $aimogen_Main_Settings['claude_context_limit'], true); } } elseif($prompt_tokens > aimogen_get_max_input_tokens($model)) { $aicontent = aimogen_strip_to_token_count($aicontent, aimogen_get_max_input_tokens($model), true); } } else { $error = 'Error: incorrect model provided: ' . $model; return false; } if($is_chat) { $aicontent = "\n\nHuman: " . $aicontent . "\n\nAssistant:"; } else { $aicontent = "\n\nHuman: Don't act as an assistant, reply strictly what you are asked. " . $aicontent . "\n\nAssistant:"; } $base_params = [ 'model' => $model, 'prompt' => $aicontent, 'temperature' => $temperature, 'top_p' => $top_p, 'stream' => false ]; if($is_chat) { $base_params['stop_sequences'] = array("\n\nUser:"); } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if (!isset($aimogen_Main_Settings['no_max']) || $aimogen_Main_Settings['no_max'] != 'on') { $base_params['max_tokens_to_sample'] = $max_tokens; } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API Claude payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode Claude API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterClaudeForStream_local'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( 'https://api.anthropic.com/v1/complete', array( 'headers' => [ 'accept' => 'application/json', 'anthropic-version' => '2023-06-01', 'content-type' => 'application/json', 'x-api-key' => $token, ], 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterClaudeForStream_local'); } $httpStatusCode = wp_remote_retrieve_response_code( $api_call ); if(is_wp_error( $api_call )) { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } if ($api_call === false) { $error = 'Claude curl error!'; return false; } elseif ($httpStatusCode >= 400) { $decodedResponse = json_decode($api_call['body'], true); $errorType = isset($decodedResponse['error']['type']) ? $decodedResponse['error']['type'] : 'unknown_error'; $errorMessage = isset($decodedResponse['error']['message']) ? $decodedResponse['error']['message'] : 'No specific error message provided'; switch ($httpStatusCode) { case 400: $error = "Claude Invalid request error: " . $errorMessage . " type: " . $errorType; return false; break; case 401: $error = "Claude Unauthorized error: " . $errorMessage . " type: " . $errorType; return false; break; case 403: $error = "Claude Forbidden error: " . $errorMessage . " type: " . $errorType; return false; break; case 404: $error = "Claude Not found error: " . $errorMessage . " type: " . $errorType; return false; break; case 429: $error = "Claude Rate limit exceeded: " . $errorMessage . " type: " . $errorType; return false; break; case 500: $error = "Claude Internal server error: " . $errorMessage . " type: " . $errorType; return false; break; case 529: $error = "Claude API overloaded error: " . $errorMessage . " type: " . $errorType; return false; break; default: $error = "Claude Error $httpStatusCode: " . $errorMessage . " type: " . $errorType; return false; break; } } else { $decodedResponse = json_decode( $api_call['body'], true); if($decodedResponse === null) { $error = 'Error: Failed to decode initial chat API response(5): ' . json_encode($api_call); return false; } if (isset($decodedResponse['error'])) { $error = 'Claude Error: ' . $decodedResponse['error']['message']; return false; } else { if(!isset($decodedResponse['completion'])) { $error = 'Claude Failed to parse response!' . json_encode($api_call['body']); return false; } return $decodedResponse['completion']; } } $error = 'Claude failure: text cannot be generated'; return false; } function aimogen_generate_text_local_claude_chat($token, $model, $aicontent, $temperature, $top_p, $vision_file, $max_tokens, $stream, $is_chat, $enabled_mcp_servers, &$error) { if(!aimogen_claude_local_extension_is_claude_model($model)) { $model = 'claude-3-5-sonnet-20240620'; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $prompt_tokens = count(aimogen_encode($aicontent)); if(aimogen_is_claude_model_200k($model)) { if(isset($aimogen_Main_Settings['claude_context_limit_200k']) && $aimogen_Main_Settings['claude_context_limit_200k'] != '') { if($prompt_tokens > intval($aimogen_Main_Settings['claude_context_limit_200k'])) { $aicontent = aimogen_strip_to_token_count($aicontent, $aimogen_Main_Settings['claude_context_limit_200k'], true); } } elseif($prompt_tokens > aimogen_get_max_input_tokens($model)) { $aicontent = aimogen_strip_to_token_count($aicontent, aimogen_get_max_input_tokens($model), true); } } elseif(aimogen_is_claude_model($model)) { if(isset($aimogen_Main_Settings['claude_context_limit']) && $aimogen_Main_Settings['claude_context_limit'] != '') { if($prompt_tokens > intval($aimogen_Main_Settings['claude_context_limit'])) { $aicontent = aimogen_strip_to_token_count($aicontent, $aimogen_Main_Settings['claude_context_limit'], true); } } elseif($prompt_tokens > aimogen_get_max_input_tokens($model)) { $aicontent = aimogen_strip_to_token_count($aicontent, aimogen_get_max_input_tokens($model), true); } } else { $error = 'Error: incorrect model provided: ' . $model; return false; } $chatgpt_obj = array(); if(!empty($vision_file)) { if($is_chat) { $claude_content = $aicontent; } else { $claude_content = "Don't act as an assistant, reply strictly what you are asked. " . $aicontent; } $base64_vision = ''; { $base64_vision = aimogen_get_base64_from_url($vision_file); } if(!empty($base64_vision)) { $xacontent = [ [ "type" => "text", "text" => $claude_content ], [ "type" => "image", "source" => [ "type" => "base64", "media_type" => "image/jpeg", "data" => $base64_vision ] ] ]; } else { $xacontent = [ "type" => "text", "text" => $claude_content ]; } $chatgpt_obj[] = array("role" => 'user', "content" => $xacontent); } else { if($is_chat) { $chatgpt_obj[] = array("role" => 'user', "content" => $aicontent); } else { $chatgpt_obj[] = array("role" => 'user', "content" => "Don't act as an assistant, reply strictly what you are asked. " . $aicontent); } } $max_tokens = aimogen_get_max_tokens($model); $base_params = [ 'model' => $model, 'messages' => $chatgpt_obj, 'temperature' => $temperature, 'top_p' => $top_p, 'stream' => false ]; if((int)$max_tokens > 0) { $base_params['max_tokens'] = $max_tokens; } $mcp_servers = get_option('aimogen_mcp_servers', array()); if (!empty($mcp_servers)) { $body['mcp_servers'] = array(); $enabled_ids = array(); if (!empty($enabled_mcp_servers)) { $enabled_ids = array_map('trim', explode(',', $enabled_mcp_servers)); } foreach ($mcp_servers as $id => $server) { if ($server['global'] != 'on' && !in_array($id, $enabled_ids)) { continue; } $server_label = $server['name'] . '_' . $id; $server_label = preg_replace('/[^a-zA-Z0-9_]/', '', $server_label); $server_label = preg_replace('/_{2,}/', '_', $server_label); if (!preg_match('/^[a-zA-Z]/', $server_label)) { $server_label = 'mcp_' . $server_label; } $mcp_config = array( 'type' => 'url', 'url' => $server['url'], 'name' => $server_label, 'description' => $server['description'], 'tool_configuration' => array( 'enabled' => true ) ); if (!empty($server['token'])) { $mcp_config['authorization_token'] = $server['token']; } $body['mcp_servers'][] = $mcp_config; } } if($is_chat) { $base_params['stop_sequences'] = array("\n\nUser:"); } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { $base_params['stream'] = true; } if($model == 'claude-opus-4-1-20250805' || $model == 'claude-sonnet-4-5-20250929') { if(isset($base_params['temperature']) && isset($base_params['top_p'])) { if($base_params['temperature'] == 1 && $base_params['top_p'] == 1) { unset($base_params['top_p']); } elseif($base_params['temperature'] == 1) { unset($base_params['top_p']); } elseif($base_params['top_p'] == 1) { unset($base_params['temperature']); } else { unset($base_params['top_p']); } } } try { $send_json = aimogen_safe_json_encode($base_params); } catch(Exception $e) { $error = 'Error: Exception in chat API Claude payload encoding: ' . $e->getMessage(); return false; } if($send_json === false) { $error = 'Error: Failed to encode Claude API payload: ' . json_encode($base_params); return false; } if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { add_action('http_api_curl', 'aimogen_filterClaudeForStream_local'); } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( 'https://api.anthropic.com/v1/messages', array( 'headers' => [ 'accept' => 'application/json', 'anthropic-version' => '2023-06-01', 'content-type' => 'application/json', 'x-api-key' => $token, ], 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if($stream === true && function_exists('curl_init') && extension_loaded("curl")) { remove_action('http_api_curl', 'aimogen_filterClaudeForStream_local'); } $httpStatusCode = wp_remote_retrieve_response_code( $api_call ); if(is_wp_error( $api_call )) { $error = 'Error: Failed to get initial chat API response: ' . json_encode($api_call); return false; } if ($api_call === false) { $error = 'Claude curl error!'; return false; } elseif ($httpStatusCode >= 400) { $decodedResponse = json_decode($api_call['body'], true); $errorType = isset($decodedResponse['error']['type']) ? $decodedResponse['error']['type'] : 'unknown_error'; $errorMessage = isset($decodedResponse['error']['message']) ? $decodedResponse['error']['message'] : 'No specific error message provided'; switch ($httpStatusCode) { case 400: $error = "Claude Invalid messages request error: " . $errorMessage . " type: " . $errorType; return false; break; case 401: $error = "Claude Unauthorized error: " . $errorMessage . " type: " . $errorType; return false; break; case 403: $error = "Claude Forbidden error: " . $errorMessage . " type: " . $errorType; return false; break; case 404: $error = "Claude Not found error: " . $errorMessage . " type: " . $errorType; return false; break; case 429: $error = "Claude Rate limit exceeded: " . $errorMessage . " type: " . $errorType; return false; break; case 500: $error = "Claude Internal server error: " . $errorMessage . " type: " . $errorType; return false; break; case 529: $error = "Claude API overloaded error: " . $errorMessage . " type: " . $errorType; return false; break; default: $error = "Claude Error $httpStatusCode: " . $errorMessage . " type: " . $errorType; return false; break; } } else { $decodedResponse = json_decode( $api_call['body'], true); if($decodedResponse === null) { $error = 'Error: Failed to decode initial chat API response(6): ' . json_encode($api_call); return false; } if (isset($decodedResponse['error'])) { $error = 'Claude Error: ' . $decodedResponse['error']['message']; return false; } else { if(!isset($decodedResponse['content'][0]['text'])) { $error = 'Claude Failed to parse chat response!' . json_encode($api_call['body']); return false; } return $decodedResponse['content'][0]['text']; } } $error = 'Claude failure: text cannot be generated'; return false; } function aimogen_generate_text_openrouter($token, $model, $aicontent, $temperature, $top_p, $vision_file, $max_tokens, $stream, &$error) { $token = apply_filters('aimogen_openrouter_api_key', $token); if(!aimogen_is_openrouter_model($model)) { $model = 'openrouter/auto'; } if(is_string($aicontent)) { $prompt_tokens = count(aimogen_encode($aicontent)); if($prompt_tokens > aimogen_get_max_input_tokens_openrouter($model)) { $aicontent = aimogen_strip_to_token_count($aicontent, aimogen_get_max_input_tokens_openrouter($model), true); } $role = 'user'; $chatgpt_obj = array(); if(!empty($vision_file)) { $base64_vision = ''; if(!empty($base64_vision)) { $xacontent = [ [ "type" => "text", "text" => $aicontent ], [ "type" => "image_url", "image_url" => [ "url" => "data:image/jpeg;base64," . $base64_vision ] ] ]; } else { $xacontent = [ [ "type" => "text", "text" => $aicontent ], [ "type" => "image_url", "image_url" => [ "url" => $vision_file ] ] ]; } $chatgpt_obj[] = array("role" => $role, "content" => $xacontent); } else { $chatgpt_obj[] = array("role" => $role, "content" => $aicontent); } } else { $chatgpt_obj = $aicontent; } $base_params = [ 'model' => $model, 'messages' => $chatgpt_obj, 'temperature' => $temperature, 'top_p' => $top_p ]; if($stream == true) { $base_params['stream'] = true; } if((int)$max_tokens > 0) { $base_params['max_tokens'] = $max_tokens; } require_once (dirname(__FILE__) . "/res/openrouter/UrlOpenRouter.php"); require_once (dirname(__FILE__) . "/res/openrouter/OpenRouter.php"); $open_ai = new OpenRouter($token); if($stream == true) { $responsex = $open_ai->chat($base_params, function ($curl_info, $chunk) { // unescaped output is needed because of the streaming format // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $chunk . "

"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo PHP_EOL; ob_flush(); flush(); return strlen($chunk); }); $result = json_decode(trim($responsex)); return $result; } else { $responsex = $open_ai->chat($base_params); $result = json_decode(trim($responsex)); if (isset($result->error) && !empty($result->error)) { $error = 'Error in OpenRouter API: ' . $result->error->message; return false; } else { if(!isset($result->choices[0]->message->content)) { $error = 'Failed to parse API response: ' . trim($responsex); return false; } return $result->choices[0]->message->content; } $error = 'OpenRouter failure: text cannot be generated'; return false; } } function aimogen_generate_text_ollama($api_url, $model, $aicontent, $zenv, $temperature, $top_p, $functions, $vision_file, $max_tokens, $stream, &$error) { $api_url = apply_filters('aimogen_ollama_url', $api_url); if(!empty($functions)) { $stream = false; } $response = ''; try { $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'text'; $maxResults = 1; $aicontentquery = ''; $token = ''; if(is_array($aicontent)) { $aicontentquery = $aicontent[0]['content']; if(is_array($aicontentquery) && isset($aicontentquery[0]['content'])) { $aicontentquery = $aicontentquery[0]['content']; } } $query = new Aimogen_lite_Query($aicontentquery, $max_tokens, $model, $temperature, $stop, $zenv, $mode, $token, $session, $maxResults, '', ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = $ok; return false; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating chat AI text using model: ' . $model . ' and prompt: ' . $aicontent); } $env = [ 'temperature' => $temperature, 'top_p' => $top_p ]; require_once (dirname(__FILE__) . "/res/ollama/ollama.php"); $ollama = new AimogenOllamaAPI($api_url); try { $response = $ollama->chatCompletion($model, $aicontent, $stream, $env, $functions, false, $vision_file); if(!$stream && empty($response)) { throw new Exception('Empty response from Ollama'); } } catch (Exception $e) { throw new Exception('Failed to generate Ollama API response: ' . $e->getMessage()); } if(!$stream) { $response = apply_filters( 'aimogen_ai_reply_raw', $response, $aicontent ); if(!isset($response['message']['content'])) { throw new Exception('Incorrect Ollama response: ' . json_encode($response)); } if(isset($response['message']['tool_calls']) && is_array($response['message']['tool_calls']) && count($response['message']['tool_calls']) > 0) { $toolOutputs = aimogen_call_ollama_tool($response); if(!empty($toolOutputs)) { try { $response = $ollama->chatCompletion($model, $aicontent, $stream, $env, $functions, $toolOutputs, $vision_file); if(!$stream && empty($response)) { throw new Exception('Empty response from Ollama function call'); } $response = apply_filters( 'aimogen_ai_reply_raw', $response, $aicontent ); if(!isset($response['message']['content'])) { throw new Exception('Incorrect Ollama function call response: ' . json_encode($response)); } } catch (Exception $e) { throw new Exception('Failed to generate Ollama API response: ' . $e->getMessage()); } } } $response = $response['message']['content']; apply_filters( 'aimogen_ai_reply', $response, $query ); } } catch (Exception $e) { $error = 'Ollama failure: ' . $e->getMessage(); return false; } return $response; } function aimogen_generate_text_huggingface($token, $model, $aicontent, $zenv, $temperature, $top_p, $max_tokens, $stream, &$error) { $token = apply_filters('aimogen_huggingface_api_key', $token); $response = ''; try { $custom_url = ''; $all_models = get_option('aimogen_huggingface_models', array()); if(!array_key_exists($model, $all_models)) { throw new Exception('Inexistent HuggingFace model provided: ' . $model); } else { if(is_array($all_models[$model]) && isset($all_models[$model][1]) && trim($all_models[$model][1]) != '') { $custom_url = trim($all_models[$model][1]); } } $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'text'; $maxResults = 1; if(is_array($aicontent)) { $aicontent = $aicontent[0]['content']; } $query = new Aimogen_lite_Query($aicontent, $max_tokens, $model, $temperature, $stop, $zenv, $mode, $token, $session, $maxResults, '', ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = $ok; return false; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating chat AI text using model: ' . $model . ' and prompt: ' . $aicontent); } require_once (dirname(__FILE__) . "/res/huggingface/api.php"); $env = [ 'apikey' => $token, 'model' => $model, 'temperature' => $temperature, 'top_p' => $top_p ]; $extra = array(); if($stream == true) { $env['stream'] = true; $extra['stream'] = true; } else { $env['stream'] = false; } $sdk = new AimogenHuggingFaceSDK($env, $custom_url); $response = $sdk->generate_text($aicontent, $extra); if(!$stream && empty($response)) { throw new Exception('Failed to generate API response!'); } if(!isset($response[0]['generated_text'])) { throw new Exception('Failed to parse HuggingFace response: ' . json_encode($response)); } if(!$stream) { $response = $response[0]['generated_text']; $pos = strpos($response, $aicontent); if ($pos !== false) { $response = substr_replace($response, '', $pos, strlen($aicontent)); } $response = apply_filters( 'aimogen_ai_reply_raw', $response, $aicontent ); apply_filters( 'aimogen_ai_reply', $response, $query ); } } catch (Exception $e) { $error = 'HuggingFace failure: ' . $e->getMessage(); return false; } return $response; } function aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, $retry_count, &$error, $image_model = 'dalle2', $file_name = '') { $token = apply_filters('aimogen_openai_api_key', $token); $is_allowed = apply_filters('aimogen_is_ai_image_allowed', true, $prompt); if ( $is_allowed !== true ) { $error = is_string( $is_allowed ) ? $is_allowed : esc_html__('You are not allowed to do this query', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $image_model = trim($image_model); if(!in_array($image_model, AIMOGEN_DALLE_IMAGE_MODELS)) { $image_model = 'dalle2'; } if(empty($file_name)) { $file_name = $prompt; } $prompt = apply_filters('aimogen_modify_ai_image_query', $prompt); $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'image'; $maxResults = 1; $temperature = 1; if(($size == '1792x1024' || $size == '1024x1792') && stristr($image_model, 'dall') === false) { if($size == '1792x1024') { $size == '1536x1024'; } elseif($size == '1024x1792') { $size == '1024x1536'; } } $query = new Aimogen_lite_Query($prompt, 4000, $image_model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, $size, ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = $ok; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $delay = ''; if(!isset($aimogen_Main_Settings['no_img_translate']) || $aimogen_Main_Settings['no_img_translate'] != 'on') { $text_trans = aimogen_translate_stability($prompt); if($text_trans != $prompt && !empty($text_trans)) { aimogen_log_to_file('Dall-E prompt translated from: "' . $prompt . '" to: "' . $text_trans . '"'); $prompt = $text_trans; } } $prompt = trim($prompt); if($prompt == '') { $error = esc_html__('Empty prompt added to image generator', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if($image_model == 'dalle2') { if(strlen($prompt) > 1000) { $prompt = aimogen_substr($prompt, 0, 1000); } } else { if(stristr($image_model, 'dall') !== false) { if(strlen($prompt) > 4000) { $prompt = aimogen_substr($prompt, 0, 4000); } } else { if(strlen($prompt) > 32000) { $prompt = aimogen_substr($prompt, 0, 32000); } } } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating AI Image using prompt: ' . $prompt); } if (isset($aimogen_Main_Settings['request_delay']) && $aimogen_Main_Settings['request_delay'] != '') { if(stristr($aimogen_Main_Settings['request_delay'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay'])); } } } if($delay != '' && is_numeric($delay)) { usleep($delay); } else { if (isset($aimogen_Main_Settings['request_delay_dalle']) && $aimogen_Main_Settings['request_delay_dalle'] != '') { if(stristr($aimogen_Main_Settings['request_delay_dalle'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay_dalle']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay_dalle']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay_dalle'])); } } } if($delay != '' && is_numeric($delay)) { usleep($delay); } } if($image_model == 'dalle2') { if($size != '256x256' && $size != '512x512' && $size != '1024x1024') { $size = '512x512'; } } else { if($size != '1792x1024' && $size != '1024x1792' && $size != '1024x1024' && stristr($image_model, 'dall') !== false) { $size = '1024x1024'; } } $is_hd = false; if($image_model == 'dalle3hd') { $is_hd = true; } if($image_model == 'dalle2') { $image_model = 'dall-e-2'; } elseif($image_model == 'dalle3' || $image_model == 'dalle3hd') { $image_model = 'dall-e-3'; $number = 1; } $return_arr = array(); if(aimogen_is_aimogen_key($token)) { $send_model = $image_model; if($is_hd == true) { $send_model .= '-hd'; } $pargs = array(); $api_url = 'https://aiomaticapi.com/apis/ai/v1/image/'; $pargs['prompt'] = trim($prompt); $pargs['model'] = trim($send_model); $pargs['image_size'] = $size; if ($image_model != 'dall-e-2' && stristr($image_model, 'dall') !== false && isset($aimogen_Main_Settings['dalle_style']) && $aimogen_Main_Settings['dalle_style'] != '') { $pargs['style'] = $aimogen_Main_Settings['dalle_style']; } $api_headers = array( 'Authorization' => 'Bearer ' . trim($token) ); $ai_response = aimogen_get_web_page_api($api_url, $pargs, $api_headers); if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial AimogenAPI response: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Error: Failed to get AimogenAPI image response!'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } $ai_json = json_decode($ai_response); if($ai_json === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode AimogenAPI response: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Error: Failed to decode AimogenAPI image response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(isset($ai_json->error)) { if (stristr($ai_json->error, 'Your subscription expired, please renew it.') === false && stristr($ai_json->error, '[RATE LIMITED]') === false && isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after error AimogenAPI response: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Error while processing AI image response: ' . $ai_json->error; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(!isset($ai_json->result)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after result AimogenAPI response: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Error: Failed to parse AimogenAPI image response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(stristr($image_model, 'dall') !== false) { $ai_json = apply_filters( 'aimogen_dalle_reply_raw', $ai_json, $prompt ); apply_filters( 'aimogen_ai_reply', $ai_json->result, $query ); if(isset($ai_json->remainingtokens)) { set_transient('aimogenapi_tokens', $ai_json->remainingtokens, 86400); } if (isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $nocopy === false) { $attached_id = ''; $localpath = aimogen_copy_image_locally($ai_json->result, $attached_id, '', false, $file_name); if($localpath !== false) { $return_arr[] = $localpath[0]; } else { $return_arr[] = $ai_json->result; } } else { $return_arr[] = $ai_json->result; } } else { apply_filters( 'aimogen_ai_reply', $ai_json->result, $query ); if(isset($ai_json->remainingtokens)) { set_transient('aimogenapi_tokens', $ai_json->remainingtokens, 86400); } $img = $ai_json->result; $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($prompt, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $file_name); if($localpath !== false) { $rezi = $localpath; } } } $return_arr[] = $rezi[1]; } } elseif (aimogen_check_if_azure($aimogen_Main_Settings)) { if (!isset($aimogen_Main_Settings['azure_endpoint']) || trim($aimogen_Main_Settings['azure_endpoint']) == '') { $error = 'You need to enter an Azure Endpoint for this to work!'; return false; } if($image_model == 'dall-e-3' || $image_model == 'gpt-image-1' || $image_model == 'gpt-image-1-mini') { $localAzureDeployments = array(); $depl_arr = aimogen_get_deployments($token); if(is_array($depl_arr)) { foreach($depl_arr as $dar) { if(empty($dar)) { continue; } if(is_string($dar)) { $localAzureDeployments[trim($dar)] = trim($dar); } else { $localAzureDeployments[trim($dar->model)] = trim($dar->id); } } } $azureDeployment = ''; foreach ( $localAzureDeployments as $dmodel => $dname ) { if ( $dmodel === str_replace('.', '', $image_model) || $dmodel === $image_model ) { $azureDeployment = $dname; break; } } if ( $azureDeployment == '' ) { $new_dep = aimogen_update_deployments_azure($token); if($new_dep !== false) { $localAzureDeployments = array(); foreach($new_dep as $dar) { if(empty($dar)) { continue; } if(is_string($dar)) { $localAzureDeployments[trim($dar)] = trim($dar); } else { $localAzureDeployments[trim($dar->model)] = trim($dar->id); } } foreach ( $localAzureDeployments as $dmodel => $dname ) { if ( $dmodel === str_replace('.', '', $image_model) || $dmodel === $image_model ) { $azureDeployment = $dname; break; } } } if ( $azureDeployment == '' ) { $error = 'No added Azure deployment found for image model: ' . $image_model . ' - you need to add this model in your Azure Portal as a Deployment'; return false; } } if (isset($aimogen_Main_Settings['azure_api_selector_dalle3']) && $aimogen_Main_Settings['azure_api_selector_dalle3'] != '' && $aimogen_Main_Settings['azure_api_selector_dalle3'] != 'default') { $api_ver = '?api-version=' . $aimogen_Main_Settings['azure_api_selector_dalle3']; } else { $api_ver = AIMOGEN_AZURE_DALLE3_API_VERSION; } $apiurl = trailingslashit(trim($aimogen_Main_Settings['azure_endpoint'])) . 'openai/deployments/' . $azureDeployment . '/images/generations' . $api_ver; } else { if (isset($aimogen_Main_Settings['azure_api_selector_dalle2']) && $aimogen_Main_Settings['azure_api_selector_dalle2'] != '' && $aimogen_Main_Settings['azure_api_selector_dalle2'] != 'default') { $api_ver = '?api-version=' . $aimogen_Main_Settings['azure_api_selector_dalle2']; } else { $api_ver = AIMOGEN_AZURE_DALLE_API_VERSION; } $apiurl = trailingslashit(trim($aimogen_Main_Settings['azure_endpoint'])) . 'openai/images/generations:submit' . $api_ver; } //https://learn.microsoft.com/en-us/azure/ai-services/openai/dall-e-quickstart?tabs=dalle3%2Ccommand-line&pivots=rest-api try { $jsarrx = [ 'prompt' => $prompt, 'n' => 1, 'size' => $size ]; if($is_hd == true) { $jsarrx['quality'] = 'hd'; } if ($image_model != 'dall-e-2' && stristr($image_model, 'dall') !== false && isset($aimogen_Main_Settings['dalle_style']) && $aimogen_Main_Settings['dalle_style'] != '') { $jsarrx['style'] = $aimogen_Main_Settings['dalle_style']; } $send_json = aimogen_safe_json_encode($jsarrx); } catch(Exception $e) { $error = 'Error: Exception in API payload encoding: ' . $e->getMessage(); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if($send_json === false) { $error = 'Error: Failed to encode API payload: ' . $prompt; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } add_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_post( $apiurl, array( 'headers' => array( 'Content-Type' => 'application/json', 'api-key' => $token ), 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial DALLE response: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Failed to get DallE API response: ' . json_encode($api_call); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $result = json_decode( $api_call['body'] ); if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode DALLE response: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Failed to decode initial DallE API response: ' . json_encode($api_call); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'You exceeded your Azure OpenAI quota limit for image generator, please wait a period for the quota to refill (initial call).'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the image API, result: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $image_model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after type DALLE response: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'An error occurred when initially calling OpenAI API, no type found: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } } if($image_model == 'dall-e-3') { if(!isset($result->created) || !isset($result->data)) { delete_option('aimogen_deployments_list'); if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after incorrect DALLE3 response: ' . json_encode($result)); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Incorrect response format for Azure Dall-E3 images: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $final_url = $result->data[0]->url; $result = apply_filters( 'aimogen_dalle_reply_raw', $result, $prompt ); apply_filters( 'aimogen_ai_reply', $final_url, $query ); if (isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $nocopy === false) { $attached_id = ''; $localpath = aimogen_copy_image_locally($final_url, $attached_id, '', false, $file_name); if($localpath !== false) { $return_arr[] = $localpath[0]; } else { $return_arr[] = $final_url; } } else { $return_arr[] = $final_url; } } } elseif($image_model == 'gpt-image-1') { if(!isset($result->created) || !isset($result->data)) { delete_option('aimogen_deployments_list'); if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after incorrect gpt-image-1 response: ' . json_encode($result)); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Incorrect response format for Azure gpt-image-1 images: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $img = $result->data[0]->b64_json; apply_filters( 'aimogen_ai_reply', $img, $query ); $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($file_name, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $file_name); if($localpath !== false) { $rezi = $localpath; } } } if(isset($rezi[1])) { $return_arr[] = $rezi[1]; } } } elseif($image_model == 'gpt-image-1-mini') { if(!isset($result->created) || !isset($result->data)) { delete_option('aimogen_deployments_list'); if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after incorrect gpt-image-1-mini response: ' . json_encode($result)); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Incorrect response format for Azure gpt-image-1-mini images: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $img = $result->data[0]->b64_json; apply_filters( 'aimogen_ai_reply', $img, $query ); $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($file_name, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $file_name); if($localpath !== false) { $rezi = $localpath; } } } if(isset($rezi[1])) { $return_arr[] = $rezi[1]; } } } else { if(!isset($result->id) || !isset($result->status)) { delete_option('aimogen_deployments_list'); if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after incorrect DALLE response: ' . json_encode($result)); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Incorrect response format for Azure Dall-E images: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $retry_after = 2; $retry_url = wp_remote_retrieve_header( $api_call, 'Operation-Location' ); if(empty($retry_url)) { $error = 'Failed to find the Operation-Location header: ' . json_encode($api_call); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $final_url = ''; $max_wait = 300; $waited = 0; while($final_url == '') { if($waited > $max_wait) { $error = 'Timeout for image generator in Azure DallEAPI: ' . $prompt; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } sleep($retry_after); $waited += $retry_after; remove_action('http_api_curl', 'aimogen_add_proxy'); $api_call = wp_remote_get( $retry_url, array( 'headers' => array( 'Content-Type' => 'application/json', 'api-key' => $token ), 'timeout' => 30, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if(is_wp_error( $api_call )) { $error = 'Failed to get Azure DallE API Image URL response: ' . json_encode($api_call); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } else { $result = json_decode( $api_call['body'] ); if($result === null) { $error = 'Failed to decode Azure DallE API Image URL response: ' . json_encode($api_call['body']); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(isset($result->status) && $result->status == 'succeeded') { if(isset($result->result->data) && $result->result->data[0]->url != '') { $final_url = $result->result->data[0]->url; } else { $error = 'Corrupted response from Azure DallE API Image URL: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } } } if($final_url == '') { $error = 'Failed to generate Azure DallE API Image URL for prompt: ' . $prompt; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $result = apply_filters( 'aimogen_dalle_reply_raw', $result, $prompt ); apply_filters( 'aimogen_ai_reply', $final_url, $query ); if (isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $nocopy === false) { $attached_id = ''; $localpath = aimogen_copy_image_locally($final_url, $attached_id, '', false, $file_name); if($localpath !== false) { $return_arr[] = $localpath[0]; } else { $return_arr[] = $final_url; } } else { $return_arr[] = $final_url; } } } } } } else { try { $send_arr = [ 'n' => intval($number), 'prompt' => $prompt, 'size' => $size, 'model' => $image_model ]; if(stristr($image_model, 'dall') !== false) { $send_arr['response_format'] = 'url'; } if($is_hd === true) { $send_arr['quality'] = 'hd'; } if ($image_model != 'dall-e-2' && stristr($image_model, 'dall') !== false && isset($aimogen_Main_Settings['dalle_style']) && $aimogen_Main_Settings['dalle_style'] != '') { $send_arr['style'] = $aimogen_Main_Settings['dalle_style']; } $send_json = aimogen_safe_json_encode( $send_arr ); } catch(Exception $e) { $error = 'Error: Exception in API payload encoding: ' . $e->getMessage(); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if($send_json === false) { $error = 'Error: Failed to encode API payload: ' . $prompt; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } add_action('http_api_curl', 'aimogen_add_proxy'); $xh = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ); if (isset($aimogen_Main_Settings['openai_organization']) && $aimogen_Main_Settings['openai_organization'] != '') { $xh['OpenAI-Organization'] = $aimogen_Main_Settings['openai_organization']; } if (isset($aimogen_Main_Settings['api_selector']) && trim($aimogen_Main_Settings['api_selector']) == 'custom' && isset($aimogen_Main_Settings['custom_endpoint']) && trim($aimogen_Main_Settings['custom_endpoint'], ' /') != '') { $api_endpoint = trim($aimogen_Main_Settings['custom_endpoint'], ' /'); } else { $api_endpoint = 'https://api.openai.com'; } $api_call = wp_remote_post( $api_endpoint . '/v1/images/generations', array( 'headers' => $xh, 'body' => $send_json, 'method' => 'POST', 'data_format' => 'body', 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, ) ); remove_action('http_api_curl', 'aimogen_add_proxy'); if(is_wp_error( $api_call )) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after initial DALLE response: ' . json_encode($api_call)); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Failed to get DallE API response: ' . json_encode($api_call); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $result = json_decode( $api_call['body'] ); if($result === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after decode DALLE response: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'Failed to decode initial DallE API response: ' . json_encode($api_call); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $is_error = false; $sleep_time = false; if(isset($result->error)) { $result = $result->error; $is_error = true; } if($is_error && isset($result->type)) { if($result->type == 'insufficient_quota') { $error = 'You exceeded your OpenAI quota limit for images. To fix this, if you are using a free OpenAI account, you need to add a VISA card to your account, as OpenAI heavily limits free accounts. Please check details here: https://platform.openai.com/docs/guides/rate-limits'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } elseif($result->type == 'invalid_request_error') { $error = 'Error: Invalid request submitted to the image API, result: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } else { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { if(isset($result->code) && $result->code == 'rate_limit_exceeded') { $errmessage = $result->message; preg_match_all('#Rate limit reached for.*?in organization.*?Please try again in ([\d.]*?)s#i', $errmessage, $htmlrez); if(isset($htmlrez[1][0])) { $sleep_time = ceil(floatval($htmlrez[1][0])); if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Rate limit reached for model: ' . $image_model . ', sleeping for: ' . $sleep_time . ' seconds'); } sleep($sleep_time); } } aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after type DALLE response: ' . json_encode($api_call['body'])); if($sleep_time === false) { sleep(pow(2, $retry_count)); } $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'An error occurred when initially calling OpenAI API, no type found: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } } if(!isset($result->data)) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') API call after data DALLE response: ' . json_encode($api_call['body'])); sleep(pow(2, $retry_count)); $image_model = apply_filters('aimogen_model_fallback', $image_model, $error, $prompt); $prompt = apply_filters('aimogen_aicontent_try_fix', $prompt, $error, $image_model); return aimogen_generate_ai_image($token, $number, $prompt, $size, $env, $nocopy, intval($retry_count) + 1, $error, $image_model, $file_name); } else { $error = 'An error occurred when initially calling OpenAI data API: ' . json_encode($result); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $result = apply_filters( 'aimogen_dalle_reply_raw', $result, $prompt ); foreach($result->data as $rdata) { if(isset($rdata->b64_json)) { $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($prompt, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $img = $rdata->b64_json; apply_filters( 'aimogen_ai_reply', $img, $query ); $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $file_name); if($localpath !== false) { $rezi = $localpath; } } } if(isset($rezi[1])) { $return_arr[] = $rezi[1]; } } else { apply_filters( 'aimogen_ai_reply', $rdata->url, $query ); if (isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $nocopy === false) { $attached_id = ''; $localpath = aimogen_copy_image_locally($rdata->url, $attached_id, '', false, $file_name); if($localpath !== false) { $return_arr[] = $localpath[0]; } else { $return_arr[] = $rdata->url; } } else { $return_arr[] = $rdata->url; } } } } } } } return $return_arr; } function aimogen_generate_ai_image_midjourney($prompt, $width, $height, $env, $nocopy, &$error) { $return_url = false; $is_allowed = apply_filters('aimogen_is_ai_image_allowed', true, $prompt); if ( $is_allowed !== true ) { $error = is_string( $is_allowed ) ? $is_allowed : esc_html__('You are not allowed to do this query', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if (!isset($aimogen_Main_Settings['midjourney_app_id']) || trim($aimogen_Main_Settings['midjourney_app_id']) == '') { $error = 'GoAPI API key is needed to be entered for this feature to work!'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['midjourney_app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_goapi_api_key', $token); if (!isset($aimogen_Main_Settings['midjourney_image_model']) || trim($aimogen_Main_Settings['midjourney_image_model']) == '') { $image_model = 'fast'; } else { $image_model = trim($aimogen_Main_Settings['midjourney_image_model']); } if (!isset($aimogen_Main_Settings['midjourney_image_engine']) || trim($aimogen_Main_Settings['midjourney_image_engine']) == '') { $image_engine = 'midjourney'; } else { $image_engine = trim($aimogen_Main_Settings['midjourney_image_engine']); } $prompt = apply_filters('aimogen_modify_ai_image_query', $prompt); $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'image'; $maxResults = 1; $temperature = 1; $query = new Aimogen_lite_Query($prompt, 1000, $image_model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, $width . 'x' . $height, ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = $ok; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $delay = ''; if(!isset($aimogen_Main_Settings['no_img_translate']) || $aimogen_Main_Settings['no_img_translate'] != 'on') { $text_trans = aimogen_translate_stability($prompt); if($text_trans != $prompt && !empty($text_trans)) { aimogen_log_to_file('Midjourney prompt translated from: "' . $prompt . '" to: "' . $text_trans . '"'); $prompt = $text_trans; } } $prompt = trim($prompt); if($prompt == '') { $error = esc_html__('Empty prompt added to image generator', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(strlen($prompt) > 30000) { $prompt = aimogen_substr($prompt, 0, 30000); } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating AI Image using GoAPI Midjourney prompt: ' . $prompt); } if (isset($aimogen_Main_Settings['request_delay']) && $aimogen_Main_Settings['request_delay'] != '') { if(stristr($aimogen_Main_Settings['request_delay'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay'])); } } } if($delay != '' && is_numeric($delay)) { usleep($delay); } else { if (isset($aimogen_Main_Settings['request_delay_dalle']) && $aimogen_Main_Settings['request_delay_dalle'] != '') { if(stristr($aimogen_Main_Settings['request_delay_dalle'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay_dalle']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay_dalle']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay_dalle'])); } } } if($delay != '' && is_numeric($delay)) { usleep($delay); } } $imagineUrl = 'https://api.goapi.ai/api/v1/task'; $args = [ 'method' => 'POST', 'headers' => [ 'x-api-key' => $token, 'Content-Type' => 'application/json' ], 'body' => '', 'timeout' => 300, 'sslverify' => false ]; $aspect_ratio = $width > $height ? '16:9' : ($width < $height ? '9:16' : '1:1'); if ($image_engine === 'midjourney') { $input_arr = [ "prompt" => $prompt, "aspect_ratio" => $aspect_ratio, "process_mode" => $image_model, "skip_prompt_check" => true ]; $task_type = 'imagine'; } else { $input_arr = [ "prompt" => $prompt, "width" => (int)$width, "height" => (int)$height ]; $task_type = 'txt2img'; } $args['body'] = json_encode([ "model" => $image_engine, "task_type" => $task_type, "input" => $input_arr ]); $response = wp_remote_post($imagineUrl, $args); if (is_wp_error($response)) { $error = esc_html__('Failed to execute the Midjourney task!', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $body = wp_remote_retrieve_body($response); $body = str_replace('"', '"', $body); $task = json_decode($body, true); $taskId = $task['data']['task_id'] ?? ''; if (!$taskId) { $error = esc_html__('Failed to create the Midjourney task!', 'aimogen') . ' ' . json_encode($body); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $fetchUrl = "https://api.goapi.ai/api/v1/task/$taskId"; $fetch_args = [ 'method' => 'GET', 'headers' => ['x-api-key' => $token], 'timeout' => 300, 'sslverify' => false ]; $start_time = time(); $fetchResult = ["data" => ["status" => "NotStarted"]]; while (!in_array($fetchResult["data"]["status"], ["completed","failed"], true) && time() - $start_time < 300) { $r = wp_remote_get($fetchUrl, $fetch_args); if (is_wp_error($r)) { $error = esc_html__('Failed to check the Midjourney task!', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $fetchResponse = wp_remote_retrieve_body($r); $fetchResult = json_decode(str_replace('"', '"', $fetchResponse), true); if (!is_array($fetchResult)) $fetchResult = []; sleep(4); } if (($fetchResult["data"]["status"] ?? '') !== "completed") { $error = esc_html__('Error during GoAPI fetch call: ', 'aimogen') . json_encode($fetchResult); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if ($image_engine === 'midjourney') { $up_args = [ 'method' => 'POST', 'headers' => [ 'X-API-Key' => $token, 'Content-Type' => 'application/json' ], 'timeout' => 300, 'sslverify' => false, 'body' => json_encode([ "model" => "midjourney", "task_type" => "upscale", "input" => [ "origin_task_id" => $taskId, "index" => "1" ] ]) ]; $upscaleResponse = wp_remote_post($imagineUrl, $up_args); if (is_wp_error($upscaleResponse)) { $error = esc_html__('Failed to execute the Midjourney upscale task!', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $upBody = wp_remote_retrieve_body($upscaleResponse); $upBody = str_replace('"', '"', $upBody); $upscaleTask = json_decode($upBody, true); $upscaleTaskId = $upscaleTask["data"]["task_id"] ?? ''; if (!$upscaleTaskId) { $error = esc_html__('Failed to get upscale API response', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $fetchUrl = "https://api.goapi.ai/api/v1/task/$upscaleTaskId"; $fetchResult = ["data" => ["status" => "NotStarted"]]; $start_time = time(); while (!in_array($fetchResult["data"]["status"], ["completed","failed"], true) && time() - $start_time < 300) { $r = wp_remote_get($fetchUrl, $fetch_args); if (is_wp_error($r)) { $error = esc_html__('Failed to check the Midjourney task!', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $fetchResponse = wp_remote_retrieve_body($r); $fetchResponse = str_replace('"', '"', $fetchResponse); $fetchResult = json_decode($fetchResponse, true); if (!is_array($fetchResult)) $fetchResult = []; sleep(4); } if (($fetchResult["data"]["status"] ?? '') !== "completed" || empty($fetchResult["data"]["output"]["image_url"])) { $error = esc_html__('Error during GoAPI call: ', 'aimogen') . json_encode($fetchResult); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } apply_filters('aimogen_ai_reply', $fetchResult["data"]["output"]["image_url"], $query); if ( isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] !== 'disabled' && !$nocopy ) { $attached_id = ''; $localpath = aimogen_copy_image_locally($fetchResult["data"]["output"]["image_url"], $attached_id, '', false, $prompt); $return_url = $localpath !== false ? $localpath[0] : $fetchResult["data"]["output"]["image_url"]; } else { $return_url = $fetchResult["data"]["output"]["image_url"]; } return $return_url; } function aimogen_generate_stability_image($text = '', $height = '512', $width = '512', $env = '', $retry_count = 0, $returnbase64 = false, &$error = '', $nolocal = false, $stable_model = false, $source_image_url = '', $image_strength = 0.5) { $is_allowed = apply_filters('aimogen_is_ai_image_allowed', true, $text); if ( $is_allowed !== true ) { $error = is_string( $is_allowed ) ? $is_allowed : esc_html__('You are not allowed to do this query', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if($source_image_url != '') { if($stable_model == 'stable-diffusion-ultra' || $stable_model == 'stable-diffusion-3-0-turbo' || $stable_model == 'stable-diffusion-3-0-large' || $stable_model == 'stable-diffusion-3-0-medium' || $stable_model == 'sd3.5-large' || $stable_model == 'sd3.5-large-turbo' || $stable_model == 'sd3.5-medium') {} else { //care if you change this to AIMOGEN_STABLE_DEFAULT_MODE, as not all stability ai endpoints allow image sending $stable_model == 'sd3.5-large-turbo'; } } $text = apply_filters('aimogen_modify_ai_image_query', $text); $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if(empty($stable_model)) { if (!isset($aimogen_Main_Settings['stable_model']) || trim($aimogen_Main_Settings['stable_model']) == '') { $stable_model = AIMOGEN_STABLE_DEFAULT_MODE; } else { $stable_model = trim($aimogen_Main_Settings['stable_model']); } } if (!isset($aimogen_Main_Settings['stability_app_id']) || trim($aimogen_Main_Settings['stability_app_id']) == '') { $error = 'You need to enter a Stability.AI API key in the plugin\'s "Settings" menu to use this feature!'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['stability_app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_stability_api_key', $token); $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'stable'; $maxResults = 1; $available_tokens = 1000; $temperature = 1; if($stable_model == 'stable-diffusion-xl-beta-v2-2-2') { if(intval($width) > 512) { $width = 512; } if(intval($height) > 512) { $height = 512; } } if(!isset($aimogen_Main_Settings['no_img_translate']) || $aimogen_Main_Settings['no_img_translate'] != 'on') { $text_trans = aimogen_translate_stability($text); if($text_trans != $text && !empty($text_trans)) { aimogen_log_to_file('Stability.ai prompt translated from: "' . $text . '" to: "' . $text_trans . '"'); $text = $text_trans; } } if(strlen($text) > 2000) { $text = aimogen_substr($text, 0, 2000); } $query = new Aimogen_lite_Query($text, $available_tokens, $stable_model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, $width . 'x' . $height, ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = 'Image generator is rate limited: ' . $ok; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Generating Stability.AI Image using prompt: ' . $text . ' height: ' . $height . ' width: ' . $width); } if(intval($height) < 300) { $error = 'Invalid height (>300): ' . $height; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(intval($width) < 300 ) { $error = 'Invalid width (>300): ' . $width; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating AI Stable Difussion image using model: ' . $stable_model . ' and prompt: ' . $text); } if (!isset($aimogen_Main_Settings['steps']) || trim($aimogen_Main_Settings['steps']) == '') { $steps = '50'; } else { $steps = trim($aimogen_Main_Settings['steps']); } if (!isset($aimogen_Main_Settings['cfg_scale']) || trim($aimogen_Main_Settings['cfg_scale']) == '') { $cfg_scale = '7'; } else { $cfg_scale = trim($aimogen_Main_Settings['cfg_scale']); } if (!isset($aimogen_Main_Settings['cfg_seed']) || trim($aimogen_Main_Settings['cfg_seed']) == '') { $cfg_seed = ''; } else { $cfg_seed = trim($aimogen_Main_Settings['cfg_seed']); } if (!isset($aimogen_Main_Settings['clip_guidance_preset']) || trim($aimogen_Main_Settings['clip_guidance_preset']) == '') { $clip_guidance_preset = 'NONE'; } else { $clip_guidance_preset = trim($aimogen_Main_Settings['clip_guidance_preset']); } if (!isset($aimogen_Main_Settings['clip_style_preset']) || trim($aimogen_Main_Settings['clip_style_preset']) == '') { $clip_style_preset = 'NONE'; } else { $clip_style_preset = trim($aimogen_Main_Settings['clip_style_preset']); } if (!isset($aimogen_Main_Settings['sampler']) || trim($aimogen_Main_Settings['sampler']) == '') { $sampler = 'auto'; } else { $sampler = trim($aimogen_Main_Settings['sampler']); } if(intval($steps) < 10 || intval($steps) > 250) { $error = 'Invalid steps count provided (10-250): ' . intval($steps); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(intval($cfg_scale) < 0 || intval($cfg_scale) > 35) { $error = 'Invalid cfg_scale count provided (0-35): ' . intval($cfg_scale); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(intval($cfg_seed) < 0 || intval($cfg_seed) > 4294967295) { $error = 'Invalid cfg_seed count provided (0-4294967295): ' . intval($cfg_seed); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if($stable_model == 'stable-diffusion-core') { $api_url = 'https://api.stability.ai/v2beta/stable-image/generate/core'; $request_args = [ 'method' => 'POST', 'timeout' => 300, 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token ], 'sslverify' => false, 'body' => [ 'prompt' => $text, 'output_format' => 'png', 'height' => $height, 'width' => $width ] ]; if (trim($cfg_seed) !== '') { $request_args['body']['seed'] = trim($cfg_seed); } if (trim($clip_style_preset) !== '' && $clip_style_preset !== 'NONE') { $request_args['body']['style_preset'] = trim($clip_style_preset); } $response = wp_remote_post($api_url, $request_args); if (is_wp_error($response)) { if ( !empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count ) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after WP error: ' . $api_url ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $error = 'Failed to create Stability request.'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $http_code = wp_remote_retrieve_response_code($response); $ai_response = wp_remote_retrieve_body($response); if ($http_code !== 200) { if ( !empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count ) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after HTTP failure: ' . $api_url . ' code: ' . $http_code . ' response: ' . json_encode($ai_response) ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $json_resp = json_decode($ai_response, true); $msg = $json_resp['message'] ?? 'Unknown error'; aimogen_log_to_file('Invalid return code from API: ' . $http_code . ' ' . $msg); $error = 'Failed to generate the image, please try again later! Error: ' . $msg; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to get AI response: ' . $api_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $json_resp = json_decode($ai_response, true); if($json_resp === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after decode failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to decode AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(!isset($json_resp['image'])) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Invalid AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($text, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); if(isset($json_resp['seed'])) { $seed = $json_resp['seed']; } $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $reason = ''; if(isset($json_resp['finish_reason'])) { $reason = $json_resp['finish_reason']; if($reason == 'ERROR') { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after error failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'An error was encountered during API call: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } elseif($reason == 'CONTENT_FILTERED') { aimogen_log_to_file('The image was filtered, by the nudity filter, blurred parts may appear in it, prompt: ' . $ret_path); } } $json_resp = apply_filters( 'aimogen_stability_reply_raw', $json_resp, $text ); $img = $json_resp['image']; apply_filters( 'aimogen_ai_reply', $img, $query ); if($returnbase64 == true) { return $img; } $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $text); if($localpath !== false) { wp_delete_file($rezi[0]); $localrez = array(); $localrez[0] = $localpath[1]; $localrez[1] = $localpath[0]; $rezi = $localrez; } } } return $rezi; } } elseif($stable_model == 'stable-diffusion-ultra') { $api_url = 'https://api.stability.ai/v2beta/stable-image/generate/ultra'; $request_args = [ 'method' => 'POST', 'timeout' => 300, 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token ], 'sslverify' => false, 'body' => [ 'prompt' => $text, 'output_format' => 'png' ] ]; if (trim($cfg_seed) !== '') { $request_args['body']['seed'] = trim($cfg_seed); } if ($width > $height) { $request_args['body']['aspect_ratio'] = '16:9'; } elseif ($width < $height) { $request_args['body']['aspect_ratio'] = '9:16'; } else { $request_args['body']['aspect_ratio'] = '1:1'; } if (!empty($source_image_url)) { global $wp_filesystem; if (!is_a($wp_filesystem, 'WP_Filesystem_Base')) { require_once ABSPATH . 'wp-admin/includes/file.php'; $creds = request_filesystem_credentials(site_url()); wp_filesystem($creds); } $temp_file = tempnam(sys_get_temp_dir(), 'aimogen_'); $response = wp_remote_get($source_image_url); if (is_wp_error($response)) { $error = apply_filters('aimogen_modify_ai_error', 'Failed to download source image.'); return false; } $image_data = wp_remote_retrieve_body($response); if (empty($image_data)) { $error = apply_filters('aimogen_modify_ai_error', 'Failed to download source image.'); return false; } $wp_filesystem->put_contents($temp_file, $image_data); if ($temp_file === false) { $error = apply_filters('aimogen_modify_ai_error', 'Failed to save source image.'); return false; } $request_args['body']['image'] = curl_file_create($temp_file); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_file_create $request_args['body']['strength'] = 1 - floatval($image_strength); $request_args['body']['mode'] = 'image-to-image'; } $response = wp_remote_post($api_url, $request_args); if (is_wp_error($response)) { if ( !empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count ) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after WP error: ' . $api_url ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $error = apply_filters('aimogen_modify_ai_error', 'Failed to create Stability request.'); return false; } $http_code = wp_remote_retrieve_response_code($response); $ai_response = wp_remote_retrieve_body($response); if ($http_code !== 200) { if ( !empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count ) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after HTTP failure: ' . $api_url . ' code: ' . $http_code . ' response: ' . json_encode($ai_response) ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $json_resp = json_decode($ai_response, true); $msg = $json_resp['message'] ?? 'Unknown error'; aimogen_log_to_file('Invalid return code from API: ' . $http_code . ' Error: ' . $msg); $error = apply_filters( 'aimogen_modify_ai_error', 'Failed to generate the image. Error: ' . $msg ); return false; } if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to get AI response: ' . $api_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $json_resp = json_decode($ai_response, true); if($json_resp === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after decode failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to decode AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(!isset($json_resp['image'])) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Invalid AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($text, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); if(isset($json_resp['seed'])) { $seed = $json_resp['seed']; } $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $reason = ''; if(isset($json_resp['finish_reason'])) { $reason = $json_resp['finish_reason']; if($reason == 'ERROR') { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after error failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'An error was encountered during API call: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } elseif($reason == 'CONTENT_FILTERED') { aimogen_log_to_file('The image was filtered, by the nudity filter, blurred parts may appear in it, prompt: ' . $ret_path); } } $json_resp = apply_filters( 'aimogen_stability_reply_raw', $json_resp, $text ); $img = $json_resp['image']; apply_filters( 'aimogen_ai_reply', $img, $query ); if($returnbase64 == true) { return $img; } $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $text); if($localpath !== false) { wp_delete_file($rezi[0]); $localrez = array(); $localrez[0] = $localpath[1]; $localrez[1] = $localpath[0]; $rezi = $localrez; } } } return $rezi; } } elseif($stable_model == 'stable-diffusion-3-0-turbo' || $stable_model == 'stable-diffusion-3-0-large' || $stable_model == 'stable-diffusion-3-0-medium' || $stable_model == 'sd3.5-large' || $stable_model == 'sd3.5-large-turbo' || $stable_model == 'sd3.5-medium') { $api_url = 'https://api.stability.ai/v2beta/stable-image/generate/sd3'; $request_args = [ 'method' => 'POST', 'timeout' => 300, 'sslverify' => false, 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token ], 'body' => [] ]; if ($stable_model === 'stable-diffusion-3-0-turbo') { $smodel = 'sd3-large-turbo'; } elseif ($stable_model === 'stable-diffusion-3-0-large') { $smodel = 'sd3-large'; } elseif ($stable_model === 'stable-diffusion-3-0-medium') { $smodel = 'sd3-medium'; } else { $smodel = $stable_model; } $request_args['body'] = [ 'prompt' => $text, 'mode' => 'text-to-image', 'output_format' => 'png', 'model' => $smodel, 'height' => $height, 'width' => $width ]; if (trim($cfg_seed) !== '') { $request_args['body']['seed'] = trim($cfg_seed); } if (!empty($source_image_url)) { global $wp_filesystem; if (!is_a($wp_filesystem, 'WP_Filesystem_Base')) { require_once ABSPATH . 'wp-admin/includes/file.php'; $creds = request_filesystem_credentials(site_url()); wp_filesystem($creds); } $temp_file = tempnam(sys_get_temp_dir(), 'aimogen_'); $response = wp_remote_get($source_image_url); if (is_wp_error($response)) { $error = apply_filters('aimogen_modify_ai_error', 'Failed to download source image.'); return false; } $image_data = wp_remote_retrieve_body($response); if (empty($image_data)) { $error = apply_filters('aimogen_modify_ai_error', 'Failed to download source image.'); return false; } $wp_filesystem->put_contents($temp_file, $image_data); if ($temp_file === false) { $error = apply_filters('aimogen_modify_ai_error', 'Failed to save source image.'); return false; } $request_args['body']['image'] = curl_file_create($temp_file); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_file_create $request_args['body']['strength'] = 1 - floatval($image_strength); $request_args['body']['mode'] = 'image-to-image'; } $response = wp_remote_post($api_url, $request_args); if (is_wp_error($response)) { if (!empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after WP error: ' . $api_url ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $error = apply_filters('aimogen_modify_ai_error', 'Failed to create Stability request.'); return false; } $http_code = wp_remote_retrieve_response_code($response); $ai_response = wp_remote_retrieve_body($response); if ($http_code !== 200) { if (!empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after HTTP failure: ' . $api_url . ' code: ' . $http_code . ' response: ' . json_encode($ai_response) ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $json_resp = json_decode($ai_response, true); $msg = isset($json_resp['message']) ? $json_resp['message'] : 'Error'; aimogen_log_to_file('Invalid return code from API: ' . $http_code . ' Error: ' . $msg); $error = apply_filters( 'aimogen_modify_ai_error', 'Failed to generate the image, please try again later! Error: ' . $msg ); return false; } if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to get AI response: ' . $api_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $json_resp = json_decode($ai_response, true); if($json_resp === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after decode failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to decode AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(!isset($json_resp['image'])) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Invalid AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($text, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); if(isset($json_resp['seed'])) { $seed = $json_resp['seed']; } $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $reason = ''; if(isset($json_resp['finish_reason'])) { $reason = $json_resp['finish_reason']; if($reason == 'ERROR') { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after error failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'An error was encountered during API call: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } elseif($reason == 'CONTENT_FILTERED') { aimogen_log_to_file('The image was filtered, by the nudity filter, blurred parts may appear in it, prompt: ' . $ret_path); } } $json_resp = apply_filters( 'aimogen_stability_reply_raw', $json_resp, $text ); $img = $json_resp['image']; apply_filters( 'aimogen_ai_reply', $img, $query ); if($returnbase64 == true) { return $img; } $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $text); if($localpath !== false) { wp_delete_file($rezi[0]); $localrez = array(); $localrez[0] = $localpath[1]; $localrez[1] = $localpath[0]; $rezi = $localrez; } } } return $rezi; } } else { if(intval($height) > 2048) { $height = 2048; } if(intval($height) < 512) { $height = 512; } if(intval($width) > 2048) { $width = 2048; } if(intval($width) < 512) { $width = 512; } if(intval($width) * intval($height) > 1048576) { $error = 'Width x Height must not be greater than 1 Megapixel (1048576), current is: ' . intval($width) * intval($height); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $api_url = 'https://api.stability.ai/v1/generation/' . $stable_model . '/text-to-image'; $request_args = [ 'method' => 'POST', 'timeout' => 300, 'sslverify' => false, 'headers' => [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => $token ] ]; $payload = [ 'samples' => 1, 'text_prompts' => [ [ 'text' => str_replace('"', "'", $text), 'weight' => 1 ] ] ]; if (trim($cfg_scale) !== '' && trim($cfg_scale) !== '7') { $payload['cfg_scale'] = (int) trim($cfg_scale); } if (trim($cfg_seed) !== '') { $payload['seed'] = (int) trim($cfg_seed); } if (trim($clip_guidance_preset) !== '' && trim($clip_guidance_preset) !== 'NONE') { $payload['clip_guidance_preset'] = trim($clip_guidance_preset); } if (trim($clip_style_preset) !== '' && trim($clip_style_preset) !== 'NONE') { $payload['style_preset'] = trim($clip_style_preset); } if (trim($height) !== '' && trim($height) !== '512') { $payload['height'] = (int) trim($height); } if (trim($width) !== '' && trim($width) !== '512') { $payload['width'] = (int) trim($width); } if (trim($steps) !== '' && trim($steps) !== '50') { $payload['steps'] = (int) trim($steps); } if (trim($sampler) !== '' && trim($sampler) !== 'auto') { $payload['sampler'] = trim($sampler); } $request_args['body'] = wp_json_encode($payload); $response = wp_remote_post($api_url, $request_args); if (is_wp_error($response)) { if (!empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after WP error: ' . $api_url ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $error = apply_filters('aimogen_modify_ai_error', 'Failed to create Stability request.'); return false; } $http_code = wp_remote_retrieve_response_code($response); $ai_response = wp_remote_retrieve_body($response); if ($http_code !== 200) { if (!empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability API call after HTTP failure: ' . $api_url . ' code: ' . $http_code . ' response: ' . json_encode($ai_response) ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image( $text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength ); } $json_resp = json_decode($ai_response, true); $msg = isset($json_resp['message']) ? $json_resp['message'] : 'Error'; aimogen_log_to_file('Invalid return code from API: ' . $http_code . ' Error: ' . $msg); aimogen_log_to_file('PostFields: ' . $request_args['body']); $error = apply_filters( 'aimogen_modify_ai_error', 'Failed to generate the image, please try again later! Error: ' . $msg ); return false; } if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to get AI response: ' . $api_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $json_resp = json_decode($ai_response, true); if($json_resp === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after decode failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Failed to decode AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(!isset($json_resp['artifacts'][0]['base64'])) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'Invalid AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($text, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); if(isset($json_resp['artifacts'][0]['seed'])) { $seed = $json_resp['artifacts'][0]['seed']; } $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $reason = ''; if(isset($json_resp['artifacts'][0]['finishReason'])) { $reason = $json_resp['artifacts'][0]['finishReason']; if($reason == 'ERROR') { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability API call after error failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_image($text, $height, $width, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal, $stable_model, $source_image_url, $image_strength); } else { $error = 'An error was encountered during API call: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } elseif($reason == 'CONTENT_FILTERED') { aimogen_log_to_file('The image was filtered, by the nudity filter, blurred parts may appear in it, prompt: ' . $ret_path); } } $json_resp = apply_filters( 'aimogen_stability_reply_raw', $json_resp, $text ); $img = $json_resp['artifacts'][0]['base64']; apply_filters( 'aimogen_ai_reply', $img, $query ); if($returnbase64 == true) { return $img; } $rezi = aimogen_base64_to_jpeg($img, $file, $ret_path); if($rezi !== false && $rezi !== '') { if(isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi[1], $attached_id, '', false, $text); if($localpath !== false) { wp_delete_file($rezi[0]); $rezi = $localrez; } } } return $rezi; } } } function aimogen_generate_replicate_image($text = '', $height = '512', $width = '512', $env = '', $retry_count = 0, &$error = '', $nolocal = false, $replicate_model = false) { $is_allowed = apply_filters('aimogen_is_ai_image_allowed', true, $text); if ( $is_allowed !== true ) { $error = is_string( $is_allowed ) ? $is_allowed : esc_html__('You are not allowed to do this query', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $text = apply_filters('aimogen_modify_ai_image_query', $text); $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if(empty($replicate_model)) { if (!isset($aimogen_Main_Settings['replicate_image_model']) || trim($aimogen_Main_Settings['replicate_image_model']) == '') { $replicate_model = AIMOGEN_REPLICATE_DEFAULT_API_VERSION; } else { $replicate_model = trim($aimogen_Main_Settings['replicate_image_model']); } } if (!isset($aimogen_Main_Settings['replicate_app_id']) || trim($aimogen_Main_Settings['replicate_app_id']) == '') { $error = 'You need to enter a Replicate API key in the plugin\'s "Settings" menu to use this feature!'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['replicate_app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_replicate_api_key', $token); $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'replicate'; $maxResults = 1; $available_tokens = 1000; $temperature = 1; if(!isset($aimogen_Main_Settings['no_img_translate']) || $aimogen_Main_Settings['no_img_translate'] != 'on') { $text_trans = aimogen_translate_stability($text); if($text_trans != $text && !empty($text_trans)) { aimogen_log_to_file('Replicate prompt translated from: "' . $text . '" to: "' . $text_trans . '"'); $text = $text_trans; } } if(strlen($text) > 2000) { $text = aimogen_substr($text, 0, 2000); } $query = new Aimogen_lite_Query($text, $available_tokens, $replicate_model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, $width . 'x' . $height, ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = 'Image generator is rate limited: ' . $ok; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(intval($height) < 512 || intval($height) > 2048) { $error = 'Invalid height (512-2048): ' . $height; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(intval($width) < 512 || intval($width) > 2048) { $error = 'Invalid width (512-2048): ' . $width; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(intval($width) * intval($height) > 1048576) { $error = 'Width x Height must not be greater than 1 Megapixel (1048576), current is: ' . intval($width) * intval($height); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on' && AIMOGEN_IS_DEBUG === true) { aimogen_log_to_file('Generating Replicate image using model: ' . $replicate_model . ' and prompt: ' . $text); } if(isset($aimogen_Main_Settings['prompt_strength']) && $aimogen_Main_Settings['prompt_strength'] != '') { $prompt_strength = floatval($aimogen_Main_Settings['prompt_strength']); } else { $prompt_strength = 0.8; } if(isset($aimogen_Main_Settings['num_inference_steps']) && $aimogen_Main_Settings['num_inference_steps'] != '') { $num_inference_steps = intval($aimogen_Main_Settings['num_inference_steps']); } else { $num_inference_steps = 4; } if(isset($aimogen_Main_Settings['ai_scheduler']) && $aimogen_Main_Settings['ai_scheduler'] != '') { $ai_scheduler = intval($aimogen_Main_Settings['ai_scheduler']); } else { $ai_scheduler = 'DPMSolverMultistep'; } if(isset($aimogen_Main_Settings['replicate_ratio']) && $aimogen_Main_Settings['replicate_ratio'] != '' && $aimogen_Main_Settings['replicate_ratio'] != 'default') { $replicate_ratio = $aimogen_Main_Settings['replicate_ratio']; } else { $replicate_ratio = ''; } $queryArray = array(); if(isset($aimogen_Main_Settings['custom_params_replicate']) && $aimogen_Main_Settings['custom_params_replicate'] != '') { parse_str($aimogen_Main_Settings['custom_params_replicate'], $queryArray); } $body = array( 'version' => $replicate_model, 'input' => array( 'prompt' => $text, 'num_outputs' => 1, 'negative_prompt' => '', 'width' => intval($width), 'height' => intval($height), 'prompt_strength' => $prompt_strength, 'num_inference_steps' => $num_inference_steps, 'scheduler' => $ai_scheduler, ) ); if(!empty($replicate_ratio)) { $body['input']['aspect_ratio'] = $replicate_ratio; } if(!empty($queryArray)) { foreach($queryArray as $indxd => $qa) { if(is_numeric($qa)) { $queryArray[$indxd] = floatval($qa); } } $body['input'] = array_merge($body['input'], $queryArray); } $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'Token ' . $token ); try { $aimogen_response = wp_remote_post('https://api.replicate.com/v1/predictions', array( 'headers' => $headers, 'body' => json_encode($body), 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, )); if (is_wp_error($aimogen_response)) { $error = 'Error duing Replicate image creation: ' . $aimogen_response->get_error_message(); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } else { $response_body = isset($aimogen_response['body']) && !empty($aimogen_response['body']) ? json_decode($aimogen_response['body'], true) : false; if (isset($response_body['detail']) && !empty($response_body['detail'])) { $error = 'Error in Replicate image processing: ' . $response_body['detail']; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } elseif (!isset($response_body['urls']['get'])) { $error = 'Empty Replicate results'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } else { $images = aimogen_process_replicate_images($response_body['urls']['get'], $headers); if (!is_array($images)) { $images = array($images); } } } } catch (\Exception $exception) { $error = 'Failed to generate Replicate image: ' . $exception->getMessage(); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($text, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $rezi = $images[0]; $return_url = $rezi; apply_filters( 'aimogen_ai_reply', $rezi, $query ); if (isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $nolocal == false) { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi, $attached_id, '', false, $text); if($localpath !== false) { $return_url = $localpath[0]; } else { $return_url = $rezi; } } else { $return_url = $rezi; } return $return_url; } function aimogen_generate_ideogram_image($text = '', $height = '512', $width = '512', $env = '', $retry_count = 0, &$error = '', $nolocal = false, $ideogram_model = false) { $is_allowed = apply_filters('aimogen_is_ai_image_allowed', true, $text); if ( $is_allowed !== true ) { $error = is_string( $is_allowed ) ? $is_allowed : esc_html__('You are not allowed to do this query', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $text = apply_filters('aimogen_modify_ai_image_query', $text); $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if(empty($ideogram_model)) { if (!isset($aimogen_Main_Settings['ideogram_image_model']) || trim($aimogen_Main_Settings['ideogram_image_model']) == '') { $ideogram_model = AIMOGEN_IDEOGRAM_MODELS[0]; } else { $ideogram_model = trim($aimogen_Main_Settings['ideogram_image_model']); } } if (!isset($aimogen_Main_Settings['ideogram_app_id']) || trim($aimogen_Main_Settings['ideogram_app_id']) == '') { $error = 'You need to enter a Ideogram API key in the plugin\'s "Settings" menu to use this feature!'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['ideogram_app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_ideogram_api_key', $token); $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'ideogram'; $maxResults = 1; $available_tokens = 1000; $temperature = 1; if(!isset($aimogen_Main_Settings['no_img_translate']) || $aimogen_Main_Settings['no_img_translate'] != 'on') { $text_trans = aimogen_translate_stability($text); if($text_trans != $text && !empty($text_trans)) { aimogen_log_to_file('Ideogram prompt translated from: "' . $text . '" to: "' . $text_trans . '"'); $text = $text_trans; } } if(strlen($text) > 3000) { $text = aimogen_substr($text, 0, 3000); } $query = new Aimogen_lite_Query($text, $available_tokens, $ideogram_model, $temperature, $stop, $env, $mode, $token, $session, $maxResults, $width . 'x' . $height, ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = 'Image generator is rate limited: ' . $ok; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Generating Ideogram Image using prompt: ' . $text); } if(isset($aimogen_Main_Settings['ideogram_magic_prompt']) && $aimogen_Main_Settings['ideogram_magic_prompt'] != '') { $ideogram_magic_prompt = $aimogen_Main_Settings['ideogram_magic_prompt']; } else { $ideogram_magic_prompt = 'AUTO'; } if(isset($aimogen_Main_Settings['ideogram_style']) && $aimogen_Main_Settings['ideogram_style'] != '') { $ideogram_style = $aimogen_Main_Settings['ideogram_style']; } else { $ideogram_style = 'AUTO'; } if(isset($aimogen_Main_Settings['ideogram_palette']) && $aimogen_Main_Settings['ideogram_palette'] != '') { $ideogram_palette = $aimogen_Main_Settings['ideogram_palette']; } else { $ideogram_palette = ''; } if(isset($aimogen_Main_Settings['ideogram_ratio']) && $aimogen_Main_Settings['ideogram_ratio'] != '' && $aimogen_Main_Settings['ideogram_ratio'] != 'default') { $ideogram_ratio = $aimogen_Main_Settings['ideogram_ratio']; } else { $ideogram_ratio = ''; } $body = array( 'image_request' => array( 'prompt' => $text, 'model' => $ideogram_model, 'magic_prompt_option' => $ideogram_magic_prompt, )); if(!empty($ideogram_ratio) && $ideogram_ratio != 'default') { $body['image_request']['aspect_ratio'] = $ideogram_ratio; } if(!empty($ideogram_style) && $ideogram_style != 'default' && $ideogram_model != 'V_1' && $ideogram_model != 'V_1_TURBO') { $body['image_request']['style_type'] = $ideogram_style; } if(!empty($ideogram_palette) && $ideogram_palette != 'default') { $body['image_request']['color_palette']['name'] = $ideogram_palette; } $headers = array( 'Api-Key' => $token, 'Content-Type' => 'application/json' ); $images = array(); try { if ($ideogram_model === 'V_3') { $v3_endpoint = 'https://api.ideogram.ai/v1/ideogram-v3/generate'; $form_data = [ 'prompt' => $text, 'rendering_speed' => 'TURBO', 'magic_prompt' => $ideogram_magic_prompt, 'style_type' => $ideogram_style !== 'default' ? $ideogram_style : 'GENERAL', 'num_images' => 1 ]; if (!empty($ideogram_ratio) && $ideogram_ratio !== 'default') { $ideogram_ratio = str_replace('ASPECT_', '', $ideogram_ratio); $ideogram_ratio = str_replace('_', 'x', $ideogram_ratio); $form_data['aspect_ratio'] = $ideogram_ratio; } if (!empty($ideogram_palette) && $ideogram_palette !== 'default') { $form_data['color_palette'] = json_encode([ 'name' => $ideogram_palette ]); } $boundary = '----WebKitFormBoundary' . uniqid(); $body = ''; foreach ($form_data as $key => $value) { $body .= "--$boundary\r\n"; $body .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n"; $body .= "$value\r\n"; } $body .= "--$boundary--\r\n"; $headers = [ 'Api-Key' => $token, 'Content-Type' => 'multipart/form-data; boundary=' . $boundary ]; $aimogen_response = wp_remote_post($v3_endpoint, [ 'headers' => $headers, 'body' => $body, 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT ]); } else { $aimogen_response = wp_remote_post('https://api.ideogram.ai/generate', array( 'headers' => $headers, 'body' => json_encode($body), 'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT, )); } if (is_wp_error($aimogen_response)) { $error = 'Error duing Ideogram image creation: ' . $aimogen_response->get_error_message(); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } else { $response_body = isset($aimogen_response['body']) && !empty($aimogen_response['body']) ? json_decode($aimogen_response['body'], true) : false; if (!isset($response_body['data'][0]['url']) || empty($response_body['data'][0]['url'])) { $error = 'Error in Ideogram image processing: ' . json_encode($response_body); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } else { $images = array($response_body['data'][0]['url']); } } } catch (\Exception $exception) { $error = 'Failed to generate Ideogram image: ' . $exception->getMessage(); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $upload_dir = wp_upload_dir(); $keyword_class = new Aimogen_lite_keywords(); $filename = $keyword_class->keywords($text, 4); $filename = str_replace(' ', '-', $filename); if(empty($filename)) { $seed = wp_rand(); $filename = $seed . '.png'; } else { $filename .= '-' . wp_rand(1,99999) . '.png'; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } $rezi = $images[0]; $return_url = $rezi; apply_filters( 'aimogen_ai_reply', $rezi, $query ); if (isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $nolocal == false) { $attached_id = ''; $localpath = aimogen_copy_image_locally($rezi, $attached_id, '', false, $text); if($localpath !== false) { $return_url = $localpath[0]; } else { $return_url = $rezi; } } else { $return_url = $rezi; } return $return_url; } function aimogen_generate_stability_video($image_url = '', $image_size = '768x768', $env = '', $retry_count = 0, $returnbase64 = false, &$error = '', $nolocal = false) { $all_stable_video_sizes = ['768x768' => '768x768', '1024x576' => '1024x576', '576x1024' => '576x1024']; $is_allowed = apply_filters('aimogen_is_ai_video_allowed', true, $image_url); if ( $is_allowed !== true ) { $error = is_string( $is_allowed ) ? $is_allowed : esc_html__('You are not allowed to do this query', 'aimogen'); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } global $wp_filesystem; if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){ require_once ABSPATH . 'wp-admin/includes/file.php'; $creds = request_filesystem_credentials( site_url() ); wp_filesystem($creds); } $image_url = apply_filters('aimogen_modify_ai_video_url', $image_url); if($image_size != '') { $expl = explode('x', $image_size); if(!isset($expl[1])) { $error = 'Invalid resize size provided: ' . $image_size; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $width = $expl[0]; $height = $expl[1]; $upload_dir = wp_upload_dir(); if(substr( $image_url, 0, 10 ) === "data:image") { $data = explode(',', $image_url); if(isset($data[1])) { $image_data = base64_decode($data[1]); if($image_data === FALSE) { $error = 'Failed to decode image URL data: ' . $image_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $error = 'Failed to download image URL post data: ' . $image_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } preg_match('{data:image/(.*?);}', $image_url, $ex_matches); if(isset($ex_matches[1])) { $image_url = 'image.' . $ex_matches[1]; } else { $image_url = 'image.jpg'; } } else { $image_data = aimogen_get_web_page($image_url); if ($image_data === FALSE || strpos($image_data, 'Access Denied') !== FALSE || strpos($image_data, 'Just a moment...') !== FALSE || strpos($image_data, '', '-', $filename); $filename = str_replace('*', '-', $filename); $filename = str_replace('/', '-', $filename); $filename = str_replace('$', '-', $filename); $filename = str_replace('\'', '-', $filename); $filename = str_replace('"', '-', $filename); $filename = str_replace(':', '-', $filename); $filename = str_replace('@', '-', $filename); $filename = str_replace('+', '-', $filename); $filename = str_replace('|', '-', $filename); $filename = str_replace('=', '-', $filename); $filename = str_replace('`', '-', $filename); $file_parts = pathinfo($filename); if(!isset($file_parts['extension'])) { $file_parts['extension'] = ''; } switch($file_parts['extension']) { case "": if(!aimogen_endsWith($filename, '.jpg')) $filename .= '.jpg'; break; case NULL: if(!aimogen_endsWith($filename, '.jpg')) $filename .= '.jpg'; break; } if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; $ret_path = $upload_dir['url'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; $ret_path = $upload_dir['baseurl'] . '/' . $filename; } if($wp_filesystem->exists($file)) { wp_delete_file($file); } $ret = $wp_filesystem->put_contents($file, $image_data); if ($ret === FALSE) { $error = 'Failed to save initial image URL: ' . $image_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } try { if(!class_exists('\Aimogen\ImageResize')){require_once (dirname(__FILE__) . "/res/ImageResize/ImageResize.php");} $imageRes = new ImageResize($file); if (isset($aimogen_Main_Settings['ai_resize_quality']) && $aimogen_Main_Settings['ai_resize_quality'] !== '') { $imageRes->quality_jpg = intval($aimogen_Main_Settings['ai_resize_quality']); } else { $imageRes->quality_jpg = 100; } $imageRes->resize($width, $height, true); $imageRes->save($file); $imageData = $wp_filesystem->get_contents($file); if($imageData === false) { $error = 'Failed to read local file: ' . $file; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } wp_delete_file($file); } catch(Exception $e) { $error = 'Failed to resize AI generated image: ' . $image_url . ' to sizes ' . $width . ' - ' . $height . '. Exception thrown ' . esc_html($e->getMessage()) . '!'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $imageData = aimogen_get_web_page($image_url); if ($imageData === false) { $error = 'Failed to download image URL: ' . $image_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); $video_cfg_scale = '2.5'; if (isset($aimogen_Main_Settings['video_cfg_scale']) && trim($aimogen_Main_Settings['video_cfg_scale']) != '') { $video_cfg_scale = $aimogen_Main_Settings['video_cfg_scale']; } $motion_bucket_id = '40'; if (isset($aimogen_Main_Settings['motion_bucket_id']) && trim($aimogen_Main_Settings['motion_bucket_id']) != '') { $motion_bucket_id = $aimogen_Main_Settings['motion_bucket_id']; } if (!isset($aimogen_Main_Settings['stability_app_id']) || trim($aimogen_Main_Settings['stability_app_id']) == '') { $error = 'You need to enter a Stability.AI API key in the plugin\'s "Settings" menu to use this feature!'; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(!in_array($image_size, $all_stable_video_sizes)) { $image_size = '768x768'; } $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['stability_app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_stability_api_key', $token); $aimogen_Limit_Settings = get_option('aimogen_Limit_Settings', false); $stop = null; $session = aimogen_get_session_id(); $mode = 'video'; $maxResults = 1; $available_tokens = 1000; $temperature = 1; $query = new Aimogen_lite_Query($image_url, $available_tokens, 'stable-diffusion-video', $temperature, $stop, $env, $mode, $token, $session, $maxResults, $image_size, ''); $ok = apply_filters( 'aimogen_ai_allowed', true, $aimogen_Limit_Settings ); if ( $ok !== true ) { $error = 'Video generator is rate limited: ' . $ok; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if(isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Generating Stability.AI Video using image URL: ' . $image_url . ' size: ' . $image_size); } $api_url = 'https://api.stability.ai/v2alpha/generation/image-to-video'; $boundary = wp_generate_password(24, false); $boundary_mid = "--$boundary\r\n"; $boundary_end = "--$boundary--\r\n"; $multipart = ''; $multipart .= $boundary_mid . 'Content-Disposition: form-data; name="image"; filename="' . basename($tmpFilePath) . '"' . "\r\n" . 'Content-Type: ' . $mime . "\r\n\r\n" . file_get_contents($tmpFilePath) . "\r\n"; $multipart .= $boundary_mid . 'Content-Disposition: form-data; name="cfg_scale"' . "\r\n\r\n" . floatval($video_cfg_scale) . "\r\n"; $multipart .= $boundary_mid . 'Content-Disposition: form-data; name="motion_bucket_id"' . "\r\n\r\n" . intval($motion_bucket_id) . "\r\n"; $multipart .= $boundary_end; $args = [ 'method' => 'POST', 'timeout' => 300, 'sslverify' => false, 'headers' => [ 'Authorization' => $token, 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, ], 'body' => $multipart, ]; $response = wp_remote_post($api_url, $args); wp_delete_file($tmpFilePath); if (is_wp_error($response)) { if (!empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability video API call after WP error: ' . $api_url ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_video( $image_url, $image_size, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal ); } $error = apply_filters('aimogen_modify_ai_error', 'Failed to create Stability request.'); return false; } $http_code = wp_remote_retrieve_response_code($response); $ai_response = wp_remote_retrieve_body($response); if ($http_code !== 200) { if (!empty($aimogen_Main_Settings['max_retry']) && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file( 'Retrying (' . (intval($retry_count) + 1) . ') Stability video API call after HTTP failure: ' . $api_url ); sleep(pow(2, $retry_count)); return aimogen_generate_stability_video( $image_url, $image_size, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal ); } aimogen_log_to_file( 'Invalid return code from API: ' . $http_code . ' Response: ' . json_encode($ai_response) ); $error = apply_filters( 'aimogen_modify_ai_error', 'Failed to generate the video, please try again later!' ); return false; } if($ai_response === false) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability video API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_video($image_url, $image_size, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal); } else { $error = 'Failed to get AI response: ' . $api_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } else { $json_resp = json_decode($ai_response, true); if($json_resp === null) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability video API call after decode failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_video($image_url, $image_size, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal); } else { $error = 'Failed to decode AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } if(!isset($json_resp['id'])) { if (isset($aimogen_Main_Settings['max_retry']) && $aimogen_Main_Settings['max_retry'] != '' && is_numeric($aimogen_Main_Settings['max_retry']) && intval($aimogen_Main_Settings['max_retry']) > $retry_count) { aimogen_log_to_file('Retrying (' . (intval($retry_count) + 1) . ') Stability video API call after response failure: ' . $api_url); sleep(pow(2, $retry_count)); return aimogen_generate_stability_video($image_url, $image_size, $env, intval($retry_count) + 1, $returnbase64, $error, $nolocal); } else { $error = 'Invalid AI response: ' . $ai_response; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } } $filename = 'video' . uniqid() . '.mp4'; $upload_dir = wp_upload_dir(); if (wp_mkdir_p($upload_dir['path'] . '/videos')) { $file = $upload_dir['path'] . '/videos/' . $filename; $ret_path = $upload_dir['url'] . '/videos/' . $filename; } else { $file = $upload_dir['basedir'] . '/videos/' . $filename; $ret_path = $upload_dir['baseurl'] . '/videos/' . $filename; } $my_id = $json_resp['id'];$apiUrl = 'https://api.stability.ai/v2alpha/generation/image-to-video/result/' . $my_id; $headers = [ 'authorization' => $token, 'accept' => 'application/json' ]; $max_timeout = 600; $wait_time = 0; $videoData = false; do { $response = wp_remote_get($apiUrl, [ 'timeout' => 300, 'sslverify' => false, 'headers' => $headers ]); if (is_wp_error($response)) { $error = 'Failed to contact Stability video API! ' . $response->get_error_message(); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $httpCode = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); if ($httpCode == 200) { $body = apply_filters('aimogen_stability_video_reply_raw', $body, $image_url); $videojson = json_decode($body, true); if ($videojson === null) { $error = 'Failed to decode video API response! ' . $body; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } if (!isset($videojson['video'])) { $error = 'Cannot decode video response ' . json_encode($videojson); $error = apply_filters('aimogen_modify_ai_error', $error); return false; } $videoData = $videojson['video']; break; } if ($httpCode != 202) { $error = 'Error returned from video API call ' . $httpCode . ' - ID: ' . $my_id . ' - data: ' . $body; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } sleep(2); $wait_time += 2; } while ($wait_time < $max_timeout); if($videoData === false) { $error = 'Failed to get video data for ' . $image_url; $error = apply_filters('aimogen_modify_ai_error', $error); return false; } apply_filters( 'aimogen_ai_reply', $videoData, $query ); if($returnbase64 == true) { return $videoData; } $rezi = aimogen_base64_to_file($videoData, $file, $ret_path); if($rezi !== false && $rezi !== '') { if($nolocal === false && isset($aimogen_Main_Settings['copy_locally']) && $aimogen_Main_Settings['copy_locally'] != 'disabled' && $aimogen_Main_Settings['copy_locally'] != 'on') { $copy_it = $aimogen_Main_Settings['copy_locally']; $localpath = aimogen_copy_video_locally($rezi[1], 'video_' . time(), $copy_it); if($localpath !== false) { wp_delete_file($rezi[0]); $localrez = array(); $localrez[0] = $localpath[1]; $localrez[1] = $localpath[0]; $rezi = $localrez; } } } return $rezi; } } function aimogen_generate_audio_convertion($prompt, $url = '', $purpose = 'transcriptions', $model = 'whisper-1', $temperature = 0, $language = 'en') { $aimogen_result = array('status' => 'error', 'msg' => 'Something went wrong with audio converter'); $mime_types = ['mp3' => 'audio/mpeg','mp4' => 'video/mp4','mpeg' => 'video/mpeg','m4a' => 'audio/m4a','wav' => 'audio/wav','webm' => 'video/webm']; if($purpose != 'transcriptions' && $purpose != 'translations') { $aimogen_result['msg'] = 'Unknown purpose submitted.'; return $aimogen_result; } if(empty($url)){ $aimogen_result['msg'] = 'The audio URL is required'; return $aimogen_result; } $remoteFile = get_headers($url, 1); $file_name = basename($url); $is_in_mime_types = false; $file_ext = ''; foreach($mime_types as $key=>$mime_type){ if((is_array($remoteFile['Content-Type']) && in_array($mime_type,$remoteFile['Content-Type'])) || strpos($remoteFile['Content-Type'], $mime_type) !== false){ $is_in_mime_types = true; $file_ext = '.'.$key; break; } } if(!$is_in_mime_types){ $aimogen_result['msg'] = 'We only accept mp3, mp4, mpeg, mpga, m4a, wav, or webm.'; return $aimogen_result; } if(strpos($file_name, $file_ext) === false){ $file_name = md5(uniqid() . time()) . $file_ext; } if($remoteFile['Content-Length'] > 26214400){ $aimogen_result['msg'] = 'Audio file maximum 25MB'; return $aimogen_result; } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); if (!isset($aimogen_Main_Settings['app_id']) || trim($aimogen_Main_Settings['app_id']) == '') { $aimogen_result['msg'] = 'You need to add an API key in plugin settings for this shortcode to work.'; return $aimogen_result; } else { $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; $token = apply_filters('aimogen_openai_api_key', $token); if(empty($token)) { $aimogen_result['msg'] = 'Invalid API key submitted'; return $aimogen_result; } if(aimogen_is_aimogen_key($token)) { $aimogen_result['msg'] = 'Currently only OpenAI API is supported for audio processing.'; return $aimogen_result; } } if (aimogen_check_if_azure_or_others($aimogen_Main_Settings)) { $aimogen_result['msg'] = 'Azure/Claude API is not currently supported for audio conversion.'; return $aimogen_result; } require_once (dirname(__FILE__) . "/res/openai/Url.php"); require_once (dirname(__FILE__) . "/res/openai/OpenAi.php"); $open_ai = new OpenAi($token); if(!$open_ai){ $aimogen_result['msg'] = 'Missing API Setting'; return $aimogen_result; } if (isset($aimogen_Main_Settings['api_selector']) && trim($aimogen_Main_Settings['api_selector']) == 'custom' && isset($aimogen_Main_Settings['custom_endpoint']) && trim($aimogen_Main_Settings['custom_endpoint'], ' /') != '') { $open_ai->setCustomURL(trim($aimogen_Main_Settings['custom_endpoint'], ' /')); } if (isset($aimogen_Main_Settings['openai_organization']) && $aimogen_Main_Settings['openai_organization'] != '') { $open_ai->setORG($aimogen_Main_Settings['openai_organization']); } if(!function_exists('download_url')){ require_once ABSPATH . 'wp-admin/includes/file.php'; } $tmp_file = download_url($url); if ( is_wp_error( $tmp_file ) ){ $aimogen_result['msg'] = $tmp_file->get_error_message(); return $aimogen_result; } $response_format = 'text'; global $wp_filesystem; if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){ require_once ABSPATH . 'wp-admin/includes/file.php'; $creds = request_filesystem_credentials( site_url() ); wp_filesystem($creds); } $data_request = array( 'audio' => array( 'filename' => $file_name, 'data' => $wp_filesystem->get_contents($tmp_file) ), 'model' => $model, 'temperature' => $temperature, 'response_format' => $response_format, 'prompt' => $prompt ); if($purpose == 'transcriptions' && !empty($language)){ $data_request['language'] = $language; } $delay = ''; if (isset($aimogen_Main_Settings['request_delay']) && $aimogen_Main_Settings['request_delay'] != '') { if(stristr($aimogen_Main_Settings['request_delay'], ',') !== false) { $tempo = explode(',', $aimogen_Main_Settings['request_delay']); if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0]))) { $delay = wp_rand(trim($tempo[0]), trim($tempo[1])); } } else { if(is_numeric(trim($aimogen_Main_Settings['request_delay']))) { $delay = intval(trim($aimogen_Main_Settings['request_delay'])); } } } if($delay != '' && is_numeric($delay)) { usleep($delay); } if($purpose == 'transcriptions') { $completion = $open_ai->transcribe($data_request); } elseif($purpose == 'translations') { $completion = $open_ai->translate($data_request); } $result = json_decode($completion); if($result && isset($result->error)){ $aimogen_result['msg'] = $result->error->message; return $aimogen_result; } $aimogen_result['status'] = 'success'; $text_generated = $completion; $aimogen_result['data'] = $text_generated; if(empty($text_generated)){ $aimogen_result['msg'] = 'OpenAI returned empty content'; return $aimogen_result; } do_action('aimogen_audio_converter_reply', $aimogen_result); return $aimogen_result; } ?>