Plugin Directory

Changeset 3326212


Ignore:
Timestamp:
07/11/2025 11:12:31 AM (9 months ago)
Author:
samuelsilvapt
Message:

New version 1.6 : External AI service

Location:
ai-search
Files:
7 added
2 edited

Legend:

Unmodified
Added
Removed
  • ai-search/trunk/ai-search.php

    r3310573 r3326212  
    33 * Plugin Name: AI Search
    44 * Description: Replaces the default search with an intelligent search system.
    5  * Version: 1.5
     5 * Version: 1.6
    66 * Author: samuelsilvapt
    77 * License: GPL2
     
    1111    exit; // Exit if accessed directly.
    1212}
     13require_once plugin_dir_path( __FILE__ ) . 'includes/class-ai-search-service.php';
     14
    1315
    1416class AI_Search {
     
    4244        $this->api_key = get_option( 'ai_search_api_key', '' );
    4345        $this->similarity_threshold = get_option( 'ai_search_similarity_threshold', 0.5 );
     46        $this->provider = get_option( 'ai_search_provider', $this->api_key ? 'openai' : 'ai_service' );
     47        $this->service_token = get_option( 'ai_search_service_token', '' );
     48        $this->service_client = new AI_Search_Service();
     49
    4450        $this->register_hooks();
     51
     52        if ( $this->provider === 'ai_service' && empty( $this->service_token ) ) {
     53            $this->service_token = $this->service_client->register_origin();
     54        }
     55
     56
    4557    }
    4658
     
    112124            return $cached_embedding;
    113125        }
     126
     127        if ( $this->provider === 'ai_service' ) {
     128            return $this->service_client->get_embedding( $content );
     129        }
     130       
    114131        $response = wp_remote_post( 'https://api.openai.com/v1/embeddings', [
    115132            'headers' => [
     
    134151
    135152        // Cache the embedding for 1 day
    136         set_transient( 'ai_search_embedding_' . md5( $content ), $body['data'][0]['embedding'], DAY_IN_SECONDS );
     153        set_transient( 'ai_search_embedding_' . md5( $content ), $body['data'][0]['embedding'], MONTH_IN_SECONDS );
    137154        return $body['data'][0]['embedding'];
    138155    }
     
    237254            wp_die( __( 'Unauthorized access', 'ai-search' ) );
    238255        }
    239    
     256       
    240257        $cpt = sanitize_text_field( $_POST['cpt'] );
    241258        $limit = isset( $_POST['limit'] ) ? intval( $_POST['limit'] ) : 50;
     
    298315     */
    299316    private function general_settings_page() {
    300         if ( isset( $_POST['api_key'], $_POST['similarity_threshold'] ) ) {
     317        if ( isset( $_POST['provider'], $_POST['api_key'], $_POST['similarity_threshold'] ) ) {
    301318            check_admin_referer( 'ai_search_save_settings' );
     319            update_option( 'ai_search_provider', sanitize_text_field( $_POST['provider'] ) );
    302320            update_option( 'ai_search_api_key', sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) );
    303321            update_option( 'ai_search_similarity_threshold', floatval( $_POST['similarity_threshold'] ) );
    304322            echo '<div class="updated"><p>Settings saved successfully!</p></div>';
    305323        }
    306 
     324   
     325        $provider = get_option( 'ai_search_provider', $this->api_key ? 'openai' : 'ai_service' );
    307326        $api_key = get_option( 'ai_search_api_key', '' );
    308327        $similarity_threshold = get_option( 'ai_search_similarity_threshold', 0.5 );
    309 
     328   
    310329        echo '<form method="post" action="">';
    311330        wp_nonce_field( 'ai_search_save_settings' );
    312         echo '<label for="api_key">OpenAI API Key:</label>';
     331   
     332        echo '<label for="provider"><strong>AI Provider:</strong></label><br>';
     333        echo '<select id="provider" name="provider" onchange="aiSearchToggleApiKey()">';
     334        echo '<option value="ai_service"' . selected( $provider, 'ai_service', false ) . '>AI Search Service (Free up to 10,000 embeddings/site)</option>';
     335        echo '<option value="openai"' . selected( $provider, 'openai', false ) . '>Use your own OpenAI API Key</option>';
     336        echo '</select>';
     337        echo '<p><em>Choose between using our external service (no API key needed) or managing embeddings yourself with an OpenAI key. No personal data is stored or used for other purposes.</em></p>';
     338   
     339        echo '<br/><br/>';
     340   
     341        echo '<div id="openai-key-container" style="display: ' . ( $provider === 'openai' ? 'block' : 'none' ) . ';">';
     342        echo '<label for="api_key">OpenAI API Key:</label><br>';
    313343        echo '<input type="text" id="api_key" name="api_key" value="' . esc_attr( $api_key ) . '" style="width: 100%; max-width: 400px;" />';
     344        echo '</div>';
     345   
    314346        echo '<br/><br/>';
    315         echo '<label for="similarity_threshold">Similarity Threshold (0-1):</label>';
     347   
     348        echo '<label for="similarity_threshold">Similarity Threshold (0-1):</label><br>';
    316349        echo '<input type="range" id="similarity_threshold" name="similarity_threshold" min="0" max="1" step="0.01" value="' . esc_attr( $similarity_threshold ) . '" oninput="this.nextElementSibling.value = this.value">';
    317350        echo '<output>' . esc_attr( $similarity_threshold ) . '</output>';
     351        echo '<p><em>Defines how similar a post must be to appear in search results. Higher value means stricter, more relevant results. Default: 0.5.</em></p>';
     352
    318353        echo '<br/><br/>';
    319354        echo '<input type="submit" class="button-primary" value="Save Settings" />';
    320355        echo '</form>';
    321     }
     356   
     357        echo '<script>
     358        function aiSearchToggleApiKey() {
     359            var provider = document.getElementById("provider").value;
     360            document.getElementById("openai-key-container").style.display = (provider === "openai") ? "block" : "none";
     361        }
     362        </script>';
     363    }
     364   
    322365
    323366    /**
     
    363406    }
    364407
     408   
     409
    365410
    366411}
  • ai-search/trunk/readme.txt

    r3310573 r3326212  
    33Tags: search, AI, OpenAI, WordPress 
    44Tested up to: 6.8
    5 Stable tag: 1.5
     5Stable tag: 1.6
    66Requires PHP: 8.0
    77License: GPLv2
    8 Replaces the default search with an intelligent search system.
     8Replaces the default search with an intelligent search system provided by an AI service.
    99---
    1010
     
    1313AI Search for WordPress enhances the search experience by:
    1414- Replacing the default WordPress search with an AI-powered intelligent search system.
    15 - Generating embeddings for posts using OpenAI’s `text-embedding-ada-002` model.
    16 - Contextualizing search queries for better relevance and precision.
     15- Generating embeddings for posts using OpenAI’s `text-embedding-3-small` model via an external Node.js service (Open AI account isn't necessary).
     16- Managing embeddings centrally with per-origin quota control.
     17- Caching results locally using WordPress transients.
    1718- Ranking posts based on similarity using cosine similarity metrics.
     19
    1820
    1921
    2022## Features
    2123
    22 - **Intelligent Search**: Enhances default search functionality with AI-powered context inference.
    23 - **Embeddings Table**: Stores AI-generated embeddings for all posts in the database.
    24 - **Admin Settings**: Easily configure the plugin via the WordPress admin panel.
    25 - **Seamless Integration**: Automatically updates embeddings when posts are saved or updated.
    26 - Cached results for better performance
     24- **Intelligent Search**: AI-powered context inference.
     25- **Remote Embedding Service**: Embeddings stored in a central MySQL database through a Node.js API.
     26- **Admin Settings**: Manage global token and service URL.
     27- **Bulk Embedding Generation**: Generate embeddings for multiple posts or custom post types.
     28- **Caching**: Local transient caching.
    2729
    2830== Installation ==
    2931
    30 1. Upload the plugin folder to the `/wp-content/plugins/` directory.
    31 2. Activate the plugin through the 'Plugins' menu in WordPress.
    32 3. Go to **Settings > AI Search** to configure your OpenAI API key.
    33 4. Enjoy smarter search functionality on your site!
     321. Install and Activate the plugin through the 'Plugins' menu in WordPress.
     332. Go to **Settings > AI Search** and configure what you need.
     343. Run **Bulk Embedding Generation** via admin panel (the current posts have to be regenerated).
    3435
    3536
     
    4344
    4445### What is required to use AI Search for WordPress?
    45 You need an OpenAI API key to use this plugin. You can get one from the [OpenAI website](https://openai.com/).
     46If you want to use the service we provide: nothing. If you want to use your own Open AI key, you'll need that.
    4647
    4748### How does this plugin work?
    48 The plugin uses OpenAI’s `text-embedding-ada-002` model to generate embeddings for your posts. When users search, it matches the query embedding with post embeddings in the database for more relevant results.
     49The plugin uses OpenAI’s `text-embedding-3-small` model to generate embeddings for your content. When users search, it matches the query embedding with post embeddings in the database for more relevant results.
    4950
    5051### What happens if the OpenAI API fails?
     
    5354== External Service ==
    5455
    55 This plugin connects with Completions API from Open AI to get the embedding of the products. It also connects to the Embeddings endpoint.
     56This plugin connects with:
     57
     58- OpenAI Embeddings API: https://platform.openai.com/docs/guides/embeddings
     59- Custom Node.js Embedding Service
    5660
    5761Please read more here:
    58 https://platform.openai.com/docs/guides/completions
    5962https://platform.openai.com/docs/guides/embeddings
    6063
    6164== Changelog ==
     65
     66
     67= 1.6 =
     68- Replaced internal embedding storage with external service integration.
     69- Token-based authentication per origin.
    6270
    6371= 1.5 =
Note: See TracChangeset for help on using the changeset viewer.