mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-20 18:54:09 +00:00
perf: 知识库匹配模式
This commit is contained in:
@@ -13,6 +13,7 @@ import { connectRedis } from '@/service/redis';
|
|||||||
import { VecModelDataPrefix } from '@/constants/redis';
|
import { VecModelDataPrefix } from '@/constants/redis';
|
||||||
import { vectorToBuffer } from '@/utils/tools';
|
import { vectorToBuffer } from '@/utils/tools';
|
||||||
import { openaiCreateEmbedding, gpt35StreamResponse } from '@/service/utils/openai';
|
import { openaiCreateEmbedding, gpt35StreamResponse } from '@/service/utils/openai';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
/* 发送提示词 */
|
/* 发送提示词 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -70,7 +71,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
`idx:${VecModelDataPrefix}:hash`,
|
`idx:${VecModelDataPrefix}:hash`,
|
||||||
`@modelId:{${String(
|
`@modelId:{${String(
|
||||||
chat.modelId._id
|
chat.modelId._id
|
||||||
)}} @vector:[VECTOR_RANGE 0.24 $blob]=>{$YIELD_DISTANCE_AS: score}`,
|
)}} @vector:[VECTOR_RANGE 0.22 $blob]=>{$YIELD_DISTANCE_AS: score}`,
|
||||||
'RETURN',
|
'RETURN',
|
||||||
'1',
|
'1',
|
||||||
'text',
|
'text',
|
||||||
@@ -97,12 +98,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (formatRedisPrompt.length > 0) {
|
if (formatRedisPrompt.length > 0) {
|
||||||
// textArr 筛选,最多 2800 tokens
|
// 系统提示词过滤,最多 2800 tokens
|
||||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2800);
|
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2800);
|
||||||
|
|
||||||
prompts.unshift({
|
prompts.unshift({
|
||||||
obj: 'SYSTEM',
|
obj: 'SYSTEM',
|
||||||
value: `${model.systemPrompt} 知识库内容是最新的,知识库内容为: "${systemPrompt}"`
|
value: `${model.systemPrompt} 用知识库内容回答,知识库内容为: "当前时间:${dayjs().format(
|
||||||
|
'YYYY/MM/DD HH:mm:ss'
|
||||||
|
)} ${systemPrompt}"`
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return res.send('对不起,你的问题不在知识库中。');
|
return res.send('对不起,你的问题不在知识库中。');
|
||||||
|
@@ -16,6 +16,7 @@ import { connectRedis } from '@/service/redis';
|
|||||||
import { VecModelDataPrefix } from '@/constants/redis';
|
import { VecModelDataPrefix } from '@/constants/redis';
|
||||||
import { vectorToBuffer } from '@/utils/tools';
|
import { vectorToBuffer } from '@/utils/tools';
|
||||||
import { openaiCreateEmbedding, gpt35StreamResponse } from '@/service/utils/openai';
|
import { openaiCreateEmbedding, gpt35StreamResponse } from '@/service/utils/openai';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
/* 发送提示词 */
|
/* 发送提示词 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -87,7 +88,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
const redisData: any[] = await redis.sendCommand([
|
const redisData: any[] = await redis.sendCommand([
|
||||||
'FT.SEARCH',
|
'FT.SEARCH',
|
||||||
`idx:${VecModelDataPrefix}:hash`,
|
`idx:${VecModelDataPrefix}:hash`,
|
||||||
`@modelId:{${modelId}} @vector:[VECTOR_RANGE 0.24 $blob]=>{$YIELD_DISTANCE_AS: score}`,
|
`@modelId:{${modelId}} @vector:[VECTOR_RANGE 0.22 $blob]=>{$YIELD_DISTANCE_AS: score}`,
|
||||||
'RETURN',
|
'RETURN',
|
||||||
'1',
|
'1',
|
||||||
'text',
|
'text',
|
||||||
@@ -114,22 +115,24 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formatRedisPrompt.length === 0) {
|
|
||||||
throw new Error('对不起,我没有找到你的问题');
|
|
||||||
}
|
|
||||||
|
|
||||||
// system 合并
|
// system 合并
|
||||||
if (prompts[0].obj === 'SYSTEM') {
|
if (prompts[0].obj === 'SYSTEM') {
|
||||||
formatRedisPrompt.unshift(prompts.shift()?.value || '');
|
formatRedisPrompt.unshift(prompts.shift()?.value || '');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 系统提示词筛选,最多 2800 tokens
|
if (formatRedisPrompt.length > 0) {
|
||||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2800);
|
// 系统提示词过滤,最多 2800 tokens
|
||||||
|
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2800);
|
||||||
|
|
||||||
prompts.unshift({
|
prompts.unshift({
|
||||||
obj: 'SYSTEM',
|
obj: 'SYSTEM',
|
||||||
value: `${model.systemPrompt} 知识库内容是最新的,知识库内容为: "${systemPrompt}"`
|
value: `${model.systemPrompt} 用知识库内容回答,知识库内容为: "当前时间:${dayjs().format(
|
||||||
});
|
'YYYY/MM/DD HH:mm:ss'
|
||||||
|
)} ${systemPrompt}"`
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return res.send('对不起,你的问题不在知识库中。');
|
||||||
|
}
|
||||||
|
|
||||||
// 控制在 tokens 数量,防止超出
|
// 控制在 tokens 数量,防止超出
|
||||||
const filterPrompts = openaiChatFilter(prompts, modelConstantsData.contextMaxToken);
|
const filterPrompts = openaiChatFilter(prompts, modelConstantsData.contextMaxToken);
|
||||||
|
@@ -152,7 +152,7 @@ const ModelDataCard = ({ model }: { model: ModelSchema }) => {
|
|||||||
>
|
>
|
||||||
手动输入
|
手动输入
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onClick={onOpenSelectFileModal}>文本内容 QA 拆分</MenuItem>
|
<MenuItem onClick={onOpenSelectFileModal}>文本/文件 QA 拆分</MenuItem>
|
||||||
<MenuItem onClick={onOpenSelectUrlModal}>网站内容 QA 拆分</MenuItem>
|
<MenuItem onClick={onOpenSelectUrlModal}>网站内容 QA 拆分</MenuItem>
|
||||||
<MenuItem onClick={onOpenSelectCsvModal}>csv 问答对导入</MenuItem>
|
<MenuItem onClick={onOpenSelectCsvModal}>csv 问答对导入</MenuItem>
|
||||||
</MenuList>
|
</MenuList>
|
||||||
|
@@ -91,7 +91,7 @@ export const openaiChatFilter = (prompts: ChatItemType[], maxTokens: number) =>
|
|||||||
const formatPrompts = prompts.map((item) => ({
|
const formatPrompts = prompts.map((item) => ({
|
||||||
obj: item.obj,
|
obj: item.obj,
|
||||||
value: item.value
|
value: item.value
|
||||||
.replace(/[\u3000\u3001\uff01-\uff5e\u3002]/g, ' ') // 中文标点改空格
|
// .replace(/[\u3000\u3001\uff01-\uff5e\u3002]/g, ' ') // 中文标点改空格
|
||||||
.replace(/\n+/g, '\n') // 连续空行
|
.replace(/\n+/g, '\n') // 连续空行
|
||||||
.replace(/[^\S\r\n]+/g, ' ') // 连续空白内容
|
.replace(/[^\S\r\n]+/g, ' ') // 连续空白内容
|
||||||
.trim()
|
.trim()
|
||||||
|
Reference in New Issue
Block a user