PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.0.2
MxChat – AI Chatbot & Content Generation for WordPress v2.0.2
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
mxchat-basic / includes / class-mxchat-utils.php

class-mxchat-utils.php in MxChat – AI Chatbot & Content Generation for WordPress 2.0.2, at includes/class-mxchat-utils.php

116 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) {
3 exit; // Exit if accessed directly
4 }
5
6 class MxChat_Utils {
7
8 /**
9 * Submit or update content (and its embedding) in the database.
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.
15 * @param string $source_url The source URL of the content.
16 * @param string $api_key The API key used for generating embeddings.
17 */
18 public static function submit_content_to_db($content, $source_url, $api_key) {
19 global $wpdb;
20 $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
21
22 // Sanitize the source URL
23 $source_url = esc_url_raw($source_url);
24
25 // Generate the embedding using the API key
26 $embedding_vector = self::generate_embedding($content, $api_key);
27
28 if (is_array($embedding_vector)) {
29 $embedding_vector_serialized = maybe_serialize($embedding_vector);
30
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
36 )
37 );
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 } else {
66 // If embedding generation failed, you could log or handle it here
67 // error_log('Embedding generation failed for content from ' . $source_url);
68 }
69 }
70
71 /**
72 * Generate an embedding for the given text using the specified API key.
73 *
74 * @param string $text The text to be embedded.
75 * @param string $api_key The API key used for generating embeddings.
76 * @return array|null The embedding vector or null on failure.
77 */
78 private static function generate_embedding($text, $api_key) {
79 $endpoint = 'https://api.openai.com/v1/embeddings';
80
81 $body = wp_json_encode([
82 'input' => $text,
83 'model' => 'text-embedding-ada-002'
84 ]);
85
86 $args = [
87 'body' => $body,
88 'headers' => [
89 'Content-Type' => 'application/json',
90 'Authorization' => 'Bearer ' . $api_key,
91 ],
92 'timeout' => 60,
93 'redirection' => 5,
94 'blocking' => true,
95 'httpversion' => '1.0',
96 'sslverify' => true,
97 ];
98
99 $response = wp_remote_post($endpoint, $args);
100
101 if (is_wp_error($response)) {
102 // error_log('Error generating embedding: ' . $response->get_error_message());
103 return null;
104 }
105
106 $response_body = json_decode(wp_remote_retrieve_body($response), true);
107
108 if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
109 return $response_body['data'][0]['embedding'];
110 } else {
111 // error_log('Invalid response received from embedding API.');
112 return null;
113 }
114 }
115 }
116