PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.0.8
MxChat – AI Chatbot & Content Generation for WordPress v1.0.8
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
← All changes | includes/class-mxchat-utils.php +58 -99 2.0.61.0.8 View file →
@@ -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,110 +22,72 @@
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 -/**
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 - // Get options and selected model
80 - $options = get_option('mxchat_options');
81 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
82 -
83 - // Determine endpoint and API key based on model
84 - if (strpos($selected_model, 'voyage') === 0) {
85 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
86 - $api_key = $options['voyage_api_key'] ?? '';
87 - } else {
88 - $endpoint = 'https://api.openai.com/v1/embeddings';
89 - // Use the passed API key for OpenAI
90 - }
47 + /**
48 + * Generate an embedding for the given text.
49 + *
50 + * @param string $text The text to be embedded.
51 + * @param string $api_key The API key used for generating embeddings.
52 + * @return array|null The embedding vector or null on failure.
53 + */
54 + private static function generate_embedding($text, $api_key) {
55 + $endpoint = 'https://api.openai.com/v1/embeddings';
91 56
92 - // Prepare request body
93 - $request_body = [
94 - 'input' => $text,
95 - 'model' => $selected_model
96 - ];
57 + $body = wp_json_encode([
58 + 'input' => $text,
59 + 'model' => 'text-embedding-ada-002'
60 + ]);
97 61
98 - // Add output_dimension for voyage-3-large
99 - if ($selected_model === 'voyage-3-large') {
100 - $request_body['output_dimension'] = 2048;
101 - }
62 + $args = [
63 + 'body' => $body,
64 + 'headers' => [
65 + 'Content-Type' => 'application/json',
66 + 'Authorization' => 'Bearer ' . $api_key,
67 + ],
68 + 'timeout' => 60,
69 + 'redirection' => 5,
70 + 'blocking' => true,
71 + 'httpversion' => '1.0',
72 + 'sslverify' => true,
73 + ];
102 74
103 - $args = [
104 - 'body' => wp_json_encode($request_body),
105 - 'headers' => [
106 - 'Content-Type' => 'application/json',
107 - 'Authorization' => 'Bearer ' . $api_key,
108 - ],
109 - 'timeout' => 60,
110 - 'redirection' => 5,
111 - 'blocking' => true,
112 - 'httpversion' => '1.0',
113 - 'sslverify' => true,
114 - ];
75 + $response = wp_remote_post($endpoint, $args);
115 76
116 - $response = wp_remote_post($endpoint, $args);
117 -
118 - if (is_wp_error($response)) {
119 - error_log('Error generating embedding: ' . $response->get_error_message());
120 - return null;
121 - }
77 + if (is_wp_error($response)) {
78 + //error_log('Error generating embedding: ' . $response->get_error_message());
79 + return null;
80 + }
122 81
123 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
124 -
125 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
126 - return $response_body['data'][0]['embedding'];
127 - } else {
128 - error_log('Invalid response received from embedding API: ' . wp_json_encode($response_body));
129 - return null;
130 - }
82 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
83 +
84 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
85 + return $response_body['data'][0]['embedding'];
86 + } else {
87 + //error_log('Invalid response received from embedding API.');
88 + return null;
89 + }
90 + }
131 91 }
132 -
133 -
134 -}
92 +
93 +?>