Skip to content

Forum in maintenance, we will back soon 🙂

I am getting Oops S...
 
Notifications
Clear all

I am getting Oops Something went wrong error when i am trying to chat with the bot. can you help me.

9 Posts
3 Users
1 Reactions
78 Views
(@emarketniga)
Posts: 1
New Member
Topic starter
 

 

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

Screenshot (642)

 

Screenshot (642)
 
Posted : 11/03/2024 3:26 pm
(@husein)
Posts: 550
Member Moderator
 

@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.

 
Posted : 11/04/2024 5:45 pm
(@moh6)
Posts: 8
Active Member
 

@husein can i do gemini api instaed of openai how i do it

 

 
Posted : 11/07/2024 1:30 pm
(@husein)
Posts: 550
Member Moderator
 

@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.

 
Posted : 11/09/2024 10:01 am
(@moh6)
Posts: 8
Active Member
 

@husein can you give me the code I traid some but didn't work

 
Posted : 11/09/2024 11:51 am
(@husein)
Posts: 550
Member Moderator
 

@moh6 Can you attach the code you tried so that I can see what's the problem?

 
Posted : 11/12/2024 7:26 am
(@moh6)
Posts: 8
Active Member
 

@husein 

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',
));
});

 
Posted : 11/13/2024 9:46 am
(@husein)
Posts: 550
Member Moderator
 

@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.

 
Posted : 11/14/2024 5:33 pm
(@moh6)
Posts: 8
Active Member
 

@husein i sorry  i didn't mean that but thanks for your help

 
Posted : 11/16/2024 8:04 pm
Share:
build ai agents ad