Phi-3-mini-4k-instruct-gguf实战教程:集成到Notion插件实现笔记自动摘要
Phi-3-mini-4k-instruct-gguf实战教程:集成到Notion插件实现笔记自动摘要
1. 项目背景与目标
你是否经常在Notion中积累了大量笔记,却苦于没有时间整理和提炼关键信息?本文将带你一步步将Phi-3-mini-4k-instruct-gguf模型集成到Notion插件中,实现笔记内容的自动摘要功能。
Phi-3-mini-4k-instruct-gguf是微软推出的轻量级文本生成模型,特别适合处理问答、文本改写和摘要整理等任务。通过本教程,你将学会:
- 如何调用Phi-3-mini-4k-instruct-gguf的API
- 如何开发一个简单的Notion插件
- 如何将两者结合实现自动摘要功能
2. 环境准备与模型部署
2.1 获取模型访问权限
首先确保你可以访问Phi-3-mini-4k-instruct-gguf模型。如果你使用的是CSDN提供的镜像服务,可以直接通过以下地址访问:
https://gpu-3sbnmfumnj-7860.web.gpu.csdn.net/2.2 测试模型基础功能
在浏览器中打开上述地址,尝试输入以下测试提示词,确认模型正常工作:
请用中文一句话介绍你自己。如果看到模型返回合理的回答,说明环境准备就绪。
2.3 安装必要开发工具
我们需要以下工具来开发Notion插件:
# 安装Node.js和npm sudo apt install nodejs npm # 安装Notion官方开发工具 npm install -g @notionhq/client # 安装axios用于HTTP请求 npm install axios3. 开发Notion插件基础框架
3.1 创建插件项目
首先创建一个新的Node.js项目:
mkdir notion-phi3-summarizer cd notion-phi3-summarizer npm init -y3.2 配置Notion集成
- 登录Notion开发者平台(https://www.notion.so/my-integrations)
- 点击"New integration"创建新集成
- 填写名称(如"Phi-3 Summarizer")并选择关联工作区
- 记下生成的"Internal Integration Token"
3.3 基础代码结构
创建index.js文件,添加以下基础代码:
const { Client } = require('@notionhq/client'); const axios = require('axios'); // 初始化Notion客户端 const notion = new Client({ auth: process.env.NOTION_TOKEN, }); // Phi-3模型API地址 const PHI3_API_URL = 'https://gpu-3sbnmfumnj-7860.web.gpu.csdn.net/generate'; async function summarizeText(text) { // 这里将添加调用Phi-3模型的代码 } // 主函数 async function main() { // 这里将添加与Notion交互的代码 } main().catch(console.error);4. 集成Phi-3模型实现摘要功能
4.1 设计摘要提示词
Phi-3模型对提示词格式比较敏感。我们设计一个专门用于摘要生成的提示词模板:
请为以下文本生成简洁的摘要,保留核心信息,长度控制在3-5句话内: {{TEXT}}4.2 实现模型调用函数
更新summarizeText函数:
async function summarizeText(text) { try { const prompt = `请为以下文本生成简洁的摘要,保留核心信息,长度控制在3-5句话内:\n\n${text}`; const response = await axios.post(PHI3_API_URL, { prompt: prompt, max_length: 256, temperature: 0.2 }); return response.data.result; } catch (error) { console.error('调用Phi-3模型失败:', error); return null; } }4.3 测试摘要功能
添加测试代码验证功能是否正常工作:
async function testSummarize() { const testText = "人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器。该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。人工智能从诞生以来,理论和技术日益成熟,应用领域也不断扩大。"; const summary = await summarizeText(testText); console.log("生成的摘要:", summary); } testSummarize();运行测试应该能看到类似这样的输出:
生成的摘要: 人工智能是计算机科学的分支,旨在模拟人类智能。它研究领域包括机器人、语言和图像识别等。随着理论和技术发展,AI应用范围不断扩大。5. 实现Notion页面处理功能
5.1 获取Notion页面内容
添加函数从Notion页面提取文本内容:
async function getPageContent(pageId) { const blocks = await notion.blocks.children.list({ block_id: pageId, }); let textContent = ''; for (const block of blocks.results) { if (block.type === 'paragraph' && block.paragraph.rich_text) { for (const text of block.paragraph.rich_text) { textContent += text.plain_text + '\n'; } } } return textContent; }5.2 更新页面添加摘要
添加函数将生成的摘要插入到Notion页面:
async function addSummaryToPage(pageId, summary) { await notion.blocks.children.append({ block_id: pageId, children: [ { heading_2: { rich_text: [ { text: { content: "AI摘要" } } ] } }, { paragraph: { rich_text: [ { text: { content: summary } } ] } } ] }); }6. 完整工作流实现
6.1 主函数实现
更新main函数实现完整流程:
async function main() { // 从命令行参数获取Notion页面ID const pageId = process.argv[2]; if (!pageId) { console.log('请提供Notion页面ID作为参数'); return; } console.log('正在处理页面...'); const content = await getPageContent(pageId); if (!content) { console.log('页面内容为空'); return; } console.log('正在生成摘要...'); const summary = await summarizeText(content); if (!summary) { console.log('摘要生成失败'); return; } console.log('正在更新页面...'); await addSummaryToPage(pageId, summary); console.log('摘要添加完成!'); }6.2 运行插件
现在你可以这样运行插件:
NOTION_TOKEN=你的集成token node index.js 你的页面ID7. 进阶优化建议
7.1 添加错误处理
完善错误处理逻辑,确保插件更健壮:
async function main() { try { const pageId = process.argv[2]; if (!pageId) { throw new Error('请提供Notion页面ID作为参数'); } // ...原有代码... } catch (error) { console.error('处理失败:', error.message); process.exit(1); } }7.2 支持更多内容类型
扩展getPageContent函数支持更多块类型:
async function getPageContent(pageId) { const blocks = await notion.blocks.children.list({ block_id: pageId, }); let textContent = ''; for (const block of blocks.results) { switch (block.type) { case 'paragraph': case 'heading_1': case 'heading_2': case 'heading_3': if (block[block.type].rich_text) { for (const text of block[block.type].rich_text) { textContent += text.plain_text + '\n'; } } break; case 'bulleted_list_item': case 'numbered_list_item': if (block[block.type].rich_text) { for (const text of block[block.type].rich_text) { textContent += '• ' + text.plain_text + '\n'; } } break; // 可以继续添加对其他块类型的支持 } } return textContent; }7.3 添加配置选项
通过配置文件控制模型参数:
const config = { max_length: 256, temperature: 0.2, summary_heading: "AI摘要" }; // 然后在相关函数中使用这些配置8. 总结与下一步
通过本教程,你已经成功将Phi-3-mini-4k-instruct-gguf模型集成到Notion插件中,实现了笔记自动摘要功能。这个基础版本还可以进一步扩展:
- 添加定时自动摘要功能
- 支持多种摘要风格选择
- 实现批量处理多个页面
- 添加摘要质量评分功能
Phi-3-mini-4k-instruct-gguf模型虽然轻量,但在文本摘要任务上表现优秀,特别适合集成到各种应用中。希望这个教程能帮助你开启AI增强生产力工具的开发之旅。
获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。
