| 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 |
// 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 |
} |
| 91 |
|
| 92 |
// Prepare request body |
| 93 |
$request_body = [ |
| 94 |
'input' => $text, |
| 95 |
'model' => $selected_model |
| 96 |
]; |
| 97 |
|
| 98 |
// Add output_dimension for voyage-3-large |
| 99 |
if ($selected_model === 'voyage-3-large') { |
| 100 |
$request_body['output_dimension'] = 2048; |
| 101 |
} |
| 102 |
|
| 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 |
]; |
| 115 |
|
| 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 |
} |
| 122 |
|
| 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 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
} |
| 135 |
|