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); if(empty($appids_claude)) { aimogen_log_to_file('No valid Anthropic Claude API keys found in the plugin settings!'); return false; } $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); if(empty($appids_openrouter)) { aimogen_log_to_file('No valid OpenRouter API keys found in the plugin settings!'); return false; } $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); if(empty($appids_perplexity)) { aimogen_log_to_file('No valid PerplexityAI API keys found in the plugin settings!'); return false; } $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); if(empty($appids_groq)) { aimogen_log_to_file('No valid Groq API keys found in the plugin settings!'); return false; } $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); if(empty($appids_nvidia)) { aimogen_log_to_file('No valid Nvidia API keys found in the plugin settings!'); return false; } $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); if(empty($appids_xai)) { aimogen_log_to_file('No valid xAI API keys found in the plugin settings!'); return false; } $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(aimogen_is_google_model($model) === 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); if(empty($appids_google)) { aimogen_log_to_file('No valid Google Vertex API keys found in the plugin settings!'); return false; } $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 = aimogen_get_openai_api_base_url($aimogen_Main_Settings); } $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); $is_astra_model = function_exists('aimogen_is_openai_astra_model') && aimogen_is_openai_astra_model($model); 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($is_astra_model) { $base_params = aimogen_filter_astra_openai_request_options($base_params); } 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 } elseif (aimogen_get_openai_processing_region($aimogen_Main_Settings) !== 'eu') { $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'); } } if($is_astra_model) { $reasoning_effort = isset($aimogen_Main_Settings['api_reasoning']) ? trim($aimogen_Main_Settings['api_reasoning']) : ''; $base_params['reasoning'] = array('effort' => aimogen_normalize_astra_reasoning_effort($reasoning_effort)); if(isset($aimogen_Main_Settings['api_verbosity']) && $aimogen_Main_Settings['api_verbosity'] != '') { $base_params['text'] = array('verbosity' => trim($aimogen_Main_Settings['api_verbosity'])); } $base_params = aimogen_filter_astra_openai_request_options($base_params); } 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'); 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 = aimogen_get_openai_api_base_url($aimogen_Main_Settings); } $api_call = wp_remote_post( $api_endpoint . '/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($is_astra_model && aimogen_is_non_retryable_astra_error($api_call->get_error_message())) { $error = 'Administrator review required: Astra stopped this conversation with a non-retryable misalignment policy violation. Preserve the request, response, and tool-call IDs for review.'; return false; } 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($is_astra_model && ((isset($error_data->code) && aimogen_is_non_retryable_astra_error((string) $error_data->code)) || (isset($error_data->message) && aimogen_is_non_retryable_astra_error((string) $error_data->message)))) { $error = 'Administrator review required: Astra stopped this conversation with a non-retryable misalignment policy violation. Preserve the request, response, and tool-call IDs for review.'; return false; } 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 = '') { if(defined('AIMOGEN_ASSISTANTS_API_RETIRED') && AIMOGEN_ASSISTANTS_API_RETIRED) { $finish_reason = ''; $fallback_error = ''; $available_tokens = defined('AIMOGEN_DEFAULT_MAX_TOKENS') ? AIMOGEN_DEFAULT_MAX_TOKENS : 2048; do_action('aimogen_assistant_fallback_to_default_model', $assistant_id, AIMOGEN_DEFAULT_MODEL, $content); $response_text = aimogen_generate_text($token, AIMOGEN_DEFAULT_MODEL, $content, $available_tokens, 1, 1, 0, 0, true, $env, $retry_count, $finish_reason, $fallback_error, $no_internet, $no_embeddings, $stream, $vision_file, $content, $role, '', $thread_id, $embedding_namespace, $function_result, $file_data, false, false, array(), ''); if($response_text === false) { return false; } return array( 'content' => array( array( 'type' => 'text', 'text' => array( 'value' => $response_text, 'annotations' => array() ) ) ) ); } $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); if(empty($appids_claude)) { aimogen_log_to_file('No valid Anthropic Claude API keys found in the plugin settings!'); return false; } $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); if(empty($appids_hugging)) { aimogen_log_to_file('No valid HuggingFace API keys found in the plugin settings!'); return false; } $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); if(empty($ollama_url)) { aimogen_log_to_file('No valid Ollama API URLs found in the plugin settings!'); return false; } $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); if(empty($appids_openrouter)) { aimogen_log_to_file('No valid OpenRouter API keys found in the plugin settings!'); return false; } $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); if(empty($appids_perplexity)) { aimogen_log_to_file('No valid PerplexityAI API keys found in the plugin settings!'); return false; } $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); if(empty($appids_groq)) { aimogen_log_to_file('No valid Groq API keys found in the plugin settings!'); return false; } $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); if(empty($appids_nvidia)) { aimogen_log_to_file('No valid Nvidia API keys found in the plugin settings!'); return false; } $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); if(empty($appids_xai)) { aimogen_log_to_file('No valid xAI API keys found in the plugin settings!'); return false; } $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(aimogen_is_google_model($model) === 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); if(empty($appids_google)) { aimogen_log_to_file('No valid Google AI Studio API keys found in the plugin settings!'); return false; } $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) || aimogen_is_openai_astra_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' || aimogen_is_openai_astra_model($model)) ) { $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 (!aimogen_is_non_retryable_astra_error($error) && 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 } elseif (aimogen_get_openai_processing_region($aimogen_Main_Settings) !== 'eu') { $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 = aimogen_get_openai_api_base_url($aimogen_Main_Settings); } $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); if(empty($appids_google)) { aimogen_log_to_file('No valid Google AI Studio API keys found in the plugin settings!'); return false; } $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); if (empty($appids_google)) { aimogen_log_to_file('No valid Google AI Studio API keys found in the plugin settings!'); return false; } $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.1-flash-lite-preview': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite-preview:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite-preview:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aiomatic_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 = aiomatic_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . print_r($e->getMessage(), true); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . print_r($js_arr, true); return false; } break; case 'gemini-3.1-pro-preview': if($stream == false) { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview:generateContent?key=" . $token; } else { $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview:streamGenerateContent?key=" . $token; } if (!empty($vision_file)) { $base64_vision = aiomatic_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 = aiomatic_safe_json_encode($js_arr); } catch(Exception $e) { $error = 'Error: Exception in the API payload encoding: ' . print_r($e->getMessage(), true); return false; } if(empty($postData)) { $error = 'Error: Failed to encode post data: ' . print_r($js_arr, true); 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); if(empty($appids)) { throw new Exception('You need to add a valid API key in plugin settings for this shortcode to work.'); } $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.'); } } $api_endpoint = aimogen_get_openai_api_base_url(); $response = wp_remote_post( $api_endpoint . '/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); if(empty($appids)) { throw new Exception('You need to add a valid API key in plugin settings for this shortcode to work.'); } $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 = []; $api_endpoint = aimogen_get_openai_api_base_url(); foreach ( $video_ids as $video_id ) { $response = wp_remote_get( $api_endpoint . '/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 { $api_endpoint = aimogen_get_openai_api_base_url(); $response = wp_remote_get( $api_endpoint . '/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' ); $api_endpoint = aimogen_get_openai_api_base_url(); $response = wp_remote_get( $api_endpoint . '/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' ); } $api_endpoint = aimogen_get_openai_api_base_url(); $response = wp_remote_request( $api_endpoint . '/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' ); } $api_endpoint = aimogen_get_openai_api_base_url(); $response = wp_remote_get( $api_endpoint . '/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' || $model == 'claude-opus-4-5-20251101' || $model == 'claude-opus-4-6' || $model == 'claude-opus-4-7' || $model == 'claude-opus-4-8' || $model == 'claude-opus-5' || $model == 'claude-sonnet-4-6') { 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 . "