Forum in maintenance, we will back soon 🙂
I am getting Oops Something went wrong error when i am trying to chat with the bot. can you help me.
i watched and read "How To Create a Free AI Chatbot on WordPress From Scratch" i have tried to replicate it on my test website, but it is showing an error. Please let me know how I should resolve this. i have attached an error screenshot
@emarketniga Here I'll give you the read frontend and backend codes, just edit the backend code where you need to add your own working API key instead of YOUR_OWN_API_KEY i put it, and substitute the 2 links in the javascript with your own website URL, by copying your main website link from the search bar and paste it in the first 2 variables of the backend code where i wrote YOUR_WEBSITE_URL.
@moh6 Yes you can, you'll need to add your gemini API key instead of the openai one, and change the code that calls for openai's llm with a code that code that calls for gemini's llm.
@moh6 Can you attach the code you tried so that I can see what's the problem?
function generate_chat_response( $last_prompt, $conversation_history ) {
// Gemini API URL and key
$api_url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=api-key';
$api_key = 'API-key'; // Replace with your actual Gemini API key
// Set up headers for Gemini API authentication
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
];
// Add the last prompt to the conversation history
$conversation_history[] = [
'role' => 'system',
'content' => 'Answer questions only related to SEO, otherwise, say I don’t know'
];
$conversation_history[] = [
'role' => 'user',
'content' => $last_prompt
];
// Format the request body for Gemini's API requirements
$body = [
'contents' => [
[
'parts' => [
['text' => implode("\n", array_column($conversation_history, 'content'))]
]
]
]
];
// Arguments for the WordPress HTTP API
$args = [
'method' => 'POST',
'headers' => $headers,
'body' => json_encode($body),
'timeout' => 120
];
// Send the request
$response = wp_remote_request($api_url, $args);
// Handle the response
if (is_wp_error($response)) {
return $response->get_error_message();
} else {
$response_body = wp_remote_retrieve_body($response);
$data = json_decode($response_body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return [
'success' => false,
'message' => 'Invalid JSON in API response',
'result' => ''
];
} elseif (!isset($data['choices'])) { // Adjust according to Gemini's response structure
return [
'success' => false,
'message' => 'API request failed. Response: ' . $response_body,
'result' => ''
];
} else {
$content = $data['choices'][0]['message']['content']; // Modify this if Gemini returns content differently
return [
'success' => true,
'message' => 'Response Generated',
'result' => $content
];
}
}
}
function generate_dummy_response( $last_prompt, $conversation_history ) {
// Dummy static response
$dummy_response = array(
'success' => true,
'message' => 'done',
'result' => "here is my reply"
);
// Return the dummy response as an associative array
return $dummy_response;
}
function handle_chat_bot_request( WP_REST_Request $request ) {
$last_prompt = $request->get_param('last_prompt');
$conversation_history = $request->get_param('conversation_history');
$response = generate_chat_response($last_prompt, $conversation_history);
return $response;
}
function load_chat_bot_base_configuration(WP_REST_Request $request) {
// Retrieve user data or other dynamic information here
$user_avatar_url = "https://learnwithhasan.com/wp-content/uploads/2023/09/pngtree-businessman-user-avatar-wearing-suit-with-red-tie-png-image_5809521.png";
$bot_image_url = "https://learnwithhasan.com/wp-content/uploads/2023/12/site-logo-mobile.png";
$response = array(
'botStatus' => 1,
'StartUpMessage' => "Hi, How are you?",
'fontSize' => '16',
'userAvatarURL' => $user_avatar_url,
'botImageURL' => $bot_image_url,
'commonButtons' => array(
array(
'buttonText' => 'I want your help!!!',
'buttonPrompt' => 'I have a question about your courses'
),
array(
'buttonText' => 'I want a Discount',
'buttonPrompt' => 'I want a discount'
)
)
);
$response = new WP_REST_Response($response, 200);
return $response;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'myapi/v1', '/chat-bot/', array(
'methods' => 'POST',
'callback' => 'handle_chat_bot_request',
'permission_callback' => '__return_true'
));
register_rest_route('myapi/v1', '/chat-bot-config', array(
'methods' => 'GET',
'callback' => 'load_chat_bot_base_configuration',
));
});
@moh6 Please dont attach your code like this, when you wonna attach codes use the "Attach files" button in the editor.
Anwyways, I edited the code just enter your API key and it should work.
@husein i sorry i didn't mean that but thanks for your help