| @@ -5,51 +5,8 @@ | ||
| 5 | 5 | |
| 6 | 6 | class MxChat_Utils { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Centralized embedding model registry. Single source of truth for dimensions | |
| 10 | - * and provider, so model-switch protection logic doesn't drift across files. | |
| 11 | - */ | |
| 12 | -public static function embedding_model_registry() { | |
| 13 | - return array( | |
| 14 | - 'text-embedding-ada-002' => array('dims' => 1536, 'provider' => 'openai', 'label' => 'Ada 2'), | |
| 15 | - 'text-embedding-3-small' => array('dims' => 1536, 'provider' => 'openai', 'label' => 'TE3 Small'), | |
| 16 | - 'text-embedding-3-large' => array('dims' => 3072, 'provider' => 'openai', 'label' => 'TE3 Large'), | |
| 17 | - 'voyage-3-large' => array('dims' => 2048, 'provider' => 'voyage', 'label' => 'Voyage-3 Large'), | |
| 18 | - 'gemini-embedding-001' => array('dims' => 1536, 'provider' => 'gemini', 'label' => 'Gemini Embedding'), | |
| 19 | - ); | |
| 20 | -} | |
| 21 | - | |
| 22 | -public static function embedding_model_dimensions($model) { | |
| 23 | - $registry = self::embedding_model_registry(); | |
| 24 | - return isset($registry[$model]) ? (int) $registry[$model]['dims'] : 0; | |
| 25 | -} | |
| 26 | - | |
| 27 | -public static function embedding_model_label($model) { | |
| 28 | - $registry = self::embedding_model_registry(); | |
| 29 | - return isset($registry[$model]) ? $registry[$model]['label'] : $model; | |
| 30 | -} | |
| 31 | - | |
| 32 | -/** | |
| 33 | - * Returns the model that was last used to actually write embeddings into the | |
| 34 | - * KB. Differs from the user-selected setting once a switch has happened but | |
| 35 | - * no re-embed has occurred yet — that's the mismatch state we warn about. | |
| 36 | - */ | |
| 37 | -public static function get_active_embedding_model() { | |
| 38 | - return get_option('mxchat_active_embedding_model', ''); | |
| 39 | -} | |
| 40 | - | |
| 41 | -/** | |
| 42 | - * Stamp the model that produced the most recent successful embedding. Called | |
| 43 | - * from generate_embedding() right after the API responds with a valid vector. | |
| 44 | - */ | |
| 45 | -public static function stamp_active_embedding_model($model) { | |
| 46 | - if (!empty($model) && $model !== self::get_active_embedding_model()) { | |
| 47 | - update_option('mxchat_active_embedding_model', $model, false); | |
| 48 | - } | |
| 49 | -} | |
| 50 | - | |
| 51 | -/** | |
| 52 | 9 | * UPDATED: Submit or update content (and its embedding) in the database. |
| 53 | 10 | * Stores in Pinecone if enabled, otherwise stores in WordPress DB. |
| 54 | 11 | * |
| 55 | 12 | * @param string $content The content to be embedded. |
| @@ -255,10 +212,8 @@ | ||
| 255 | 212 | $max_attempts = 3; |
| 256 | 213 | $current_content = $safe_content; |
| 257 | 214 | $result = false; |
| 258 | 215 | |
| 259 | - $active_model = self::get_active_embedding_model(); | |
| 260 | - | |
| 261 | 216 | while ($attempt <= $max_attempts && $result === false) { |
| 262 | 217 | try { |
| 263 | 218 | if ($existing_id) { |
| 264 | 219 | //error_log('[MXCHAT-DB] Found existing entry (ID: ' . $existing_id . '). Updating... (Attempt ' . $attempt . ')'); |
| @@ -271,13 +226,12 @@ | ||
| 271 | 226 | 'article_content' => $current_content, |
| 272 | 227 | 'embedding_vector' => $embedding_vector_serialized, |
| 273 | 228 | 'source_url' => $source_url, |
| 274 | 229 | 'content_type' => $content_type, |
| 275 | - 'embedding_model' => $active_model, | |
| 276 | 230 | 'timestamp' => current_time('mysql'), |
| 277 | 231 | ), |
| 278 | 232 | array('id' => $existing_id), |
| 279 | - array('%s','%s','%s','%s','%s','%s','%s'), | |
| 233 | + array('%s','%s','%s','%s','%s','%s'), | |
| 280 | 234 | array('%d') |
| 281 | 235 | ); |
| 282 | 236 | } else { |
| 283 | 237 | //error_log('[MXCHAT-DB] No existing entry found. Inserting new row... (Attempt ' . $attempt . ')'); |
| @@ -291,12 +245,11 @@ | ||
| 291 | 245 | 'article_content' => $current_content, |
| 292 | 246 | 'embedding_vector' => $embedding_vector_serialized, |
| 293 | 247 | 'source_url' => $source_url, // Now unique for manual content |
| 294 | 248 | 'content_type' => $content_type, |
| 295 | - 'embedding_model' => $active_model, | |
| 296 | 249 | 'timestamp' => current_time('mysql'), |
| 297 | 250 | ), |
| 298 | - array('%s','%s','%s','%s','%s','%s','%s') | |
| 251 | + array('%s','%s','%s','%s','%s','%s') | |
| 299 | 252 | ); |
| 300 | 253 | } |
| 301 | 254 | |
| 302 | 255 | if ($result === false) { |
| @@ -418,10 +371,9 @@ | ||
| 418 | 371 | 'source_url' => $url, // Can be empty for manual content |
| 419 | 372 | 'type' => $content_type, // Now supports: post, page, pdf, url, manual, product, etc. |
| 420 | 373 | 'last_updated' => time(), |
| 421 | 374 | 'created_at' => time(), // Add creation timestamp |
| 422 | - 'bot_id' => $bot_id, // Add bot identification | |
| 423 | - 'embedding_model' => self::get_active_embedding_model() // 3.2.3: track which model produced this vector | |
| 375 | + 'bot_id' => $bot_id // Add bot identification | |
| 424 | 376 | ); |
| 425 | 377 | |
| 426 | 378 | $vector_data = array( |
| 427 | 379 | 'id' => $vector_id, |
| @@ -569,9 +521,8 @@ | ||
| 569 | 521 | // Handle different response formats based on provider |
| 570 | 522 | if (strpos($selected_model, 'gemini-embedding') === 0) { |
| 571 | 523 | // Gemini API response format |
| 572 | 524 | if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) { |
| 573 | - self::stamp_active_embedding_model($selected_model); | |
| 574 | 525 | return $response_body['embedding']['values']; |
| 575 | 526 | } else { |
| 576 | 527 | //error_log('Invalid response received from Gemini embedding API for bot ' . $bot_id . ': ' . wp_json_encode($response_body)); |
| 577 | 528 | return null; |
| @@ -578,9 +529,8 @@ | ||
| 578 | 529 | } |
| 579 | 530 | } else { |
| 580 | 531 | // OpenAI/Voyage API response format |
| 581 | 532 | if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) { |
| 582 | - self::stamp_active_embedding_model($selected_model); | |
| 583 | 533 | return $response_body['data'][0]['embedding']; |
| 584 | 534 | } else { |
| 585 | 535 | //error_log('Invalid response received from embedding API for bot ' . $bot_id . ': ' . wp_json_encode($response_body)); |
| 586 | 536 | return null; |
| @@ -736,10 +686,9 @@ | ||
| 736 | 686 | 'total_chunks' => $chunk_metadata['total_chunks'], |
| 737 | 687 | 'parent_url_hash' => $chunk_metadata['parent_url_hash'], |
| 738 | 688 | 'last_updated' => time(), |
| 739 | 689 | 'created_at' => time(), |
| 740 | - 'bot_id' => $bot_id, | |
| 741 | - 'embedding_model' => self::get_active_embedding_model() | |
| 690 | + 'bot_id' => $bot_id | |
| 742 | 691 | ); |
| 743 | 692 | |
| 744 | 693 | $vector_data = array( |
| 745 | 694 | 'id' => $vector_id, |
| @@ -792,12 +741,11 @@ | ||
| 792 | 741 | 'article_content' => $content_with_metadata, |
| 793 | 742 | 'embedding_vector' => $embedding_vector_serialized, |
| 794 | 743 | 'source_url' => $source_url, |
| 795 | 744 | 'content_type' => $content_type, |
| 796 | - 'embedding_model' => self::get_active_embedding_model(), | |
| 797 | 745 | 'timestamp' => current_time('mysql') |
| 798 | 746 | ), |
| 799 | - array('%s', '%s', '%s', '%s', '%s', '%s', '%s') | |
| 747 | + array('%s', '%s', '%s', '%s', '%s', '%s') | |
| 800 | 748 | ); |
| 801 | 749 | |
| 802 | 750 | if ($result === false) { |
| 803 | 751 | return new WP_Error('database_failed', 'Failed to insert chunk: ' . $wpdb->last_error); |