mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-04 15:10:23 +00:00
feat(原生AI摘要功能支持缓存): 自定义缓存功能只支持memory,不支持文件缓存
(cherry picked from commit dd56dae44f7f555d9004e7d1f872085bded2cb86)
This commit is contained in:
@@ -277,6 +277,7 @@ const BLOG = {
|
|||||||
AI_SUMMARY_KEY:
|
AI_SUMMARY_KEY:
|
||||||
process.env.AI_SUMMARY_KEY ||
|
process.env.AI_SUMMARY_KEY ||
|
||||||
'',
|
'',
|
||||||
|
AI_SUMMARY_CACHE_TIME: process.env.AI_SUMMARY_CACHE_TIME || 1800, // 缓存时间,单位秒
|
||||||
AI_SUMMARY_WORD_LIMIT: process.env.AI_SUMMARY_WORD_LIMIT || 1000,
|
AI_SUMMARY_WORD_LIMIT: process.env.AI_SUMMARY_WORD_LIMIT || 1000,
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
4
lib/cache/cache_manager.js
vendored
4
lib/cache/cache_manager.js
vendored
@@ -20,12 +20,12 @@ export async function getDataFromCache(key, force) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setDataToCache(key, data) {
|
export async function setDataToCache(key, data, customCacheTime) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// console.trace('[API-->>缓存写入]:', key)
|
// console.trace('[API-->>缓存写入]:', key)
|
||||||
await getApi().setCache(key, data)
|
await getApi().setCache(key, data, customCacheTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function delCacheData(key) {
|
export async function delCacheData(key) {
|
||||||
|
|||||||
4
lib/cache/memory_cache.js
vendored
4
lib/cache/memory_cache.js
vendored
@@ -7,8 +7,8 @@ export async function getCache(key, options) {
|
|||||||
return await cache.get(key)
|
return await cache.get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setCache(key, data) {
|
export async function setCache(key, data, customCacheTime) {
|
||||||
await cache.put(key, data, cacheTime * 1000)
|
await cache.put(key, data, (customCacheTime || cacheTime) * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function delCache(key) {
|
export async function delCache(key) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
export async function getAiSummary(aiSummaryAPI, aiSummaryKey, truncatedText) {
|
export async function getAiSummary(aiSummaryAPI, aiSummaryKey, truncatedText) {
|
||||||
try {
|
try {
|
||||||
|
console.log('请求文章摘要', truncatedText.slice(0, 100))
|
||||||
const response = await fetch(aiSummaryAPI, {
|
const response = await fetch(aiSummaryAPI, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { idToUuid } from 'notion-utils'
|
|||||||
import Slug from '..'
|
import Slug from '..'
|
||||||
import { getPageContentText } from '@/pages/search/[keyword]'
|
import { getPageContentText } from '@/pages/search/[keyword]'
|
||||||
import { getAiSummary } from '@/lib/plugins/aiSummary'
|
import { getAiSummary } from '@/lib/plugins/aiSummary'
|
||||||
|
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据notion的slug访问页面
|
* 根据notion的slug访问页面
|
||||||
@@ -99,22 +100,30 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
|
|||||||
|
|
||||||
const aiSummaryAPI = siteConfig('AI_SUMMARY_API')
|
const aiSummaryAPI = siteConfig('AI_SUMMARY_API')
|
||||||
if (aiSummaryAPI) {
|
if (aiSummaryAPI) {
|
||||||
const aiSummaryKey = siteConfig('AI_SUMMARY_KEY')
|
|
||||||
const wordLimit = siteConfig('AI_SUMMARY_WORD_LIMIT', '1000')
|
|
||||||
const post = props.post
|
const post = props.post
|
||||||
let content = ''
|
const cacheKey = `ai_summary_${post.id}`
|
||||||
for (let heading of post.toc) {
|
let aiSummary = await getDataFromCache(cacheKey)
|
||||||
content += heading.text + ' '
|
if (aiSummary) {
|
||||||
|
props.post.aiSummary = aiSummary
|
||||||
|
} else {
|
||||||
|
const aiSummaryKey = siteConfig('AI_SUMMARY_KEY')
|
||||||
|
const aiSummaryCacheTime = siteConfig('AI_SUMMARY_CACHE_TIME')
|
||||||
|
const wordLimit = siteConfig('AI_SUMMARY_WORD_LIMIT', '1000')
|
||||||
|
let content = ''
|
||||||
|
for (let heading of post.toc) {
|
||||||
|
content += heading.text + ' '
|
||||||
|
}
|
||||||
|
content += getPageContentText(post, post.blockMap)
|
||||||
|
const combinedText = post.title + ' ' + content
|
||||||
|
const truncatedText = combinedText.slice(0, wordLimit)
|
||||||
|
aiSummary = await getAiSummary(
|
||||||
|
aiSummaryAPI,
|
||||||
|
aiSummaryKey,
|
||||||
|
truncatedText
|
||||||
|
)
|
||||||
|
await setDataToCache(cacheKey, aiSummary, aiSummaryCacheTime)
|
||||||
|
props.post.aiSummary = aiSummary
|
||||||
}
|
}
|
||||||
content += getPageContentText(post, post.blockMap)
|
|
||||||
const combinedText = post.title + ' ' + content
|
|
||||||
const truncatedText = combinedText.slice(0, wordLimit)
|
|
||||||
|
|
||||||
props.post.aiSummary = await getAiSummary(
|
|
||||||
aiSummaryAPI,
|
|
||||||
aiSummaryKey,
|
|
||||||
truncatedText
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user