mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
perf: pg
This commit is contained in:
@@ -66,14 +66,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
const similarity = ModelVectorSearchModeMap[model.search.mode]?.similarity || 0.22;
|
const similarity = ModelVectorSearchModeMap[model.search.mode]?.similarity || 0.22;
|
||||||
const vectorSearch = await PgClient.select<{ id: string; q: string; a: string }>('modelData', {
|
const vectorSearch = await PgClient.select<{ id: string; q: string; a: string }>('modelData', {
|
||||||
fields: ['id', 'q', 'a'],
|
fields: ['id', 'q', 'a'],
|
||||||
|
where: [['model_id', model._id], 'AND', `vector <=> '[${promptVector}]' < ${similarity}`],
|
||||||
order: [{ field: 'vector', mode: `<=> '[${promptVector}]'` }],
|
order: [{ field: 'vector', mode: `<=> '[${promptVector}]'` }],
|
||||||
where: [
|
|
||||||
['model_id', model._id],
|
|
||||||
'AND',
|
|
||||||
['user_id', userId],
|
|
||||||
'AND',
|
|
||||||
`vector <=> '[${promptVector}]' < ${similarity}`
|
|
||||||
],
|
|
||||||
limit: 30
|
limit: 30
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -97,8 +91,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 有匹配情况下,添加知识库内容。
|
// 有匹配情况下,添加知识库内容。
|
||||||
// 系统提示词过滤,最多 2500 tokens
|
// 系统提示词过滤,最多 2000 tokens
|
||||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2500);
|
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2000);
|
||||||
|
|
||||||
prompts.unshift({
|
prompts.unshift({
|
||||||
obj: 'SYSTEM',
|
obj: 'SYSTEM',
|
||||||
|
@@ -163,8 +163,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
|
|
||||||
const formatRedisPrompt: string[] = vectorSearch.rows.map((item) => `${item.q}\n${item.a}`);
|
const formatRedisPrompt: string[] = vectorSearch.rows.map((item) => `${item.q}\n${item.a}`);
|
||||||
|
|
||||||
// textArr 筛选,最多 3000 tokens
|
// textArr 筛选,最多 2500 tokens
|
||||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 3000);
|
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2500);
|
||||||
|
|
||||||
prompts.unshift({
|
prompts.unshift({
|
||||||
obj: 'SYSTEM',
|
obj: 'SYSTEM',
|
||||||
|
@@ -80,14 +80,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
const similarity = ModelVectorSearchModeMap[model.search.mode]?.similarity || 0.22;
|
const similarity = ModelVectorSearchModeMap[model.search.mode]?.similarity || 0.22;
|
||||||
const vectorSearch = await PgClient.select<{ id: string; q: string; a: string }>('modelData', {
|
const vectorSearch = await PgClient.select<{ id: string; q: string; a: string }>('modelData', {
|
||||||
fields: ['id', 'q', 'a'],
|
fields: ['id', 'q', 'a'],
|
||||||
|
where: [['model_id', model._id], 'AND', `vector <=> '[${promptVector}]' < ${similarity}`],
|
||||||
order: [{ field: 'vector', mode: `<=> '[${promptVector}]'` }],
|
order: [{ field: 'vector', mode: `<=> '[${promptVector}]'` }],
|
||||||
where: [
|
|
||||||
['model_id', model._id],
|
|
||||||
'AND',
|
|
||||||
['user_id', userId],
|
|
||||||
'AND',
|
|
||||||
`vector <=> '[${promptVector}]' < ${similarity}`
|
|
||||||
],
|
|
||||||
limit: 30
|
limit: 30
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -116,8 +110,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 有匹配或者低匹配度模式情况下,添加知识库内容。
|
// 有匹配或者低匹配度模式情况下,添加知识库内容。
|
||||||
// 系统提示词过滤,最多 2500 tokens
|
// 系统提示词过滤,最多 2000 tokens
|
||||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2500);
|
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2000);
|
||||||
|
|
||||||
prompts.unshift({
|
prompts.unshift({
|
||||||
obj: 'SYSTEM',
|
obj: 'SYSTEM',
|
||||||
|
@@ -23,7 +23,9 @@ export const pushChatBill = async ({
|
|||||||
// 计算 token 数量
|
// 计算 token 数量
|
||||||
const tokens = Math.floor(encode(text).length * 0.75);
|
const tokens = Math.floor(encode(text).length * 0.75);
|
||||||
|
|
||||||
console.log(`chat generate success. text len: ${text.length}. token len: ${tokens}`);
|
console.log(
|
||||||
|
`chat generate success. text len: ${text.length}. token len: ${tokens}. pay:${isPay}`
|
||||||
|
);
|
||||||
|
|
||||||
if (isPay) {
|
if (isPay) {
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
@@ -79,7 +81,9 @@ export const pushSplitDataBill = async ({
|
|||||||
let billId;
|
let billId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`splitData generate success. text len: ${text.length}. token len: ${tokenLen}`);
|
console.log(
|
||||||
|
`splitData generate success. text len: ${text.length}. token len: ${tokenLen}. pay:${isPay}`
|
||||||
|
);
|
||||||
|
|
||||||
if (isPay) {
|
if (isPay) {
|
||||||
try {
|
try {
|
||||||
@@ -130,7 +134,9 @@ export const pushGenerateVectorBill = async ({
|
|||||||
let billId;
|
let billId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`vector generate success. text len: ${text.length}. token len: ${tokenLen}`);
|
console.log(
|
||||||
|
`vector generate success. text len: ${text.length}. token len: ${tokenLen}. pay:${isPay}`
|
||||||
|
);
|
||||||
|
|
||||||
if (isPay) {
|
if (isPay) {
|
||||||
try {
|
try {
|
||||||
|
@@ -16,7 +16,7 @@ export async function connectToDatabase(): Promise<void> {
|
|||||||
mongoose.set('strictQuery', true);
|
mongoose.set('strictQuery', true);
|
||||||
global.mongodb = await mongoose.connect(process.env.MONGODB_URI as string, {
|
global.mongodb = await mongoose.connect(process.env.MONGODB_URI as string, {
|
||||||
bufferCommands: true,
|
bufferCommands: true,
|
||||||
dbName: process.env.NODE_ENV === 'development' ? 'doc_gpt_test' : 'doc_gpt',
|
dbName: process.env.MONGODB_NAME,
|
||||||
maxPoolSize: 5,
|
maxPoolSize: 5,
|
||||||
minPoolSize: 1,
|
minPoolSize: 1,
|
||||||
maxConnecting: 5
|
maxConnecting: 5
|
||||||
|
@@ -1,44 +0,0 @@
|
|||||||
import { createClient } from 'redis';
|
|
||||||
import { customAlphabet } from 'nanoid';
|
|
||||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 10);
|
|
||||||
|
|
||||||
export const connectRedis = async () => {
|
|
||||||
// 断开了,重连
|
|
||||||
if (global.redisClient && !global.redisClient.isOpen) {
|
|
||||||
await global.redisClient.disconnect();
|
|
||||||
} else if (global.redisClient) {
|
|
||||||
// 没断开,不再连接
|
|
||||||
return global.redisClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
global.redisClient = createClient({
|
|
||||||
url: process.env.REDIS_URL
|
|
||||||
});
|
|
||||||
|
|
||||||
global.redisClient.on('error', (err) => {
|
|
||||||
console.log('Redis Client Error', err);
|
|
||||||
global.redisClient = null;
|
|
||||||
});
|
|
||||||
global.redisClient.on('end', () => {
|
|
||||||
global.redisClient = null;
|
|
||||||
});
|
|
||||||
global.redisClient.on('ready', () => {
|
|
||||||
console.log('redis connected');
|
|
||||||
});
|
|
||||||
|
|
||||||
await global.redisClient.connect();
|
|
||||||
|
|
||||||
await global.redisClient.SELECT(0);
|
|
||||||
|
|
||||||
return global.redisClient;
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error, '==');
|
|
||||||
global.redisClient = null;
|
|
||||||
return Promise.reject('redis 连接失败');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getKey = (prefix = '') => {
|
|
||||||
return `${prefix}:${nanoid()}`;
|
|
||||||
};
|
|
Reference in New Issue
Block a user