Files
wordpress-theme-aksrt/inc/classes/class-aksrt-ai-prompt.php
T

118 lines
3.4 KiB
PHP

<?php
/**
* AKSRT AI Prompt — AI Prompt 模板管理
*
* 管理各种 AI 功能的 prompt 模板。
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_AI_Prompt {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* 构建 SEO 建议 prompt
*
* 优先读取后台 ai_seo_*_prompt 设置,为空则使用内置默认提示词。
*/
public function build_seo_prompt(string $title, string $content): string {
// 读取后台自定义提示词
$custom_title = '';
$custom_kw = '';
$custom_desc = '';
if (class_exists('AKSRT_Settings')) {
$s = AKSRT_Settings::get_all();
$custom_title = trim((string) ($s['ai_seo_title_prompt'] ?? ''));
$custom_kw = trim((string) ($s['ai_seo_keywords_prompt'] ?? ''));
$custom_desc = trim((string) ($s['ai_seo_description_prompt'] ?? ''));
}
// 内置默认提示词(zibll 风格长提示词)
$title_instruction = $custom_title ?: '请为文章生成一个 SEO 友好的标题,要求:包含核心关键词,控制在 60 个字符以内,语言自然流畅,吸引用户点击。';
$kw_instruction = $custom_kw ?: '请为文章提取 5-8 个 SEO 关键词,要求:涵盖核心主题词和长尾词,按搜索热度排序,用英文逗号分隔。';
$desc_instruction = $custom_desc ?: '请为文章生成一段 SEO 描述(meta description),要求:160 个字符以内,准确概括文章内容,包含核心关键词,具有吸引力能促进点击。';
return <<<PROMPT
请根据以下文章内容,生成 SEO 优化建议。
文章标题:{$title}
文章内容:
{$content}
请以 JSON 格式返回,包含以下字段:
- suggested_title: {$title_instruction}
- suggested_description: {$desc_instruction}
- suggested_keywords: {$kw_instruction}(以 JSON 数组形式返回)
只返回 JSON,不要其他内容。
PROMPT;
}
/**
* 构建内容摘要 prompt
*/
public function build_summary_prompt(string $content, int $max_length = 200): string {
$content = mb_substr($content, 0, 3000);
return <<<PROMPT
请为以下内容生成一段简洁的摘要,不超过 {$max_length} 个字。
内容:
{$content}
只返回摘要文本,不要其他内容。
PROMPT;
}
/**
* 构建标签建议 prompt
*/
public function build_tags_prompt(string $title, string $content): string {
$content = mb_substr($content, 0, 2000);
return <<<PROMPT
请根据以下文章内容,建议合适的标签。
文章标题:{$title}
文章内容:
{$content}
请以 JSON 数组格式返回标签列表(5-10个),例如:["标签1", "标签2"]
只返回 JSON 数组,不要其他内容。
PROMPT;
}
/**
* 构建摘要建议 prompt
*/
public function build_excerpt_prompt(string $title, string $content): string {
$content = mb_substr($content, 0, 2000);
return <<<PROMPT
请为以下文章生成一段吸引人的摘要(excerpt),用于列表页展示。
文章标题:{$title}
文章内容:
{$content}
要求:
- 50-100字
- 突出文章核心价值
- 吸引读者点击
只返回摘要文本,不要其他内容。
PROMPT;
}
}