This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot-chatgpt.php
331 lines (270 loc) · 13.5 KB
/
chatbot-chatgpt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/*
* Plugin Name: Chatbot ChatGPT
* Plugin URI: https://github.com/fernandodilland/HackMTY2023
* Description: A simple plugin to add a Chatbot ChatGPT to your Wordpress Website.
*
*/
// If this file is called directly, die.
defined( 'WPINC' ) || die;
// If this file is called directly, die.
if ( ! defined( 'ABSPATH' ) ) {
die();
}
// Include necessary files
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings.php';
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-api-model.php'; // Refactoring Settings - Ver 1.5.0
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-avatar.php'; // Refactoring Settings - Ver 1.5.0
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-crawler.php'; // Crawler aka Knowlege Navigator - Ver 1.6.1
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-kn-analysis.php'; // Crawler aka Knowlege Navigator Analysis- Ver 1.6.2
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-links.php'; // Refactoring Settings - Ver 1.5.0
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-localize.php'; // Fixing localStorage - Ver 1.6.1
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-premium.php'; // Refactoring Settings - Ver 1.5.0
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-registration.php'; // Refactoring Settings - Ver 1.5.0
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-settings-setup.php'; // Refactoring Settings - Ver 1.5.0
require_once plugin_dir_path(__FILE__) . 'includes/chatbot-chatgpt-shortcode.php';
// Diagnotics on/off setting can be found on the Settings tab - Ver 1.5.0
// update_option('chatgpt_diagnostics', 'Off');
$chatgpt_diagnostics = esc_attr(get_option('chatgpt_diagnostics', 'Off'));
// Context History - Ver 1.6.1
$context_history = [];
// Enqueue plugin scripts and styles
function chatbot_chatgpt_enqueue_scripts() {
// Ensure the Dashicons font is properly enqueued - Ver 1.1.0
wp_enqueue_style( 'dashicons' );
wp_enqueue_style('chatbot-chatgpt-css', plugins_url('assets/css/chatbot-chatgpt.css', __FILE__));
wp_enqueue_script('chatbot-chatgpt-js', plugins_url('assets/js/chatbot-chatgpt.js', __FILE__), array('jquery'), '1.0', true);
// Enqueue the chatbot-chatgpt-local.js file - Ver 1.4.1
wp_enqueue_script('chatbot-chatgpt-local', plugins_url('assets/js/chatbot-chatgpt-local.js', __FILE__), array('jquery'), '1.0', true);
// Defaults for Ver 1.6.1
$defaults = array(
'chatgpt_bot_name' => 'Chatbot ChatGPT',
'chatgpt_initial_greeting' => 'Hello! How can I help you today?',
'chatgpt_subsequent_greeting' => 'Hello again! How can I help you?',
'chatgptStartStatus' => 'closed',
'chatgptStartStatusNewVisitor' => 'closed',
'chatgpt_disclaimer_setting' => 'No',
'chatgpt_max_tokens_setting' => '150',
'chatgpt_width_setting' => 'Narrow',
'chatgpt_diagnostics' => 'Off',
'chatgpt_avatar_icon_setting' => 'icon-001.png',
'chatgpt_avatar_icon_url_setting' => '',
'chatgpt_custom_avatar_icon_setting' => 'icon-001.png',
'chatgpt_avatar_greeting_setting' => 'Howdy!!! Great to see you today! How can I help you?',
'chatgpt_model_choice' => 'gpt-3.5-turbo',
'chatgpt_max_tokens_setting' => 150,
'chatbot_chatgpt_conversation_context' => 'You are a versatile, friendly, and helpful assistant designed to support me in a variety of tasks.',
);
// Revised for Ver 1.5.0
$option_keys = array(
'chatgpt_bot_name',
'chatgpt_initial_greeting',
'chatgpt_subsequent_greeting',
'chatgptStartStatus',
'chatgptStartStatusNewVisitor',
'chatgpt_disclaimer_setting',
'chatgpt_max_tokens_setting',
'chatgpt_width_setting',
'chatgpt_diagnostics',
// Avatar Options - Ver 1.5.0
'chatgpt_avatar_icon_setting',
'chatgpt_avatar_icon_url_setting',
'chatgpt_custom_avatar_icon_setting',
'chatgpt_avatar_greeting_setting',
);
$chatbot_settings = array();
foreach ($option_keys as $key) {
$default_value = isset($defaults[$key]) ? $defaults[$key] : '';
$chatbot_settings[$key] = esc_attr(get_option($key, $default_value));
}
$chatbot_settings['iconBaseURL'] = plugins_url( 'assets/icons/', __FILE__ );
wp_localize_script('chatbot-chatgpt-js', 'plugin_vars', array(
'pluginUrl' => plugins_url('', __FILE__ ),
));
wp_localize_script('chatbot-chatgpt-local', 'chatbotSettings', $chatbot_settings);
wp_localize_script('chatbot-chatgpt-js', 'chatbot_chatgpt_params', array(
'ajax_url' => admin_url('admin-ajax.php'),
// 'api_key' => esc_attr(get_option('chatgpt_api_key')),
));
// Update localStorage - Ver 1.6.1
echo "<script type=\"text/javascript\">
document.addEventListener('DOMContentLoaded', (event) => {
let chatbotSettings = " . json_encode($chatbot_settings) . ";
Object.keys(chatbotSettings).forEach((key) => {
if(!localStorage.getItem(key)) {
// console.log('Setting ' + key + ' in localStorage');
localStorage.setItem(key, chatbotSettings[key]);
} else {
// console.log(key + ' is already set in localStorage');
}
});
});
</script>";
}
add_action('wp_enqueue_scripts', 'chatbot_chatgpt_enqueue_scripts');
// Settings and Deactivation Links - Ver - 1.5.0
function enqueue_jquery_ui() {
wp_enqueue_style('wp-jquery-ui-dialog');
wp_enqueue_script('jquery-ui-dialog');
}
add_action( 'admin_enqueue_scripts', 'enqueue_jquery_ui' );
// Handle Ajax requests
function chatbot_chatgpt_send_message() {
// Retrieve the API key
$api_key = esc_attr(get_option('chatgpt_api_key'));
// Retrieve the model from the settings or default to gpt-3.5-turbo
$model = esc_attr(get_option('chatgpt_model_choice', 'gpt-3.5-turbo'));
// Retrieve the Max tokens - Ver 1.4.2
$max_tokens = esc_attr(get_option('chatgpt_max_tokens_setting', 150));
// Send only clean text via the API
$message = sanitize_text_field($_POST['message']);
// Check API key and message
if (!$api_key || !$message) {
wp_send_json_error('Invalid API key or message');
}
// Send message to ChatGPT API
$response = chatbot_chatgpt_call_api($api_key, $message);
// Return response
wp_send_json_success($response);
}
add_action('wp_ajax_chatbot_chatgpt_send_message', 'chatbot_chatgpt_send_message');
add_action('wp_ajax_nopriv_chatbot_chatgpt_send_message', 'chatbot_chatgpt_send_message');
// Settings and Deactivation - Ver 1.5.0
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'chatbot_chatgpt_plugin_action_links');
add_action('wp_ajax_chatbot_chatgpt_deactivation_feedback', 'chatbot_chatgpt_deactivation_feedback');
add_action('admin_footer', 'chatbot_chatgpt_admin_footer');
// Crawler aka Knowledge Navigator - Ver 1.6.1
function chatbot_chatgpt_kn_status_activation() {
add_option('chatbot_chatgpt_kn_status', 'Never Run');
}
register_activation_hook(__FILE__, 'chatbot_chatgpt_kn_status_activation');
// TODO Clean Up in Aisle 4
function chatbot_chatgpt_kn_status_deactivation() {
delete_option('chatbot_chatgpt_kn_status');
wp_clear_scheduled_hook('crawl_scheduled_event_hook');
}
register_deactivation_hook(__FILE__, 'chatbot_chatgpt_kn_status_deactivation');
// Function to add a new message and response, keeping only the last five - Ver 1.6.1
function addEntry($transient_name, $newEntry) {
$context_history = get_transient($transient_name);
if (!$context_history) {
$context_history = [];
}
// Determine the total length of all existing entries
$totalLength = 0;
foreach ($context_history as $entry) {
if (is_string($entry)) {
$totalLength += strlen($entry);
} elseif (is_array($entry)) {
$totalLength += strlen(json_encode($entry)); // Convert to string if an array
}
}
// Define thresholds for the number of entries to keep
$maxEntries = 30; // Default maximum number of entries
if ($totalLength > 5000) { // Higher threshold
$maxEntries = 20;
}
if ($totalLength > 10000) { // Lower threshold
$maxEntries = 10;
}
while (count($context_history) >= $maxEntries) {
array_shift($context_history); // Remove the oldest element
}
if (is_array($newEntry)) {
$newEntry = json_encode($newEntry); // Convert the array to a string
}
array_push($context_history, $newEntry); // Append the new element
set_transient($transient_name, $context_history); // Update the transient
}
// Function to return message and response - Ver 1.6.1
function concatenateHistory($transient_name) {
$context_history = get_transient($transient_name);
if (!$context_history) {
return ''; // Return an empty string if the transient does not exist
}
return implode(' ', $context_history); // Concatenate the array values into a single string
}
// Call the ChatGPT API
function chatbot_chatgpt_call_api($api_key, $message) {
// Diagnostics - Ver 1.6.1
global $chatgpt_diagnostics;
// The current ChatGPT API URL endpoint for gpt-3.5-turbo and gpt-4
$api_url = 'https://api.openai.com/v1/chat/completions';
$headers = array(
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
);
// Select the OpenAI Model
// Get the saved model from the settings or default to "gpt-3.5-turbo"
$model = esc_attr(get_option('chatgpt_model_choice', 'gpt-3.5-turbo'));
// Max tokens - Ver 1.4.2
$max_tokens = intval(esc_attr(get_option('chatgpt_max_tokens_setting', '150')));
// Conversation Context - Ver 1.6.1
$context = "";
$context = esc_attr(get_option('chatbot_chatgpt_conversation_context', 'You are a versatile, friendly, and helpful assistant designed to support me in a variety of tasks.'));
// Context History - Ver 1.6.1
$chatgpt_last_response = concatenateHistory('context_history');
// Diagnostics - Ver 1.6.1
// error_log('context_history' . print_r($chatgpt_last_response, true));
// Knowledge Navigator keyword append for context
$chatbot_chatgpt_kn_conversation_context = get_option('chatbot_chatgpt_kn_conversation_context', '');
// Append prior message, then context, then Knowledge Navigator - Ver 1.6.1
$context = $chatgpt_last_response . ' ' . $context . ' ' . $chatbot_chatgpt_kn_conversation_context;
// Diagnostics - Ver 1.6.1
// error_log('$context: ' . print_r($context, true));
// Added Role, System, Content Static Veriable - Ver 1.6.0
$body = array(
'model' => $model,
'max_tokens' => $max_tokens,
'temperature' => 0.5,
'messages' => array(
array('role' => 'system', 'content' => $context),
array('role' => 'user', 'content' => $message)
),
);
// Context History - Ver 1.6.1
addEntry('context_history', $message);
// Diagnostics - Ver 1.6.1
// error_log('storedc: ' . print_r($chatbot_chatgpt_kn_conversation_context, true));
// error_log('context: ' . print_r($context, true));
// error_log('message: ' . print_r($message, true));
$args = array(
'headers' => $headers,
'body' => json_encode($body),
'method' => 'POST',
'data_format' => 'body',
'timeout' => 50, // Increase the timeout values to 15 seconds to wait just a bit longer for a response from the engine
);
$response = wp_remote_post($api_url, $args);
// Handle any errors that are returned from the chat engine
if (is_wp_error($response)) {
return 'Error: ' . $response->get_error_message().' Please check Settings for a valid API key or your OpenAI account for additional information.';
}
// Return json_decode(wp_remote_retrieve_body($response), true);
$response_body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($response_body['choices']) && !empty($response_body['choices'])) {
// Handle the response from the chat engine
// Diagnostics - Ver 1.6.1
// error_log('$response_body: ' . print_r($response_body['choices'][0]['message']['content'], true));
// Context History - Ver 1.6.1
addEntry('context_history', $response_body['choices'][0]['message']['content']);
return $response_body['choices'][0]['message']['content'];
} else {
// Handle any errors that are returned from the chat engine
return 'Error: Unable to fetch response from ChatGPT API. Please check Settings for a valid API key or your OpenAI account for additional information.';
}
}
function enqueue_greetings_script() {
global $chatgpt_diagnostics;
// Diagnostics - Ver 1.6.1
// error_log('ENTERING enqueue_greetings_script');
wp_enqueue_script('greetings', plugin_dir_url(__FILE__) . 'assets/js/greetings.js', array('jquery'), null, true);
$greetings = array(
'initial_greeting' => esc_attr(get_option('chatgpt_initial_greeting', 'Hello! How can I help you today?')),
'subsequent_greeting' => esc_attr(get_option('chatgpt_subsequent_greeting', 'Hello again! How can I help you?')),
);
wp_localize_script('greetings', 'greetings_data', $greetings);
// Diagnostics - Ver 1.6.1
// error_log('EXITING enqueue_greetings_script');
}
add_action('wp_enqueue_scripts', 'enqueue_greetings_script');