Home / Admin / Chatbot backend
Duplicate Snippet

Embed Snippet on Your Site

Chatbot backend

function generate_chat_response( $last_prompt, $conversation_history ) {

// OpenAI API URL and key
$api_url = 'https://modelsearch1.openai.azure.com/openai/deployments/gpt-35-turbo-StoreWayfinder/chat/completions';
$api_key = 'ac860bf2631e4405b85dc9442be2402c'; // Replace with your actual API key

// Headers for the OpenAI API
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
];

// Add the last prompt to the conversation history
$conversation_history[] = [
'role' => 'system',
'content' => 'You are a sales focused assistant. You work for CSA Group and can only recommend products listed in the knowledge base you are trained on. Do not make things up. Always be factual. Provide links to all products. Reference Citations should always be provided. Only provide answers from ai search wayfinder-index. Be concise and mention products in a list format.'
];

$conversation_history[] = [
'role' => 'user',
'content' => $last_prompt
];

// Body for the OpenAI API
$body = [
'model' => 'gpt-3.5-turbo', // You can change the model if needed
'messages' => $conversation_history,
'temperature' => 0.7 // You can adjust this value based on desired creativity
];

// Args 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'])) {
return [
'success' => false,
'message' => 'API request failed. Response: ' . $response_body,
'result' => ''
];
} else {
$content = $data['choices'][0]['message']['content'];
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) {
// You can 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"; // Implement this function
$bot_image_url = "https://learnwithhasan.com/wp-content/uploads/2023/12/site-logo-mobile.png"; // Implement this function

$response = array(
'botStatus' => 1,
'StartUpMessage' => "Hi, How are you?",
'fontSize' => '16',
'userAvatarURL' => $user_avatar_url,
'botImageURL' => $bot_image_url,
// Adding the new field
'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',
));
} );

Code Preview
php
<?php

Comments

Add a Comment