| @@ -67,49 +67,68 @@ | ||
| 67 | 67 | // error_log('Embedding generation failed for content from ' . $source_url); |
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 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'; | |
| 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 | + } | |
| 80 | 91 | |
| 81 | - $body = wp_json_encode([ | |
| 82 | - 'input' => $text, | |
| 83 | - 'model' => 'text-embedding-ada-002' | |
| 84 | - ]); | |
| 92 | + // Prepare request body | |
| 93 | + $request_body = [ | |
| 94 | + 'input' => $text, | |
| 95 | + 'model' => $selected_model | |
| 96 | + ]; | |
| 85 | 97 | |
| 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 | + // Add output_dimension for voyage-3-large | |
| 99 | + if ($selected_model === 'voyage-3-large') { | |
| 100 | + $request_body['output_dimension'] = 2048; | |
| 101 | + } | |
| 98 | 102 | |
| 99 | - $response = wp_remote_post($endpoint, $args); | |
| 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 | + ]; | |
| 100 | 115 | |
| 101 | - if (is_wp_error($response)) { | |
| 102 | - // error_log('Error generating embedding: ' . $response->get_error_message()); | |
| 103 | - return null; | |
| 104 | - } | |
| 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 | + } | |
| 105 | 122 | |
| 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 | - } | |
| 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 | + | |
| 115 | 134 | } |