文章主题:AI摘要 1. AI摘要是一种基于人工智能技术的文章生成工具,它可以自动提取文章的关键信息,生成简洁明了的总结。 2. 使用AI摘要可以节省用户撰写文章的时间,提高工作效率。 3. 在当前文章中添加一行代码,调用AI摘要函数,生成不超过180字的总结。
前提条件:
注册 https://www.xfyun.cn/
访问 https://console.xfyun.cn/app/myapp 创建应用侧边栏选择 Spark Lite 复制 APIPassword
开始修改!
1. 在Jon主题目录下的 /module/single/
创建AISummary.php
内容为:
<?php
function AISummary($content)
{
$url = "https://spark-api-open.xf-yun.com/v1/chat/completions";
$data = [
"model" => "lite",
"messages" => [
[
"role" => "user",
"content" => $content . "\n\n\n\n为这篇文章做个不超过200字的总结"
]
]
];
$headers = [
"Authorization: Bearer 你的APIPassword",
"Content-Type: application/json"
];
// 初始化cURL会话
$ch = curl_init($url);
// 滚草泥马,不验证SSL证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// 执行cURL请求
$response = curl_exec($ch);
// 检查是否有错误
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
}
// 关闭cURL会话
curl_close($ch);
$response = json_decode($response, true, 1024);
return $response["choices"][0]['message']['content'];
}
?>
<div class="panel-b">
<div class="ai-summary-block">
<p><span class="typing-effect"><?= AISummary($this->content) ?></span></p>
</div>
<style>
.typing-effect {
white-space: nowrap;
overflow: hidden;
animation: typing 2s steps(40) forwards;
}
@keyframes typing {
from {
width: 0;
}
to {
width: 100%;
}
}
.panel-b {
overflow-y: scroll;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
const typingElements = document.querySelectorAll('.typing-effect');
typingElements.forEach(element => {
element.classList.remove('typing-effect');
const text = element.textContent;
element.textContent = '';
let i = 0;
const typingInterval = setInterval(() => {
if (i < text.length) {
element.textContent += text[i];
i++;
} else {
clearInterval(typingInterval);
}
}, 50);
});
});
</script>
</div>
<div class="relative">
<div class="flex ac single-metabox abs-right">
<div class="post-metas">
<i class="fa fa-twitch" style="font-size: 1.0em; font-weight:bold;"></i>
<item class="meta-comm">AI摘要</item>
</div>
</div>
<i class="line-form-line"></i>
</div>
2. 还是在当前目录修改 article.php
<article class="joe_detail__article" data-nav="posts">
<?php
下面添加一行
require_once 'AISummary.php';