feat: kb ui

This commit is contained in:
archer
2023-07-14 11:49:36 +08:00
parent 358c4716f9
commit 5a96e167ee
24 changed files with 463 additions and 412 deletions

View File

@@ -7,6 +7,7 @@ import { getVector } from '../plugin/vector';
import type { KbTestItemType } from '@/types/plugin';
export type Props = {
model: string;
kbId: string;
text: string;
};
@@ -14,9 +15,9 @@ export type Response = KbTestItemType['results'];
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { kbId, text } = req.body as Props;
const { kbId, text, model } = req.body as Props;
if (!kbId || !text) {
if (!kbId || !text || !model) {
throw new Error('缺少参数');
}
@@ -27,7 +28,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
throw new Error('缺少用户ID');
}
const vector = await getVector({
const { vectors } = await getVector({
model,
userId,
input: [text]
});
@@ -36,9 +38,9 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
`BEGIN;
SET LOCAL ivfflat.probes = ${global.systemEnv.pgIvfflatProbe || 10};
select id,q,a,source,(vector <#> '[${
vector[0]
vectors[0]
}]') * -1 AS score from modelData where kb_id='${kbId}' AND user_id='${userId}' order by vector <#> '[${
vector[0]
vectors[0]
}]' limit 12;
COMMIT;`
);

View File

@@ -5,6 +5,13 @@ import { authUser } from '@/service/utils/auth';
import { PgClient } from '@/service/pg';
import type { KbDataItemType } from '@/types/plugin';
export type Response = {
id: string;
q: string;
a: string;
source: string;
};
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
let { dataId } = req.query as {

View File

@@ -18,10 +18,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await connectToDatabase();
const data = await KB.findOne({
_id: id,
userId
});
const data = await KB.findOne(
{
_id: id,
userId
},
'_id avatar name userId tags'
);
if (!data) {
throw new Error('kb is not exist');
@@ -33,7 +36,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
avatar: data.avatar,
name: data.name,
userId: data.userId,
updateTime: data.updateTime,
tags: data.tags.join(' ')
}
});

View File

@@ -2,8 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, KB } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { PgClient } from '@/service/pg';
import { KbItemType } from '@/types/plugin';
import { KbListItemType } from '@/types/plugin';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
@@ -12,25 +11,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await connectToDatabase();
const kbList = await KB.find({
userId
}).sort({ updateTime: -1 });
const kbList = await KB.find(
{
userId
},
'_id avatar name tags'
).sort({ updateTime: -1 });
const data = await Promise.all(
kbList.map(async (item) => ({
_id: item._id,
avatar: item.avatar,
name: item.name,
userId: item.userId,
updateTime: item.updateTime,
tags: item.tags.join(' '),
totalData: await PgClient.count('modelData', {
where: [['user_id', userId], 'AND', ['kb_id', item._id]]
})
tags: item.tags
}))
);
jsonRes<KbItemType[]>(res, {
jsonRes<KbListItemType[]>(res, {
data
});
} catch (err) {