'code_interpreter']; $files_ok = true; } if($file_search == 'on') { $tools[] = ['type' => 'file_search']; $files_ok = true; } $functions_json = json_decode($functions_str, true); if($functions_json === null) { $functions = array(); } else { if(is_array($functions_json) && !isset($functions_json['name'])) { $functions = $functions_json; } elseif(isset($functions_json['name'])) { $functions = array($functions_json); } else { $functions = array(); } } foreach($functions as $func) { $tools[] = ['type' => 'function', 'function' => $func]; } if($files_ok === false) { $assistant_files = []; } try { $metadata = ''; $assistantData = aimogen_openai_save_assistant( $token, $model, $title, $description, $temperature, $topp, $prompt, $tools, $assistant_files, $metadata, $vector_store_id ); if($assistantData === false) { $aimogen_result['msg'] = 'Failed to save assistant using the API'; return $aimogen_result; } if(!isset($assistantData['id'])) { $aimogen_result['msg'] = 'Failed to decode assistant saving request: ' . json_encode($assistantData); return $aimogen_result; } $assistant_id = $assistantData['id']; } catch(Exception $e) { $aimogen_result['msg'] = 'Exception occured during Assistant saving: ' . $e->getMessage(); return $aimogen_result; } if(empty($assistant_id)) { $aimogen_result['msg'] = 'Failed to insert assistant to AI service: ' . $title; return $aimogen_result; } $aimogen_result = array('status' => 'error', 'msg' => 'Something went wrong assistant saving'); $assistant_data = array( 'post_type' => 'aimogen_assistants', 'post_title' => $title, 'post_content' => $prompt, 'post_excerpt' => $description, 'post_status' => 'publish' ); remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses'); $local_assistant_id = wp_insert_post($assistant_data); add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses'); if(is_wp_error($local_assistant_id)) { $aimogen_result['msg'] = $local_assistant_id->get_error_message(); } elseif($local_assistant_id === 0) { $aimogen_result['msg'] = 'Failed to insert assistant to database: ' . $title; } else { update_post_meta($local_assistant_id, '_assistant_id', $assistant_id); if(!empty($assistant_first_message)) { update_post_meta($local_assistant_id, '_assistant_first_message', $assistant_first_message); } else { update_post_meta($local_assistant_id, '_assistant_first_message', ''); } if(!empty($model)) { update_post_meta($local_assistant_id, '_assistant_model', $model); } if(!empty($vector_store_id)) { update_post_meta($local_assistant_id, '_assistant_vector_store_id', $vector_store_id); } if(!empty($tools)) { update_post_meta($local_assistant_id, '_assistant_tools', $tools); } else { update_post_meta($local_assistant_id, '_assistant_tools', array()); } if(!empty($temperature)) { update_post_meta($local_assistant_id, '_assistant_temperature', $temperature); } if(!empty($topp)) { update_post_meta($local_assistant_id, '_assistant_topp', $topp); } if(!empty($assistant_files)) { update_post_meta($local_assistant_id, '_assistant_files', $assistant_files); } else { update_post_meta($local_assistant_id, '_assistant_files', array()); } if(!empty($avatar)) { if(!function_exists('set_post_thumbnail')) { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); } $res2 = set_post_thumbnail($local_assistant_id, $avatar); if ($res2 === FALSE) { $aimogen_result['msg'] = 'Failed to insert assistant avatar to database: ' . $avatar; } } $aimogen_result['status'] = 'success'; $aimogen_result['id'] = $local_assistant_id; } return $aimogen_result; } function aimogen_update_assistant($token, $assistant_id, $assistant_id_local, $title, $model, $prompt, $description, $temperature, $topp, $assistant_first_message, $avatar, $code_interpreter, $file_search, $assistant_files, $functions_str = '') { $aimogen_result = array('status' => 'error', 'msg' => 'Something went wrong assistant updating'); require_once (dirname(__FILE__) . "/res/aimogen-assistants-api.php"); if(empty($title)) { $aimogen_result['msg'] = 'You need to add a name for the assistant!'; return $aimogen_result; } if(empty($model)) { $aimogen_result['msg'] = 'You need to add a model for the assistant!'; return $aimogen_result; } $vector_store_id = null; $files_ok = false; $tools = []; if($code_interpreter == 'on') { $tools[] = ['type' => 'code_interpreter']; $files_ok = true; } if($file_search == 'on') { $tools[] = ['type' => 'file_search']; $files_ok = true; } $functions_json = json_decode($functions_str, true); if($functions_json === null) { $functions = array(); } else { if(is_array($functions_json) && !isset($functions_json['name'])) { $functions = $functions_json; } elseif(isset($functions_json['name'])) { $functions = array($functions_json); } else { $functions = array(); } } foreach($functions as $func) { $tools[] = ['type' => 'function', 'function' => $func]; } if($files_ok === false) { $assistant_files = []; } try { $address_post_id = ''; $metadata = ''; $assistantData = aimogen_openai_modify_assistant( $token, $assistant_id, $model, $title, $description, $prompt, $temperature, $topp, $tools, $assistant_files, $metadata, $vector_store_id, $address_post_id ); if($assistantData === false) { $aimogen_result['msg'] = 'Failed to update assistant using the API'; return $aimogen_result; } if(!isset($assistantData['id'])) { $aimogen_result['msg'] = 'Failed to decode assistant updating request: ' . json_encode($assistantData); return $aimogen_result; } $assistant_id = $assistantData['id']; } catch(Exception $e) { $aimogen_result['msg'] = 'Exception occured during Assistant updating: ' . $e->getMessage(); return $aimogen_result; } if(empty($assistant_id)) { $aimogen_result['msg'] = 'Failed to update assistant to AI service: ' . $title; return $aimogen_result; } $assistant_data = array( 'post_type' => 'aimogen_assistants', 'post_title' => $title, 'post_content' => $prompt, 'post_excerpt' => $description, 'post_status' => 'publish', 'ID' => $assistant_id_local ); remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses'); $local_assistant_id = wp_update_post($assistant_data); add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses'); if(is_wp_error($local_assistant_id)) { $aimogen_result['msg'] = $local_assistant_id->get_error_message(); } elseif($local_assistant_id === 0) { $aimogen_result['msg'] = 'Failed to update assistant to database: ' . $title; } else { $vector_store_id = get_post_meta($local_assistant_id, '_assistant_vector_store_id', true); if(!empty($vector_store_id)) { $solved = false; $ls_fid = []; try { $list_stores = aimogen_openai_list_vector_store_files($token, $vector_store_id); } catch(Exception $e) { if(strstr($e->getMessage(), 'No vector store found with id') !== false) { $vs = aimogen_openai_create_vector_store($token, 'Assistant Vector Store', $assistant_files); if(isset($vs['id'])) { update_post_meta($local_assistant_id, '_assistant_vector_store_id', $vs['id']); $solved = true; $vector_store_id = $vs['id']; $assistantData = aimogen_openai_modify_assistant( $token, $assistant_id, $model, $title, $description, $prompt, $temperature, $topp, $tools, $assistant_files, $metadata, $vector_store_id, $address_post_id ); if($assistantData === false) { $aimogen_result['msg'] = 'Failed to update assistant vector store using the API'; return $aimogen_result; } if(!isset($assistantData['id'])) { $aimogen_result['msg'] = 'Failed to decode assistant vector store updating request: ' . json_encode($assistantData); return $aimogen_result; } } } else { throw $e; } } if(!$solved) { foreach($list_stores as $ls) { $ls_fid[] = $ls['id']; } foreach($assistant_files as $fid) { if(!in_array($fid, $ls_fid)) { aimogen_openai_create_vector_store_file($token, $vector_store_id, $fid); } } foreach($ls_fid as $fid) { if(!in_array($fid, $assistant_files)) { aimogen_openai_delete_vector_store_file($token, $vector_store_id, $fid); } } $assistantData = aimogen_openai_modify_assistant( $token, $assistant_id, $model, $title, $description, $prompt, $temperature, $topp, $tools, $assistant_files, $metadata, $vector_store_id, $address_post_id ); if($assistantData === false) { $aimogen_result['msg'] = 'Failed to update assistant vector store db using the API'; return $aimogen_result; } if(!isset($assistantData['id'])) { $aimogen_result['msg'] = 'Failed to decode assistant vector store db updating request: ' . json_encode($assistantData); return $aimogen_result; } } } else { $vs = aimogen_openai_create_vector_store($token, 'New Assistant Vector Store', $assistant_files); if(isset($vs['id'])) { update_post_meta($local_assistant_id, '_assistant_vector_store_id', $vs['id']); $vector_store_id = $vs['id']; $assistantData = aimogen_openai_modify_assistant( $token, $assistant_id, $model, $title, $description, $prompt, $temperature, $topp, $tools, $assistant_files, $metadata, $vector_store_id, $address_post_id ); if($assistantData === false) { $aimogen_result['msg'] = 'Failed to update assistant vector store using the API'; return $aimogen_result; } if(!isset($assistantData['id'])) { $aimogen_result['msg'] = 'Failed to decode assistant vector store updating request: ' . json_encode($assistantData); return $aimogen_result; } } } update_post_meta($local_assistant_id, '_assistant_id', $assistant_id); if(!empty($assistant_first_message)) { update_post_meta($local_assistant_id, '_assistant_first_message', $assistant_first_message); } else { update_post_meta($local_assistant_id, '_assistant_first_message', ''); } if(!empty($model)) { update_post_meta($local_assistant_id, '_assistant_model', $model); } if(!empty($temperature)) { update_post_meta($local_assistant_id, '_assistant_temperature', $temperature); } else { update_post_meta($local_assistant_id, '_assistant_temperature', ''); } if(!empty($topp)) { update_post_meta($local_assistant_id, '_assistant_topp', $topp); } else { update_post_meta($local_assistant_id, '_assistant_topp', ''); } if(!empty($tools)) { update_post_meta($local_assistant_id, '_assistant_tools', $tools); } else { update_post_meta($local_assistant_id, '_assistant_tools', array()); } if(!empty($assistant_files)) { update_post_meta($local_assistant_id, '_assistant_files', $assistant_files); } else { update_post_meta($local_assistant_id, '_assistant_files', array()); } if(!empty($avatar)) { if(!function_exists('set_post_thumbnail')) { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); } $res2 = set_post_thumbnail($local_assistant_id, $avatar); if ($res2 === FALSE) { $aimogen_result['msg'] = 'Failed to insert assistant avatar to database: ' . $avatar; } } else { if($avatar === '0' || $avatar === 0) { delete_post_thumbnail($local_assistant_id); } } $aimogen_result['status'] = 'success'; $aimogen_result['id'] = $local_assistant_id; } return $aimogen_result; } function aimogen_save_assistant_only_local($token, $title, $model, $prompt, $description, $temperature, $topp, $assistant_first_message, $avatar, $assistant_files, $assistant_id, $created_at, $tools, $vector_store_id) { if(empty($title)) { $title = 'Untitled Assistant'; } $args = array( 'post_type' => 'aimogen_assistants', 'meta_query' => array( array( 'key' => '_assistant_id', 'value' => $assistant_id, 'compare' => 'EXISTS' ), ), ); $updated = false; $query = new WP_Query( $args ); require_once (dirname(__FILE__) . "/res/aimogen-assistants-api.php"); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $post_id = get_the_ID(); $ass_id = get_post_meta($post_id, '_assistant_id', true); if(!empty($ass_id)) { $failed = false; try { $assistant = aimogen_openai_retrieve_assistant($token, $ass_id); if(!isset($assistant['id'])) { throw new Exception('Incorrect response from assistant grabbing: ' . json_encode($assistant)); } } catch(Exception $e) { aimogen_log_to_file('Exception in assistant grabbing: ' . $e->getMessage()); $failed = true; } if($failed == false) { if(empty($assistant['description'])) { $assistant['description'] = ''; } if(empty($assistant['instructions'])) { $assistant['instructions'] = ''; } $assistant_data = array( 'post_type' => 'aimogen_assistants', 'post_title' => $assistant['name'], 'post_content' => $assistant['instructions'], 'post_excerpt' => $assistant['description'], 'post_status' => 'publish', 'ID' => $post_id ); remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses'); $local_assistant_id = wp_update_post($assistant_data); add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses'); if(is_wp_error($local_assistant_id)) { aimogen_log_to_file('Failed to update assistant ' . $local_assistant_id->get_error_message()); } elseif($local_assistant_id === 0) { aimogen_log_to_file('Failed to update assistant to database: ' . $assistant['name']); } else { $updated = true; $file_ids = array(); if(isset($assistant['tool_resources']['code_interpreter']['file_ids'][0])) { $file_ids = $assistant['tool_resources']['code_interpreter']['file_ids']; } update_post_meta($local_assistant_id, '_assistant_model', $assistant['model']); update_post_meta($local_assistant_id, '_assistant_tools', (array) $assistant['tools']); update_post_meta($local_assistant_id, '_assistant_files', $file_ids); if(!empty($vector_store_id)) { update_post_meta($local_assistant_id, '_assistant_vector_store_id', $vector_store_id); } if(!empty($temperature)) { update_post_meta($local_assistant_id, '_assistant_temperature', $temperature); } if(!empty($topp)) { update_post_meta($local_assistant_id, '_assistant_topp', $topp); } $aimogen_result['status'] = 'success'; $aimogen_result['id'] = $local_assistant_id; } } } } } if(!$updated) { if(empty($title)) { $aimogen_result['msg'] = 'You need to add a name for the assistant!'; return $aimogen_result; } if(empty($model)) { $aimogen_result['msg'] = 'You need to add a model for the assistant!'; return $aimogen_result; } if(empty($prompt)) { $prompt = ''; } if(empty($description)) { $description = ''; } $postdate = gmdate("Y-m-d H:i:s", $created_at); $aimogen_result = array('status' => 'error', 'msg' => 'Something went wrong assistant saving'); $assistant_data = array( 'post_type' => 'aimogen_assistants', 'post_title' => $title, 'post_content' => $prompt, 'post_excerpt' => $description, 'post_date' => $postdate, 'post_status' => 'publish' ); remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses'); $local_assistant_id = wp_insert_post($assistant_data); add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses'); if(is_wp_error($local_assistant_id)) { $aimogen_result['msg'] = $local_assistant_id->get_error_message(); } elseif($local_assistant_id === 0) { $aimogen_result['msg'] = 'Failed to insert assistant to database: ' . $title; } else { update_post_meta($local_assistant_id, '_assistant_id', $assistant_id); if(!empty($assistant_first_message)) { update_post_meta($local_assistant_id, '_assistant_first_message', $assistant_first_message); } else { update_post_meta($local_assistant_id, '_assistant_first_message', ''); } if(!empty($model)) { update_post_meta($local_assistant_id, '_assistant_model', $model); } if(!empty($tools)) { update_post_meta($local_assistant_id, '_assistant_tools', (array)$tools); } else { update_post_meta($local_assistant_id, '_assistant_tools', array()); } if(!empty($vector_store_id)) { update_post_meta($local_assistant_id, '_assistant_vector_store_id', $vector_store_id); } if(!empty($temperature)) { update_post_meta($local_assistant_id, '_assistant_temperature', $temperature); } if(!empty($topp)) { update_post_meta($local_assistant_id, '_assistant_topp', $topp); } if(!empty($assistant_files)) { update_post_meta($local_assistant_id, '_assistant_files', $assistant_files); } else { update_post_meta($local_assistant_id, '_assistant_files', array()); } if(!empty($avatar)) { if(!function_exists('set_post_thumbnail')) { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); } $res2 = set_post_thumbnail($local_assistant_id, $avatar); if ($res2 === FALSE) { $aimogen_result['msg'] = 'Failed to insert assistant avatar to database: ' . $avatar; } } $aimogen_result['status'] = 'success'; $aimogen_result['id'] = $local_assistant_id; } } return $aimogen_result; } ?>