惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

RisingIce

2024年终总结 | RisingIce 使用大模型总结B站视频 | RisingIce 使用Vercel搭建Umami统计 | RisingIce 部署Twikoo评论系统 | RisingIce Vercel域名被墙的问题 | RisingIce Ubuntu挂载群晖共享文件夹与Comfyui模型迁移 | RisingIce 记一次conda环境激活却不起作用的问题及解决方法 | RisingIce 《程序员的README》- 随笔 | RisingIce Hello Word的由来 | RisingIce
修改anzhiyu主题的TianliGPT插件为其他大模型API | RisingIce
871996643@qq.com (RisingIce) · 2024-03-05 · via RisingIce

文章摘要 RisingIce

加载中...|

此内容由DeepSeek根据文章生成,未经过人工审核,仅用于文章内容的解释与总结 投诉

一、前言

最近刚好白嫖到了DeepSeek 的大模型API,就想着给自己的博客加上AI文章摘要的功能,但原本我博客用的anzhiyu主题集成的是TianliGPT的接口,并且只能使用他们家的接口,这就有点难受,所以扒了一下主题的源代码,发现有可以改的地方,故出此教程记录一下这个修改的过程

二、修改步骤

准备工作:

  • 能用的大模型API并且知道该API的调用方法及参数(必备)
  • anzhiyu主题为本地部署的(即你的主题文件位于blog项目的theme目录下,而不是npm安装的方式)

2.1 开启AI摘要功能

修改博客目录下_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:
  • mode修改为tianli
  • key修改为你自己的大模型API的密钥
  • Referer修改为你自己的大模型API的请求地址

Ps:Key和Referer每家的大模型都不太相同,自行查阅官方的接口文档

2.2 修改大模型请求脚本

修改主题目录下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("请求发生错误❎");
    // }
  }
  • 此处要修改文章摘要请求的大模型API的方式,这里以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:

2.3 获取模型输出内容及展示

请求完毕之后获取大模型的总结摘要结果,具体的获取方式需要查看官方接口文档的非流式的Responses字段,DeepSeek的Responses字段如图:

找到async function aiAbstractTianli(num)函数,修改summary参数,Result为大模型输出的Json格式的数据,如图:

最终效果:

三、 存在的风险

以上的方式存在key暴露的风险,建议自己搭建一个后端用于转发请求,将key写入环境变量中,以及增加更多校验方式,降低风险