| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
class MxChat_Utils { |
| 7 |
|
| 8 |
/** |
| 9 |
* Submit content and its embedding to the database. |
| 10 |
* |
| 11 |
* @param string $content The content to be embedded. |
| 12 |
* @param string $source_url The source URL of the content. |
| 13 |
* @param string $api_key The API key used for generating embeddings. |
| 14 |
*/ |
| 15 |
public static function submit_content_to_db($content, $source_url, $api_key) { |
| 16 |
global $wpdb; |
| 17 |
$table_name = $wpdb->prefix . 'mxchat_system_prompt_content'; |
| 18 |
|
| 19 |
// Sanitize the source URL |
| 20 |
$source_url = esc_url_raw($source_url); |
| 21 |
|
| 22 |
// Generate the embedding using the API key |
| 23 |
$embedding_vector = self::generate_embedding($content, $api_key); |
| 24 |
|
| 25 |
if (is_array($embedding_vector)) { |
| 26 |
$embedding_vector_serialized = serialize($embedding_vector); |
| 27 |
|
| 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', |
| 40 |
) |
| 41 |
); |
| 42 |
} else { |
| 43 |
//error_log('Embedding generation failed for content from ' . $source_url); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 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'; |
| 56 |
|
| 57 |
$body = wp_json_encode([ |
| 58 |
'input' => $text, |
| 59 |
'model' => 'text-embedding-ada-002' |
| 60 |
]); |
| 61 |
|
| 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 |
]; |
| 74 |
|
| 75 |
$response = wp_remote_post($endpoint, $args); |
| 76 |
|
| 77 |
if (is_wp_error($response)) { |
| 78 |
//error_log('Error generating embedding: ' . $response->get_error_message()); |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 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 |
} |
| 91 |
} |
| 92 |
|
| 93 |
?> |
| 94 |
|