
简介
最近在玩儿 检索增强生成(RAG,Retrieval Augmented Generation), 本地部署了一套 dify, 应该算是 RAG 功能最全的开源项目了, 可以集成工大厂商的 AI API 以及自建的 LLM 服务.
所以就用 dify 做了一个个人知识库, 数据来源与博客内容.
dify 的部署以及使用可查看官方文档, 写的非常详细, 这里只是介绍一下如何将 dify 集成到 hexo 的博客中.
根据 官方文档 的说明, 我选择使用 script 标签方式
集成 dify 到博客中, 这种方式会有一个聊天机器人按钮, 不会影响博客的整体体验:

集成
首先从 dify 获取嵌入到网站中的代码, 比如下面这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <script> window.difyChatbotConfig = { token: 'xxxxxxxxxxxx', baseUrl: 'http://192.168.1.2' } </script> <script src="http://192.168.1.2/embed.min.js" id="xxxxxxxxxxxx" defer> </script> <style> #dify-chatbot-bubble-button { background-color: #1C64F2 !important; } #dify-chatbot-bubble-window { width: 24rem !important; height: 40rem !important; } </style>
|
embed.min.js
文件我下载到本地并上传到了 CDN, 然后对上述代码进行了拆分.
根据个人 hexo 配置的不同部分步骤或代码不一定适用, 但是思路都是一样.
在 source/js/dify
目录下新建下面 2 个 js 文件(/js/dify
没有就新建, 当然你可以在其他地方创建, 只要能被 hexo 引用)
dify-chat.embed.min.js
用于保存上述 embed.min.js
中的代码.
另一个是 dify-chat.js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| window.difyChatbotConfig = { token: "xxxxxxxxxxxx", isDev: false, baseUrl: "https://dify.dong4j.ink:1024", containerProps: {}, draggable: false, dragAxis: "both", inputs: { }, };
|
然后对 css 样式进行自定义修改, 可以新建 dify-chat.css
文件, 也可以根据官方文档, 在 difu-chat.js
进行添加, 我这里选择新建 css 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| :root { --dify-chatbot-bubble-button-bottom: 4.5rem; --dify-chatbot-bubble-button-right: unset; --dify-chatbot-bubble-button-left: 1.5rem; --dify-chatbot-bubble-button-top: unset; --dify-chatbot-bubble-button-bg-color: #155EEF; --dify-chatbot-bubble-button-width: 35px; --dify-chatbot-bubble-button-height: 35px; --dify-chatbot-bubble-button-border-radius: 30px; --dify-chatbot-bubble-button-box-shadow: rgba(0, 0, 0, 0.2) 0px 4px 8px 0px; --dify-chatbot-bubble-button-hover-transform: scale(1.1); }
|
我将按钮固定到了左下角, 这样不会影响右下角的 hexo 按钮.
hexo 加载
dify 对于移动设备不是很友好, 所以我只在非移动设备上开启了 dify 聊天功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function loadStylesheet(href) { const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = href; document.head.appendChild(link); }
function loadScript(src, async = true) { const script = document.createElement('script'); script.src = src; script.async = async; document.body.appendChild(script); }
function isMobileDevice() { const userAgent = navigator.userAgent || navigator.vendor || window.opera; return /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent.toLowerCase()); }
if (!isMobileDevice()) { loadStylesheet('https://cdn.dong4j.site/source/static/dify-chat.css'); loadScript('https://cdn.dong4j.site/source/static/dify-chat.embed.min.js'); loadScript('https://cdn.dong4j.site/source/static/dify-chat.js'); }
|
整体效果:

总结
目前知识库还在持续建设, 因为现在 DS 的接口还不太稳定, 现在还使用的 GLM4 来提供 AI 对话服务, 后期会将 LLM 服务更换为 DeepSeek-R1.