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 vectorSearch = await PgClient.select<{ id: string; q: string; a: string }>('modelData', {
|
||||
fields: ['id', 'q', 'a'],
|
||||
where: [['model_id', model._id], 'AND', `vector <=> '[${promptVector}]' < ${similarity}`],
|
||||
order: [{ field: 'vector', mode: `<=> '[${promptVector}]'` }],
|
||||
where: [
|
||||
['model_id', model._id],
|
||||
'AND',
|
||||
['user_id', userId],
|
||||
'AND',
|
||||
`vector <=> '[${promptVector}]' < ${similarity}`
|
||||
],
|
||||
limit: 30
|
||||
});
|
||||
|
||||
@@ -97,8 +91,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
} else {
|
||||
// 有匹配情况下,添加知识库内容。
|
||||
// 系统提示词过滤,最多 2500 tokens
|
||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2500);
|
||||
// 系统提示词过滤,最多 2000 tokens
|
||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2000);
|
||||
|
||||
prompts.unshift({
|
||||
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}`);
|
||||
|
||||
// textArr 筛选,最多 3000 tokens
|
||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 3000);
|
||||
// textArr 筛选,最多 2500 tokens
|
||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2500);
|
||||
|
||||
prompts.unshift({
|
||||
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 vectorSearch = await PgClient.select<{ id: string; q: string; a: string }>('modelData', {
|
||||
fields: ['id', 'q', 'a'],
|
||||
where: [['model_id', model._id], 'AND', `vector <=> '[${promptVector}]' < ${similarity}`],
|
||||
order: [{ field: 'vector', mode: `<=> '[${promptVector}]'` }],
|
||||
where: [
|
||||
['model_id', model._id],
|
||||
'AND',
|
||||
['user_id', userId],
|
||||
'AND',
|
||||
`vector <=> '[${promptVector}]' < ${similarity}`
|
||||
],
|
||||
limit: 30
|
||||
});
|
||||
|
||||
@@ -116,8 +110,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
} else {
|
||||
// 有匹配或者低匹配度模式情况下,添加知识库内容。
|
||||
// 系统提示词过滤,最多 2500 tokens
|
||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2500);
|
||||
// 系统提示词过滤,最多 2000 tokens
|
||||
const systemPrompt = systemPromptFilter(formatRedisPrompt, 2000);
|
||||
|
||||
prompts.unshift({
|
||||
obj: 'SYSTEM',
|
||||
|
@@ -23,7 +23,9 @@ export const pushChatBill = async ({
|
||||
// 计算 token 数量
|
||||
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) {
|
||||
await connectToDatabase();
|
||||
@@ -79,7 +81,9 @@ export const pushSplitDataBill = async ({
|
||||
let billId;
|
||||
|
||||
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) {
|
||||
try {
|
||||
@@ -130,7 +134,9 @@ export const pushGenerateVectorBill = async ({
|
||||
let billId;
|
||||
|
||||
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) {
|
||||
try {
|
||||
|
@@ -16,7 +16,7 @@ export async function connectToDatabase(): Promise<void> {
|
||||
mongoose.set('strictQuery', true);
|
||||
global.mongodb = await mongoose.connect(process.env.MONGODB_URI as string, {
|
||||
bufferCommands: true,
|
||||
dbName: process.env.NODE_ENV === 'development' ? 'doc_gpt_test' : 'doc_gpt',
|
||||
dbName: process.env.MONGODB_NAME,
|
||||
maxPoolSize: 5,
|
||||
minPoolSize: 1,
|
||||
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