user_settings_meta_key = Options::appendPrefix('user_settings');
$this->ollama = new Ollama();
}
public function getSummarizedTitle(string $message, $title_prefix = 'Chat Log')
{
$message = wp_strip_all_tags($message);
switch (Options::get('title_generation_method')) {
case 'ollama':
$summary = $this->getSummaryOllama($message);
break;
default:
$summary = $this->getSummaryTextRank($message);
break;
}
return !empty($summary) ? array_shift($summary) : $title_prefix . ' ' . gmdate('Y-m-d H:i:s');
}
public function getSummaryOllama(string $message)
{
// Send text generation request to Ollama
// Prompt: Quickly summarize the text into a single short sentence.
// use model used for chat
$body = [
'model' => $this->getPostInput('model'),
'prompt' => '[INST]Summarize this text into a single short sentence.[/INST]' . $message,
];
// get assistant response
$response = $this->ollama->apiGenerate($body);
// return response
return $response;
}
public function getSummaryTextRank(string $message)
{
$text_rank = new TextRankFacade();
$stop_words = new English();
$text_rank->setStopWords($stop_words);
$stripped_json = wp_strip_all_tags($message);
return $text_rank->summarizeTextBasic($stripped_json);
}
public function addChatLog(string $model, string $prompt, array $json, int $post_id)
{
// check if saving is enabled
if (!Options::get('chat_history_save')) {
return $post_id;
}
if ($post_id > 0) {
$post = [
'ID' => $post_id,
];
} else {
$message_content = $json['message']['content'] ?? '';
$post_title = $this->getSummarizedTitle($message_content);
$stripped_json = wp_strip_all_tags($message_content);
$post = [
'post_title' => $post_title,
'post_excerpt' => wp_trim_excerpt($stripped_json),
'post_type' => 'chat_history',
'post_slug' => wp_generate_uuid4(),
'post_status' => 'publish',
];
}
// build user message matching api response
$user_message = [
'model' => $model,
'message' => [
'role' => get_current_user_id(),
'content' => $prompt,
]
];
if ($post_id > 0) {
$post_id = wp_update_post($post);
} else {
$post_id = wp_insert_post($post);
}
// pull existing meta, add new meta, update meta
$meta = get_post_meta($post_id, 'messages', true);
if (!is_array($meta)) $meta = [];
$meta[] = $user_message;
$meta[] = $json;
update_post_meta($post_id, 'messages', $meta);
if ($chat_mode = $this->getPostInput('chat_mode')) {
$chat_mode = 'chat_mode_' . $chat_mode; // 'chat_mode_chat' or 'chat_mode_generate
update_post_meta($post_id, $chat_mode, true);
}
if (is_wp_error($post_id)) {
return false;
}
return $post_id;
}
public function checkUserInputs()
{
// Model should not be set if user cannot change model
if (Options::get('user_can_change_model') === false) {
$this->setPostInput('model', false);
}
// Check for input errors
if (!$this->getPostInput('model') and !$this->getPostInput('prompt')) {
$this->outputAssistantErrorDialog('Please select a model and enter a prompt.');
return false;
} elseif (!$this->getPostInput('model')) {
if (Options::get('user_can_change_model') === false and Options::get('default_model')) {
// If user can change model and default model is set then set model to default model
$this->setPostInput('model', Options::get('default_model'));
} else {
// If user cannot change model or model isn't sent and default model is not set then output error
$this->outputAssistantErrorDialog('Ask your system administrator to select a default model.');
return false;
}
} elseif (!$this->getPostInput('prompt')) {
$this->outputAssistantErrorDialog('Please enter a prompt.');
return false;
}
// Check if inputs are empty
if (empty($this->getPostInput('model')) or empty($this->getPostInput('prompt'))) {
return false;
}
// All checks pass
return true;
}
public function getAssistantAvatarImg($role)
{
return '';
}
public function getAssistantAvatarUrl($role)
{
// Switch gravatar by role
switch (strtolower($role)) {
case 'assistant':
case 'ollama':
$url = ALPACA_BOT_DIR_URL . 'assets/img/ollama-large.png';
break;
default:
$url = ALPACA_BOT_DIR_URL . 'assets/img/alpaca-bot-512.png';
break;
}
return $url;
}
public function getHxMultiSwapLoadChat(string $endpoint = 'htmx/chat', string $trigger = 'click')
{
return ' hx-post="' . esc_attr($this->getRenderEndpoint($endpoint)) . '" hx-trigger="' . esc_attr($trigger) . '" hx-ext="multi-swap" hx-swap="multi:#ab-response:beforeend,#chat_id:outerHTML" hx-disabled-elt="this" hx-indicator="#indicator"';
}
public function getPostInput(string $name, $default = false)
{
return $this->post[$name] ?? $default;
}
public function getRenderEndpoint(string $endpoint = '', string $version = 'v1')
{
return get_rest_url(null, ALPACA_BOT . '/' . $version . '/' . $endpoint);
}
private function getUserSetting(string $option, $default = null)
{
$user_id = get_current_user_id();
$user_settings = get_user_meta($user_id, $this->user_settings_meta_key, true);
$value = Options::validateValue($user_settings[$option] ?? '');
if ($value) {
return $value;
}
return $default;
}
public function outputAdminNotice($message = '', $class = 'notice-success')
{
$out = '
' . $message . '