Forum in maintenance, we will back soon 🙂
RAG on WordPress
Do you have any idea how to connect Pinecone API to WordPress?
I have this code to test:
// Function to get embeddings from OpenAI for a given text input
function get_openai_embedding($text) {
$OPENAI_API_KEY = "sk-ylA0VK_etc"; // Your OpenAI API key
$endpoint = 'https://api.openai.com/v1/embeddings';
// Make a POST request to the OpenAI API to get the embedding using the 'text-embedding-3-small' model
$response = wp_remote_post($endpoint, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $OPENAI_API_KEY
],
'body' => json_encode([
'input' => $text,
'model' => 'text-embedding-3-small' // Adjust the model based on your requirements
]),
'timeout' => 60
]);
// Handle any errors in the response
if (is_wp_error($response)) {
return $response;
}
// Parse the body of the response and decode the JSON data
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Check if the data contains a valid embedding and return it
if (isset($data['data'][0]['embedding'])) {
return $data['data'][0]['embedding'];
} else {
return new WP_Error('openai_error', 'Invalid response structure from OpenAI API');
}
}
// Function to query Pinecone for relevant vectors
function query_pinecone($query) {
$PINECONE_API_KEY = "f572-etc"; // Your Pinecone API key
$PINECONE_HOST = "https://nameindex-hx1dgf3.svc.aped-4627-b74a.pinecone.io/query"; // your Pinecone Host URL
// Get the embedding for the query from OpenAI
$embedding = get_openai_embedding($query);
if (is_wp_error($embedding)) {
return new WP_Error('pinecone_error', 'Error getting embedding: ' . $embedding->get_error_message());
}
// Make a POST request to the Pinecone API
$response = wp_remote_post($PINECONE_HOST, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $PINECONE_API_KEY
],
'body' => json_encode([
'vector' => $embedding,
'topK' => 5, // Retrieve the top 5 closest vectors
'includeValues' => true, // Include vector values in the response
'includeMetadata' => true // Include metadata in the response
]),
'timeout' => 60 // Set the timeout for the request
]);
// Check if the request returned an error
if (is_wp_error($response)) {
return new WP_Error('pinecone_error', 'Error in Pinecone request: ' . $response->get_error_message() . '<br><pre>' . print_r($response, true) . '</pre>');
}
// Retrieve the full response for detailed debugging
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Log and return the full response body for debugging
error_log(print_r($body, true));
if (isset($data['matches'])) {
return $data;
} else {
// If no matches or invalid response, return the entire response for debugging
return new WP_Error('pinecone_error', 'Invalid response from Pinecone: ' . print_r($body, true));
}
}
// Shortcode function to display Pinecone results and all errors
function pinecone_shortcode() {
$query = 'Ce servicii ofera Perla Senior?';
// Call the function to query Pinecone
$response = query_pinecone($query);
// Handle errors or display the result
if (is_wp_error($response)) {
return 'Error: ' . $response->get_error_message() . '<br><strong>Debug Info:</strong><pre>' . print_r($response, true) . '</pre>';
} else {
// Output the Pinecone result (formatted for better readability)
return '<strong>Pinecone Response:</strong><pre>' . esc_html(print_r($response, true)) . '</pre>';
}
}
// Register the shortcode
add_shortcode('pinecone_response', 'pinecone_shortcode');
And I got all the time this error displayed on the WordPress page:
Error: Invalid response from Pinecone: Unauthorized
Debug Info:
WP_Error Object ( [errors] => Array ( [pinecone_error] => Array ( [0] => Invalid response from Pinecone: Unauthorized ) ) [error_data] => Array ( ) [additional_data:protected] => Array ( ) )
@alfredo-gimbuta Ive never used pinecone, however checking the error you got "unauthorized" indicates that the Pinecone API is rejecting your request due to authentication issues.
So, check if your API is in the correct format / its valid / you do have permission to use it.
@husein Thank you. My code is working when I run in google colab having the same Pinecone credentials.
When comes to RAG on WordPress what vectore store did you use? Thank you
@alfredo-gimbuta do you need to give the server credentials to Pinecone API usage?
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@alfredo-gimbuta how you are providing the Pineconde credentials in your python script?