| @@ -5,16 +5,13 @@ | ||
| 5 | 5 | |
| 6 | 6 | class MxChat_Utils { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Submit or update content (and its embedding) in the database. | |
| 9 | + * Submit content and its embedding to the database. | |
| 10 | 10 | * |
| 11 | - * If the source_url already exists, update that row. | |
| 12 | - * Otherwise, insert a new row. | |
| 13 | - * | |
| 14 | - * @param string $content The content to be embedded. | |
| 11 | + * @param string $content The content to be embedded. | |
| 15 | 12 | * @param string $source_url The source URL of the content. |
| 16 | - * @param string $api_key The API key used for generating embeddings. | |
| 13 | + * @param string $api_key The API key used for generating embeddings. | |
| 17 | 14 | */ |
| 18 | 15 | public static function submit_content_to_db($content, $source_url, $api_key) { |
| 19 | 16 | global $wpdb; |
| 20 | 17 | $table_name = $wpdb->prefix . 'mxchat_system_prompt_content'; |
| @@ -25,56 +22,35 @@ | ||
| 25 | 22 | // Generate the embedding using the API key |
| 26 | 23 | $embedding_vector = self::generate_embedding($content, $api_key); |
| 27 | 24 | |
| 28 | 25 | if (is_array($embedding_vector)) { |
| 29 | - $embedding_vector_serialized = maybe_serialize($embedding_vector); | |
| 26 | + $embedding_vector_serialized = serialize($embedding_vector); | |
| 30 | 27 | |
| 31 | - // Check if a row already exists for this URL | |
| 32 | - $existing_id = $wpdb->get_var( | |
| 33 | - $wpdb->prepare( | |
| 34 | - "SELECT id FROM {$table_name} WHERE source_url = %s LIMIT 1", | |
| 35 | - $source_url | |
| 28 | + // Insert or update the record in the database | |
| 29 | + $wpdb->replace( | |
| 30 | + $table_name, | |
| 31 | + array( | |
| 32 | + 'article_content' => $content, | |
| 33 | + 'embedding_vector' => $embedding_vector_serialized, | |
| 34 | + 'source_url' => $source_url, | |
| 35 | + ), | |
| 36 | + array( | |
| 37 | + '%s', | |
| 38 | + '%s', | |
| 39 | + '%s', | |
| 36 | 40 | ) |
| 37 | 41 | ); |
| 38 | - | |
| 39 | - if ($existing_id) { | |
| 40 | - // Update the existing row | |
| 41 | - $wpdb->update( | |
| 42 | - $table_name, | |
| 43 | - array( | |
| 44 | - 'article_content' => $content, | |
| 45 | - 'embedding_vector' => $embedding_vector_serialized, | |
| 46 | - 'timestamp' => current_time('mysql'), // Remove if your table doesn't have a timestamp column | |
| 47 | - ), | |
| 48 | - array('id' => $existing_id), | |
| 49 | - array('%s','%s','%s'), | |
| 50 | - array('%d') | |
| 51 | - ); | |
| 52 | - } else { | |
| 53 | - // Insert a new row | |
| 54 | - $wpdb->insert( | |
| 55 | - $table_name, | |
| 56 | - array( | |
| 57 | - 'article_content' => $content, | |
| 58 | - 'embedding_vector' => $embedding_vector_serialized, | |
| 59 | - 'source_url' => $source_url, | |
| 60 | - 'timestamp' => current_time('mysql'), // Remove if your table doesn't have a timestamp column | |
| 61 | - ), | |
| 62 | - array('%s','%s','%s','%s') | |
| 63 | - ); | |
| 64 | - } | |
| 65 | 42 | } else { |
| 66 | - // If embedding generation failed, you could log or handle it here | |
| 67 | - // error_log('Embedding generation failed for content from ' . $source_url); | |
| 43 | + //error_log('Embedding generation failed for content from ' . $source_url); | |
| 68 | 44 | } |
| 69 | 45 | } |
| 70 | 46 | |
| 71 | 47 | /** |
| 72 | - * Generate an embedding for the given text using the specified API key. | |
| 48 | + * Generate an embedding for the given text. | |
| 73 | 49 | * |
| 74 | - * @param string $text The text to be embedded. | |
| 50 | + * @param string $text The text to be embedded. | |
| 75 | 51 | * @param string $api_key The API key used for generating embeddings. |
| 76 | - * @return array|null The embedding vector or null on failure. | |
| 52 | + * @return array|null The embedding vector or null on failure. | |
| 77 | 53 | */ |
| 78 | 54 | private static function generate_embedding($text, $api_key) { |
| 79 | 55 | $endpoint = 'https://api.openai.com/v1/embeddings'; |
| 80 | 56 | |
| @@ -83,24 +59,24 @@ | ||
| 83 | 59 | 'model' => 'text-embedding-ada-002' |
| 84 | 60 | ]); |
| 85 | 61 | |
| 86 | 62 | $args = [ |
| 87 | - 'body' => $body, | |
| 88 | - 'headers' => [ | |
| 89 | - 'Content-Type' => 'application/json', | |
| 63 | + 'body' => $body, | |
| 64 | + 'headers' => [ | |
| 65 | + 'Content-Type' => 'application/json', | |
| 90 | 66 | 'Authorization' => 'Bearer ' . $api_key, |
| 91 | 67 | ], |
| 92 | - 'timeout' => 60, | |
| 68 | + 'timeout' => 60, | |
| 93 | 69 | 'redirection' => 5, |
| 94 | - 'blocking' => true, | |
| 70 | + 'blocking' => true, | |
| 95 | 71 | 'httpversion' => '1.0', |
| 96 | - 'sslverify' => true, | |
| 72 | + 'sslverify' => true, | |
| 97 | 73 | ]; |
| 98 | 74 | |
| 99 | 75 | $response = wp_remote_post($endpoint, $args); |
| 100 | 76 | |
| 101 | 77 | if (is_wp_error($response)) { |
| 102 | - // error_log('Error generating embedding: ' . $response->get_error_message()); | |
| 78 | + //error_log('Error generating embedding: ' . $response->get_error_message()); | |
| 103 | 79 | return null; |
| 104 | 80 | } |
| 105 | 81 | |
| 106 | 82 | $response_body = json_decode(wp_remote_retrieve_body($response), true); |
| @@ -107,9 +83,11 @@ | ||
| 107 | 83 | |
| 108 | 84 | if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) { |
| 109 | 85 | return $response_body['data'][0]['embedding']; |
| 110 | 86 | } else { |
| 111 | - // error_log('Invalid response received from embedding API.'); | |
| 87 | + //error_log('Invalid response received from embedding API.'); | |
| 112 | 88 | return null; |
| 113 | 89 | } |
| 114 | 90 | } |
| 115 | 91 | } |
| 92 | + | |
| 93 | +?> | |