perf: generate queue
This commit is contained in:
@@ -3,19 +3,21 @@ import type { KbDataItemType } from '@/types/plugin';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import { connectToDatabase, TrainingData } from '@/service/mongo';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
import { generateVector } from '@/service/events/generateVector';
|
||||
import { PgClient } from '@/service/pg';
|
||||
import { authKb } from '@/service/utils/auth';
|
||||
import { withNextCors } from '@/service/utils/tools';
|
||||
import { TrainingTypeEnum } from '@/constants/plugin';
|
||||
import { startQueue } from '@/service/utils/tools';
|
||||
|
||||
interface Props {
|
||||
export type Props = {
|
||||
kbId: string;
|
||||
data: { a: KbDataItemType['a']; q: KbDataItemType['q'] }[];
|
||||
}
|
||||
mode: `${TrainingTypeEnum}`;
|
||||
prompt?: string;
|
||||
};
|
||||
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
const { kbId, data } = req.body as Props;
|
||||
const { kbId, data, mode, prompt } = req.body as Props;
|
||||
|
||||
if (!kbId || !Array.isArray(data)) {
|
||||
throw new Error('缺少参数');
|
||||
@@ -29,7 +31,9 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
data: await pushDataToKb({
|
||||
kbId,
|
||||
data,
|
||||
userId
|
||||
userId,
|
||||
mode,
|
||||
prompt
|
||||
})
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -40,36 +44,43 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
}
|
||||
});
|
||||
|
||||
export async function pushDataToKb({ userId, kbId, data }: { userId: string } & Props) {
|
||||
export async function pushDataToKb({
|
||||
userId,
|
||||
kbId,
|
||||
data,
|
||||
mode,
|
||||
prompt
|
||||
}: { userId: string } & Props) {
|
||||
await authKb({
|
||||
userId,
|
||||
kbId
|
||||
});
|
||||
|
||||
if (data.length === 0) {
|
||||
return {
|
||||
trainingId: ''
|
||||
};
|
||||
return {};
|
||||
}
|
||||
|
||||
// 插入记录
|
||||
const { _id } = await TrainingData.create({
|
||||
userId,
|
||||
kbId,
|
||||
vectorList: data
|
||||
});
|
||||
await TrainingData.insertMany(
|
||||
data.map((item) => ({
|
||||
q: item.q,
|
||||
a: item.a,
|
||||
userId,
|
||||
kbId,
|
||||
mode,
|
||||
prompt
|
||||
}))
|
||||
);
|
||||
|
||||
generateVector(_id);
|
||||
startQueue();
|
||||
|
||||
return {
|
||||
trainingId: _id
|
||||
};
|
||||
return {};
|
||||
}
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: {
|
||||
sizeLimit: '100mb'
|
||||
sizeLimit: '20mb'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -33,7 +33,15 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
// 更新 pg 内容.仅修改a,不需要更新向量。
|
||||
await PgClient.update('modelData', {
|
||||
where: [['id', dataId], 'AND', ['user_id', userId]],
|
||||
values: [{ key: 'a', value: a }, ...(q ? [{ key: 'q', value: `${vector[0]}` }] : [])]
|
||||
values: [
|
||||
{ key: 'a', value: a },
|
||||
...(q
|
||||
? [
|
||||
{ key: 'q', value: q },
|
||||
{ key: 'vector', value: `[${vector[0]}]` }
|
||||
]
|
||||
: [])
|
||||
]
|
||||
});
|
||||
|
||||
jsonRes(res);
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import { connectToDatabase, TrainingData } from '@/service/mongo';
|
||||
import { authKb, authUser } from '@/service/utils/auth';
|
||||
import { generateQA } from '@/service/events/generateQA';
|
||||
import { TrainingTypeEnum } from '@/constants/plugin';
|
||||
import { withNextCors } from '@/service/utils/tools';
|
||||
import { pushDataToKb } from '../kb/pushData';
|
||||
|
||||
/* split text */
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const { chunks, kbId, prompt, mode } = req.body as {
|
||||
kbId: string;
|
||||
chunks: string[];
|
||||
prompt: string;
|
||||
mode: `${TrainingTypeEnum}`;
|
||||
};
|
||||
if (!chunks || !kbId || !prompt) {
|
||||
throw new Error('参数错误');
|
||||
}
|
||||
await connectToDatabase();
|
||||
|
||||
const { userId } = await authUser({ req });
|
||||
|
||||
// 验证是否是该用户的 model
|
||||
await authKb({
|
||||
kbId,
|
||||
userId
|
||||
});
|
||||
|
||||
if (mode === TrainingTypeEnum.qa) {
|
||||
// 批量QA拆分插入数据
|
||||
const { _id } = await TrainingData.create({
|
||||
userId,
|
||||
kbId,
|
||||
qaList: chunks,
|
||||
prompt
|
||||
});
|
||||
generateQA(_id);
|
||||
} else if (mode === TrainingTypeEnum.subsection) {
|
||||
// 分段导入,直接插入向量队列
|
||||
const response = await pushDataToKb({
|
||||
kbId,
|
||||
data: chunks.map((item) => ({ q: item, a: '' })),
|
||||
userId
|
||||
});
|
||||
|
||||
return jsonRes(res, {
|
||||
data: response
|
||||
});
|
||||
}
|
||||
|
||||
jsonRes(res);
|
||||
} catch (err) {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: {
|
||||
sizeLimit: '100mb'
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user