Plugin Directory

Changeset 3274880


Ignore:
Timestamp:
04/16/2025 03:04:15 PM (12 months ago)
Author:
oc3dots
Message:

Refining queries by adding keywords to each search request that is sent to the semantic database

Location:
s2b-ai-assistant/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • s2b-ai-assistant/trunk/lib/S2bAia.php

    r3252391 r3274880  
    1818
    1919        public function enqueueScripts() {
    20             wp_enqueue_script('s2baia_backend', S2BAIA_URL . '/views/resources/js/s2baia-admin.js', [], '2.35', true);
    21             wp_enqueue_script('s2baia_backendv2', S2BAIA_URL . '/views/resources/js/s2baia-admin-v2.js', [], '2.35', true);
     20            wp_enqueue_script('s2baia_backend', S2BAIA_URL . '/views/resources/js/s2baia-admin.js', [], '2.37', true);
     21            wp_enqueue_script('s2baia_backendv2', S2BAIA_URL . '/views/resources/js/s2baia-admin-v2.js', [], '2.37', true);
    2222        }
    2323
  • s2b-ai-assistant/trunk/lib/controllers/AdminChatBotController.php

    r3252391 r3274880  
    316316                }
    317317               
     318                if (isset($_POST[ 's2baia_chatbot_rag_keywords'])) {//--
     319                    $data['rag_keywords']  = sanitize_text_field(wp_unslash($_POST[ 's2baia_chatbot_rag_keywords']));
     320                }
    318321               
    319322               
     
    337340                if (isset($_POST[ 's2baia_rag_index'])) {//--
    338341                    $data['rag_index']  = sanitize_text_field(wp_unslash($_POST[ 's2baia_rag_index']));
     342                }
     343               
     344                if (isset($_POST[ 's2baia_chatbot_rag_keywords'])) {//--
     345                    $data['rag_keywords']  = sanitize_text_field(wp_unslash($_POST[ 's2baia_chatbot_rag_keywords']));
    339346                }
    340347               
  • s2b-ai-assistant/trunk/lib/controllers/ChatBotController.php

    r3252391 r3274880  
    5252
    5353            wp_enqueue_script( 's2baia', S2BAIA_URL . '/views/frontend/resources/js/chatbot.js',  array( 'jquery' ), S2BAIA_VERSION, false );
     54            $new_script = apply_filters( 's2baia_chatbot_enqueue_script',['include' => false,'handle'=>'','src'=>'','deps'=>[],'ver'=>false,'args'=>[]] );
     55            if(is_array($new_script) && isset($new_script['include']) && $new_script['include']){
     56                wp_enqueue_script( $new_script['handle'], $new_script['src'],  $new_script['deps'], $new_script['ver'], $new_script['args'] );
     57            }
    5458        }
    5559       
     
    6872                    '1.0.2'
    6973            );
     74            $new_style = apply_filters( 's2baia_chatbot_enqueue_style',['include' => false,'handle'=>'','src'=>'','deps'=>[],'ver'=>false,'media'=>'all'] );
     75            if(is_array($new_style) && isset($new_style['include']) && $new_style['include']){
     76                wp_enqueue_style(
     77                    $new_style['handle'],
     78                    $new_style['src'],
     79                    $new_style['deps'],
     80                    $new_style['ver'],
     81                    $new_style['media']   
     82                );
     83            }
    7084        }
    7185
     
    126140                    if(isset($chat_bot_styles->bot_options['rag_index'])){
    127141                        unset($chat_bot_styles->bot_options['rag_index']);
     142                    }
     143                    if(isset($chat_bot_styles->bot_options['rag_keywords'])){
     144                        unset($chat_bot_styles->bot_options['rag_keywords']);
    128145                    }
    129146                }
     
    234251                default:
    235252                   
    236                             $data_parameters['bot_view'] = 1;
    237                             //$content = $this->showClassicChatGPTDefaultHistory($data_par,$data_parameters);
    238 
     253                            //$data_parameters['bot_view'] = 1;
     254                            $content =  apply_filters( 's2baia_chatbot_render_view', [$data_par,$data_parameters] );
     255                            return $content;
     256
     257                           
    239258            }
    240259                if($view !== 'default'){
     
    632651                    $rag_provider = sanitize_text_field($params['s2baia_chatbot_opt_rag_index']);
    633652                    $use_rag = true;
     653                   
    634654                    $search_content_success = false;
    635655                    $s2baia_config_embedding_break_not_found = (int) get_option(S2BAIA_PREFIX_LOW . 'config_embedding_break_not_found', 0);
     
    642662                    $config_pinecone_confidence = (int) (get_option(S2BAIA_PREFIX_LOW . 'config_pinecone_confidence', 50));
    643663                    $config_pinecone_namespace = sanitize_text_field(get_option(S2BAIA_PREFIX_LOW . 'config_pinecone_namespace', ''));
    644 
    645                     $embedded_res = S2bAia_RagUtils::makeEmbedingCall($new_message);
     664                    $emb_new_msg = $new_message;
     665                    $rag_keywords = sanitize_text_field($params['s2baia_chatbot_opt_rag_keywords']);
     666                    if(strlen($rag_keywords) > 0){
     667                        $emb_new_msg =  $new_message.' '.$rag_keywords;
     668                    }
     669                    $embedded_res = S2bAia_RagUtils::makeEmbedingCall($emb_new_msg);
    646670                    if (isset($embedded_res['status']) && $embedded_res['status'] == 200) {
    647671                        $embedded_question = $embedded_res['embedding'];
     
    844868                    $config_pinecone_confidence = (int) (get_option(S2BAIA_PREFIX_LOW . 'config_pinecone_confidence', 50));
    845869                    $config_pinecone_namespace = sanitize_text_field(get_option(S2BAIA_PREFIX_LOW . 'config_pinecone_namespace', ''));
    846 
    847                     $embedded_res = S2bAia_RagUtils::makeEmbedingCall($new_message);
     870                    $emb_new_msg = $new_message;
     871                    $rag_keywords = sanitize_text_field($params['s2baia_chatbot_opt_rag_keywords']);
     872                    if(strlen($rag_keywords) > 0){
     873                        $emb_new_msg =  $new_message.' '.$rag_keywords;
     874                    }
     875                    $embedded_res = S2bAia_RagUtils::makeEmbedingCall($emb_new_msg);
    848876                    if (isset($embedded_res['status']) && $embedded_res['status'] == 200) {
    849877                        $embedded_question = $embedded_res['embedding'];
  • s2b-ai-assistant/trunk/lib/helpers/WpHttpClient.php

    r3252391 r3274880  
    2323
    2424        public static function handleEvent($data) {
    25             //$debugger = S2bAia_Utils::$global_logger;
    26             //$debug_log = (get_option( 's2baia_debug', 0 ) > 0) && is_object($debugger) && strlen(S2bAia_Utils::$log_id) > 0;
     25            $debugger = S2bAia_Utils::$global_logger;
     26            $debug_log = (get_option( 's2baia_debug', 0 ) > 0) && is_object($debugger) && strlen(S2bAia_Utils::$log_id) > 0;
    2727           
    2828           
    2929           
    30    
     30            if (!empty($data)) {
     31                $obj = json_decode($data, true);
     32
     33                if (!class_exists('S2bAia_FileExtender')) {
     34                    require_once S2BAIA_PATH . '/lib/classes/FileExtender.php';
     35                }
     36
     37                $fext = new S2bAia_FileExtender();
     38               
     39                // 🔹 Handle Regular Text Responses
     40                if ($obj && isset($obj['delta']['content'])) {
     41                    foreach ($obj['delta']['content'] as $content) {
     42                        if ($content['type'] === 'text' && isset($content['text']['value'])) {
     43                            $text_value = $content['text']['value'];
     44                            $annotations = isset($content['text']['annotations']) ? $content['text']['annotations'] : [];
     45                            foreach ($annotations as $annotation) {
     46                                self::$annotations[] = $annotation;
     47                            }
     48                            // Process text with annotations (e.g., file citations)
     49                            $filtered_text = $text_value; //$fext->assistantapiFilterResponseStream($text_value, $annotations, $obj,self::$annotations);
     50                            self::$stream_answer .= $filtered_text;
     51                        }
     52                    }
     53                }
     54                // 🔹 2. Handle `tool_calls` from Final API Response
     55                if ($obj && isset($obj['step_details']['tool_calls'])) {
     56                    $fl2 = __DIR__ . "/tool_calls" . rand(10000, 99999) . ".txt";
     57                    $logvar = $obj;
     58                    //error_log(print_r($logvar, true), 3, $fl2);
     59                    if($debug_log){
     60                        $ldata = [];
     61                        $ldata['id_user'] = get_current_user_id();
     62                        $ldata['typeof_message'] = 1;
     63                        $ldata['id_actor'] = '';
     64                        $ldata['messages'] = wp_json_encode($obj);
     65                        $ldata['sourcetable'] = 's2baia_messages_log';
     66                        $ldata['ipaddress'] = sanitize_text_field(S2bAia_Utils::getIpAddress());
     67                        $ldata['event_id'] = sanitize_text_field(S2bAia_Utils::$log_id);
     68                        $ldata['comments'] = sanitize_text_field('handleEvent debug $object');
     69                        $debugger->insertDebugRecord($ldata);
     70                    }
     71                    foreach ($obj['step_details']['tool_calls'] as $tool_call) {
     72                        if ($tool_call['type'] === 'code_interpreter' && isset($tool_call['code_interpreter']['outputs'])) {
     73                            foreach ($tool_call['code_interpreter']['outputs'] as $output) {
     74                                if (isset($output['file_id'])) {
     75                                    $file_id = $output['file_id'];
     76
     77                                    // 🔹 Handle the file link properly
     78                                    $filtered_text = $fext->assistantapiFilterResponseStream("", [], ["file_id" => $file_id, "file_name" => basename($file_id)]);
     79                                    self::$stream_answer .= $filtered_text;
     80                                }
     81                            }
     82                        }
     83
     84                        if ($tool_call['type'] === 'code_interpreter' && isset($tool_call['code_interpreter']['input'])) {
     85                            $input_text = trim($tool_call['code_interpreter']['input']);
     86                            //self::$partial_file_path .= $input_text;
     87                            // 🔹 Check if the input contains a file path instead of Python code
     88                            $matches = [];
     89                            if (preg_match('/\/mnt\/data\/.*\.(docx|pdf|txt|csv)$/', self::$partial_file_path, $matches)) {
     90                                $file_id = $matches[0]; // Extract full file path
     91                                self::$partial_file_path = ''; // Reset buffer
     92                                // 🔹 Process only actual filenames (ignore Python code)
     93                                $filtered_text = $fext->assistantapiFilterResponseStream("", [], ["file_id" => $file_id, "file_name" => basename($file_id)]);
     94                                self::$stream_answer .= $filtered_text;
     95                            } else {
     96                                // 🔹 Ignore Python execution commands (they are not file paths)
     97                                self::$partial_file_path .= $input_text;
     98                            }
     99                        }
     100                    }
     101                }
     102
     103               
     104                if ($obj && isset($obj['object']) && $obj['object'] == 'thread.message' && isset($obj['status']) && $obj['status'] == 'completed' && isset($obj['content'])) {
     105                    $fl2 = __DIR__ . "/response_assistant_message_completed" . rand(10000, 99999) . ".txt";
     106                    $logvar = $obj;
     107                    //error_log(print_r($logvar, true), 3, $fl2);
     108                    if($debug_log){
     109                        $ldata = [];
     110                        $ldata['id_user'] = get_current_user_id();
     111                        $ldata['typeof_message'] = 1;
     112                        $ldata['id_actor'] = '';
     113                        $ldata['messages'] = wp_json_encode($obj);
     114                        $ldata['sourcetable'] = 's2baia_messages_log';
     115                        $ldata['ipaddress'] = sanitize_text_field(S2bAia_Utils::getIpAddress());
     116                        $ldata['event_id'] = sanitize_text_field(S2bAia_Utils::$log_id);
     117                        $ldata['comments'] = sanitize_text_field('handleEvent debug $object');
     118                        $debugger->insertDebugRecord($ldata);
     119                    }
     120                    $content = $obj['content'];
     121                    if (is_array($content) && count($content) > 0) {
     122
     123                        $content = $content[0];
     124                        if (isset($content['type']) && $content['type'] == 'text' && is_array($content['text'])) {
     125                            $text = $content['text'];
     126                            if (is_array($text) && isset($text['value']) && isset($text['annotations'])) {
     127                                $annotations = $text['annotations'];
     128                                foreach ($annotations as $annotation) {
     129                                    self::$annotations[] = $annotation;
     130                                }
     131                                $text_value = $text['value'];
     132                                //$filtered_text = $fext->assistantapiFilterResponseStream($text_value, $annotations, $content,self::$annotations);
     133                                //self::$stream_answer .= $filtered_text;
     134                                self::$thread_message = $obj;
     135                            }
     136                        }
     137                    }
     138                }
     139
     140                return $obj;
     141            }
    31142            return $data;
    32143        }
  • s2b-ai-assistant/trunk/readme.txt

    r3252391 r3274880  
    44Tags: chatbot, gpt,  AI, content generator,  openai
    55Requires at least: 5.6
    6 Tested up to: 6.7
     6Tested up to: 6.8
    77Requires PHP: 7.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    10 Stable tag: 1.7.0
     10Stable tag: 1.7.1
    1111
    1212Create multiple AI chatbots with OpenAI and xAI models,  with different styles and behavior, content aware features   ...
     
    3939* Summarizing
    4040* Finish the sentence
    41 * Improve your writing skills
    42 * Engaging questions
    4341* Answering All Inquiries
    4442* Create product descriptions
    45 * Intent Recognition
    4643* Code Understanding
    4744* Select any model you want from ChatGpt
    4845* Create a blog intro for your next article
    49 * Create blog outlines
    50 * Change the tone of your sentences
    5146* Create brand names, startup ideas and slogans
    5247* Generate a list of marketing ideas
     
    271266== Changelog ==
    272267
     268= 1.7.1 =
     269* Refining queries by adding  keywords to each search request that is sent to the semantic database
     270* Adding native way of communication between Chatbot and  Assistant API.
     271
    273272= 1.7.0 =
    274273* Add xAI Grok model support for chatbot
  • s2b-ai-assistant/trunk/s2b-ai-assistant.php

    r3252391 r3274880  
    88  Text Domain: s2b-ai-assistant
    99  Domain Path: /lang
    10   Version: 1.7.0
     10  Version: 1.7.1
    1111  License:  GPL-2.0+
    1212  License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
  • s2b-ai-assistant/trunk/views/backend/chatbot/chatbot_chatbots.php

    r3252391 r3274880  
    897897                                                <p class="s2baia_input_description">
    898898                                                    <span style="display: inline;"><?php esc_html_e('Select embedding index', 's2b-ai-assistant'); ?></span>
     899                                                </p>
     900                                            </div>
     901                                </div>
     902                               
     903                                <div class="s2baia_block_content">
     904                                            <div class="s2baia_row_header">
     905                                                <label for="s2baia_rag_keywords"  font-weight: 700;"><?php esc_html_e('Additional Keywords', 's2b-ai-assistant'); ?> *:</label>
     906                                            </div>
     907                                            <div class="s2baia_row_content s2baia_pr">
     908                                                <div style="position: relative;">
     909                                                    <?php $rag_keywords = isset($chat_bot_options['rag_keywords']) ? $chat_bot_options['rag_keywords'] : ''; ?>
     910                                                    <input type="text" id="s2baia_chatbot_rag_keywords"
     911                                                           name="s2baia_chatbot_rag_keywords"
     912                                                           value="<?php echo esc_html($rag_keywords); ?>" />
     913                                                </div>
     914                                                <p class="s2baia_input_description">
     915                                                    <span style="display: inline;">
     916                                                        <?php esc_html_e('Additional keywords will be added to each user requests which are sent to semantic database. This is useful when for example user enters question: Which attributes of order item? But when you add Woocommerce as Additional keyword then next text will be sent to semantic database: "Which attributes of order item? Woocommerce". So additional keywords improve context. Note that additional keywords are not sent to chat bot provider. They are only sent to semantic search database. ', 's2b-ai-assistant'); ?>
     917                                                    </span>
    899918                                                </p>
    900919                                            </div>
  • s2b-ai-assistant/trunk/views/backend/chatbot/chatbot_general.php

    r3252391 r3274880  
    674674                                </div>
    675675                                <?php
    676                                 if(false){
     676                                if(true){
    677677                                ?>
    678678                                <div class="s2baia_block_header">
  • s2b-ai-assistant/trunk/views/resources/js/s2baia-admin-v2.js

    r3252391 r3274880  
    508508            document.querySelector('#s2baia_rag_index'+itmsuffix).value = bot_options['rag_index'];
    509509           
     510            let s2brag_keywords = document.querySelector('#s2baia_chatbot_rag_keywords'+itmsuffix);
     511            if(s2brag_keywords){
     512                if(bot_options['rag_keywords']){
     513                    document.querySelector('#s2baia_chatbot_rag_keywords'+itmsuffix).value = bot_options['rag_keywords'];
     514                }else{
     515                    document.querySelector('#s2baia_chatbot_rag_keywords'+itmsuffix).value = '';
     516                }
     517            }
    510518           
    511519            let buttons = {'s2baia_submit_edit_instruction': [1, 1, 'Save'], 's2baia_saveasnew_instruction': [1, 1, ''],
Note: See TracChangeset for help on using the changeset viewer.