






















最近刚好白嫖到了DeepSeek 的大模型API,就想着给自己的博客加上AI文章摘要的功能,但原本我博客用的anzhiyu主题集成的是TianliGPT的接口,并且只能使用他们家的接口,这就有点难受,所以扒了一下主题的源代码,发现有可以改的地方,故出此教程记录一下这个修改的过程
准备工作:
修改博客目录下_config_anzhiyu.yml 文件中post_head_ai_description的内容
例子:
post_head_ai_description:
enable: true
gptName: AnZhiYu
mode: tianli # 默认模式 可选值: tianli/local
switchBtn: false # 可以配置是否显示切换按钮 以切换tianli/local
btnLink: https://afdian.net/item/886a79d4db6711eda42a52540025c377
randomNum: 3 # 按钮最大的随机次数,也就是一篇文章最大随机出来几种
basicWordCount: 1000 # 最低获取字符数, 最小1000, 最大1999
key:
Referer:Ps:Key和Referer每家的大模型都不太相同,自行查阅官方的接口文档
修改主题目录下themes/anzhiyu/source/js/anzhiyu/的ai_abstract.js文件
主要修改的地方有以下几点:
async function readAloud()函数,主要是注释掉了判断tianliGPT是否可用的请求代码,例子:async function readAloud() {
if (!summaryID) {
anzhiyu.snackbarShow("摘要还没加载完呢,请稍后。。。");
return;
}
aiReadAloudIcon = post_ai.querySelector(".anzhiyu-icon-circle-dot");
aiReadAloudIcon.style.opacity = "0.2";
if (audio && !isPaused) {
audio.pause();
isPaused = true;
aiReadAloudIcon.style.opacity = "1";
aiReadAloudIcon.style.animation = "";
aiReadAloudIcon.style.cssText = "animation: ''; opacity: 1;cursor: pointer;";
return;
}
if (audio && isPaused) {
audio.play();
isPaused = false;
aiReadAloudIcon.style.cssText = "animation: breathe .5s linear infinite; opacity: 0.2;cursor: pointer";
return;
}
// const options = {
// key: AIKey,
// Referer: AIReferer,
// };
// const requestParams = new URLSearchParams({
// key: options.key,
// id: summaryID,
// });
// const requestOptions = {
// method: "GET",
// headers: {
// "Content-Type": "application/json",
// Referer: options.Referer,
// },
// };
// try {
// const response = await fetch(`https://summary.tianli0.top/audio?${requestParams}`, requestOptions);
// if (response.status === 403) {
// console.error("403 refer与key不匹配。");
// } else if (response.status === 500) {
// console.error("500 系统内部错误");
// } else {
// const audioBlob = await response.blob();
// const audioURL = URL.createObjectURL(audioBlob);
// audio = new Audio(audioURL);
// audio.play();
// aiReadAloudIcon.style.cssText = "animation: breathe .5s linear infinite; opacity: 0.2;cursor: pointer";
// audio.addEventListener("ended", () => {
// audio = null;
// aiReadAloudIcon.style.opacity = "1";
// aiReadAloudIcon.style.animation = "";
// });
// }
// }
// catch (error) {
// console.error("请求发生错误❎");
// }
}DeepSeek api为例:DeepSeek的api的Request只要求messages与model字段是Required,所以找到async function aiAbstractTianli(num)函数,修改requestBody,关闭api的流式输出,即stream=false,此处的大模型Prompt可自由发挥,如图:
DeepSeek的api要求在请求标头加入Authentication字段来验证key,格式:bearer + xxxxxxxx(key),找到async function aiAbstractTianli(num)函数,修改requestOptions中的headers字段,如图:
async function aiAbstractTianli(num)函数下面找到const response修改api的请求地址为option.Referer:请求完毕之后获取大模型的总结摘要结果,具体的获取方式需要查看官方接口文档的非流式的Responses字段,DeepSeek的Responses字段如图:
找到async function aiAbstractTianli(num)函数,修改summary参数,Result为大模型输出的Json格式的数据,如图:
最终效果:
以上的方式存在key暴露的风险,建议自己搭建一个后端用于转发请求,将key写入环境变量中,以及增加更多校验方式,降低风险
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。