#An error occurred: undefined
10 messages · Page 1 of 1 (latest)
function metaphor_and_cohere_generate_text() {
// Get the topic from the AJAX request
$prompt = $_POST['prompt'] ?? ''; // Check if the 'prompt' key exists in the $_POST array
// Metaphor API URL and keys
$metaphor_api_url = 'https://<customer>.metaphor.systems/api/system/search[ENDPOINT]'; // Replace <customer> with your actual customer name
$metaphor_api_keys = ['YOUR-METAPHOR-API-KEY-1', 'YOUR-METAPHOR-API-KEY-2', 'YOUR-METAPHOR-API-KEY-3']; // Replace with your actual Metaphor API keys
// Headers for the Metaphor API
$metaphor_headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $metaphor_api_keys[0] // Use the first API key
];
// Body for the Metaphor API
$metaphor_body = [
'query' => $prompt,
'limit' => 3
];
// Args for the WordPress HTTP API
$metaphor_args = [
'method' => 'POST',
'headers' => $metaphor_headers,
'body' => json_encode($metaphor_body),
'timeout' => 120
];
// Send the request to the Metaphor API
$metaphor_response = wp_remote_request($metaphor_api_url, $metaphor_args);
// Handle the response from the Metaphor API
if (is_wp_error($metaphor_response)) {
$error_message = $metaphor_response->get_error_message();
wp_send_json_error("Something went wrong: $error_message");
} else {
$metaphor_body = wp_remote_retrieve_body($metaphor_response);
$metaphor_data = json_decode($metaphor_body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
wp_send_json_error('Invalid JSON in API response');
} elseif (!isset($metaphor_data['data']['search']['results'])) {
wp_send_json_error('API request failed. Response: ' . $metaphor_body);
} else {
$metaphor_result = '';
foreach ($metaphor_data['data']['search']['results'] as $result) {
$metaphor_result .= $result['snippet'] . ' ';
}
$metaphor_result = substr($metaphor_result, 0, 4000); // Chop off the input if it gets longer than 4000 tokens
// Cohere API URL and key
$cohere_api_url = 'https://api.cohere.ai/v1/text/generate';
$cohere_api_keys = ['YOUR-COHERE-API-KEY-1', 'YOUR-COHERE-API-KEY-2', 'YOUR-COHERE-API-KEY-3']; // Replace with your actual Cohere API keys
// Headers for the Cohere API
$cohere_headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $cohere_api_keys[0] // Use the first API key
];
// Body for the Cohere API
$cohere_body = [
'model' => 'base',
'prompt' => 'summarize the topic ' . $metaphor_result, // Use a fixed prompt and append the Metaphor result
'max_tokens' => 4000
];
// Args for the WordPress HTTP API
$cohere_args = [
'method' => 'POST',
'headers' => $cohere_headers,
'body' => json_encode($cohere_body),
'timeout' => 120
];
// Send the request to the Cohere API
$cohere_response = wp_remote_request($cohere_api_url, $cohere_args);
// Handle the response from the Cohere API
if (is_wp_error($cohere_response)) {
$error_message = $cohere_response->get_error_message();
wp_send_json_error("Something went wrong: $error_message");
} else {
$cohere_body = wp_remote_retrieve_body($cohere_response);
$cohere_data = json_decode($cohere_body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
wp_send_json_error('Invalid JSON in API response');
} elseif (!isset($cohere_data['choices'])) {
wp_send_json_error('API request failed. Response: ' . $cohere_body);
} else {
wp_send_json_success($cohere_data);
}
}
}
}
// Always die in functions echoing AJAX content
wp_die();
}
add_action('wp_ajax_metaphor_and_cohere_generate_text', 'metaphor_and_cohere_generate_text');
add_action('wp_ajax_nopriv_metaphor_and_cohere_generate_text', 'metaphor_and_cohere_generate_text');
btw ik the api urls are wrong
This looks like ChatGPT trash code
Lmao but Cgpt is handy for newbies like us
So why doesn't it work? 💀
Moda-phucker I said its handy
Didn't said that chatgpt gives u accurate results