Files
hermes-cpanel-agent/includes/ai-provider.php
T

113 lines
4.3 KiB
PHP

<?php
namespace HermesAiBridge;
defined('ABSPATH') or die();
class AiProvider {
protected $api_key;
protected $base_url;
protected $model;
protected $max_tokens;
protected $provider;
public function __construct() {
$this->provider = get_option('hermes_ai_provider', 'openai');
$this->model = get_option('hermes_ai_model', 'gpt-4');
$this->max_tokens = intval(get_option('hermes_ai_max_tokens', 2048));
$this->api_key = get_option('hermes_ai_api_key', '');
$this->base_url = get_option('hermes_ai_base_url', 'https://api.openai.com/v1');
}
/**
* Generate content from a prompt.
*/
public function generate($prompt, $context = 'general') {
$system_prompt = $this->get_system_prompt($context);
return $this->call_ai_api($system_prompt, $prompt);
}
/**
* Call the configured AI provider API.
*/
protected function call_ai_api($system, $user_prompt) {
if (empty($this->api_key)) {
return new \WP_Error('no_api_key', 'AI provider API key not configured. Set it in WordPress Admin → Hermes AI.');
}
$body = [
'model' => $this->model,
'max_tokens' => $this->max_tokens,
'messages' => [
['role' => 'system', 'content' => $system],
['role' => 'user', 'content' => $user_prompt],
],
];
$response = wp_remote_post($this->base_url . '/chat/completions', [
'timeout' => 120,
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json',
],
'body' => wp_json_encode($body),
]);
if (is_wp_error($response)) {
RestController::log('error', 'AI API call failed', ['error' => $response->get_error_message()]);
return $response;
}
$status = wp_remote_retrieve_response_code($response);
$body = json_decode(wp_remote_retrieve_body($response), true);
if ($status !== 200) {
$error_msg = $body['error']['message'] ?? 'Unknown API error';
RestController::log('error', 'AI API error', ['status' => $status, 'error' => $error_msg]);
return new \WP_Error('ai_api_error', "AI API returned $status: $error_msg");
}
$content = $body['choices'][0]['message']['content'] ?? '';
RestController::log('info', 'AI content generated', ['model' => $this->model, 'tokens' => $body['usage']['total_tokens'] ?? 0]);
return $content;
}
/**
* Newsletter-specific batch generation.
*/
public function generate_newsletter_content() {
$prompt = "Write a newsletter for Falah Letter — Islamic fintech and community updates. "
. "Include sections: featured article, community news, upcoming events, featured project. "
. "Write in a professional but warm tone. Target length: 1500-2000 words.";
$content = $this->generate($prompt, 'newsletter');
if (is_wp_error($content)) return;
$post_id = wp_insert_post([
'post_title' => 'Falah Letter — ' . wp_date('F j, Y'),
'post_content' => $content,
'post_type' => 'post',
'post_status' => 'draft',
'tags_input' => ['newsletter', 'falah-letter', 'automated'],
]);
if ($post_id) {
RestController::log('info', 'Newsletter draft generated', ['post_id' => $post_id]);
}
}
protected function get_system_prompt($context) {
$prompts = [
'post' => 'You are a content writer for FalahOS, an Islamic fintech platform. '
. 'Write engaging, well-structured content. Use markdown for formatting. '
. 'Keep paragraphs short and scannable.',
'newsletter' => 'You are the editor of Falah Letter, a weekly newsletter about '
. 'Islamic fintech, community updates, and FalahOS platform news. '
. 'Write in a professional warm tone.',
'social' => 'You are a social media manager for FalahOS. Create engaging posts '
. 'optimized for LinkedIn and Twitter. Use emojis sparingly.',
];
return $prompts[$context] ?? $prompts['post'];
}
}