mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
feat: allow adding tags when creating collections via api (#2268)
* feat: allow adding tags when creating collections via api * fix
This commit is contained in:
@@ -1,7 +1,4 @@
|
|||||||
import {
|
import { TrainingModeEnum } from '@fastgpt/global/core/dataset/constants';
|
||||||
TrainingModeEnum,
|
|
||||||
DatasetCollectionTypeEnum
|
|
||||||
} from '@fastgpt/global/core/dataset/constants';
|
|
||||||
import type { CreateDatasetCollectionParams } from '@fastgpt/global/core/dataset/api.d';
|
import type { CreateDatasetCollectionParams } from '@fastgpt/global/core/dataset/api.d';
|
||||||
import { MongoDatasetCollection } from './schema';
|
import { MongoDatasetCollection } from './schema';
|
||||||
import {
|
import {
|
||||||
@@ -15,6 +12,7 @@ import { deleteDatasetDataVector } from '../../../common/vectorStore/controller'
|
|||||||
import { delFileByFileIdList } from '../../../common/file/gridfs/controller';
|
import { delFileByFileIdList } from '../../../common/file/gridfs/controller';
|
||||||
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
||||||
import { ClientSession } from '../../../common/mongo';
|
import { ClientSession } from '../../../common/mongo';
|
||||||
|
import { createOrGetCollectionTags } from './utils';
|
||||||
|
|
||||||
export async function createOneCollection({
|
export async function createOneCollection({
|
||||||
teamId,
|
teamId,
|
||||||
@@ -39,6 +37,7 @@ export async function createOneCollection({
|
|||||||
rawTextLength,
|
rawTextLength,
|
||||||
metadata = {},
|
metadata = {},
|
||||||
session,
|
session,
|
||||||
|
tags,
|
||||||
...props
|
...props
|
||||||
}: CreateDatasetCollectionParams & {
|
}: CreateDatasetCollectionParams & {
|
||||||
teamId: string;
|
teamId: string;
|
||||||
@@ -46,6 +45,7 @@ export async function createOneCollection({
|
|||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
session?: ClientSession;
|
session?: ClientSession;
|
||||||
}) {
|
}) {
|
||||||
|
const collectionTags = await createOrGetCollectionTags({ tags, teamId, datasetId, session });
|
||||||
const [collection] = await MongoDatasetCollection.create(
|
const [collection] = await MongoDatasetCollection.create(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@@ -69,7 +69,8 @@ export async function createOneCollection({
|
|||||||
|
|
||||||
rawTextLength,
|
rawTextLength,
|
||||||
hashRawText,
|
hashRawText,
|
||||||
metadata
|
metadata,
|
||||||
|
tags: collectionTags
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
{ session }
|
{ session }
|
||||||
|
@@ -11,6 +11,7 @@ import {
|
|||||||
import { hashStr } from '@fastgpt/global/common/string/tools';
|
import { hashStr } from '@fastgpt/global/common/string/tools';
|
||||||
import { ClientSession } from '../../../common/mongo';
|
import { ClientSession } from '../../../common/mongo';
|
||||||
import { PushDatasetDataResponse } from '@fastgpt/global/core/dataset/api';
|
import { PushDatasetDataResponse } from '@fastgpt/global/core/dataset/api';
|
||||||
|
import { MongoDatasetCollectionTags } from '../tag/schema';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get all collection by top collectionId
|
* get all collection by top collectionId
|
||||||
@@ -200,3 +201,36 @@ export const reloadCollectionChunks = async ({
|
|||||||
insertLen: result.length
|
insertLen: result.length
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const createOrGetCollectionTags = async ({
|
||||||
|
tags = [],
|
||||||
|
datasetId,
|
||||||
|
teamId,
|
||||||
|
session
|
||||||
|
}: {
|
||||||
|
tags?: string[];
|
||||||
|
datasetId: string;
|
||||||
|
teamId: string;
|
||||||
|
session?: ClientSession;
|
||||||
|
}): Promise<string[]> => {
|
||||||
|
if (!tags.length) return [];
|
||||||
|
const existingTags = await MongoDatasetCollectionTags.find({
|
||||||
|
teamId,
|
||||||
|
datasetId,
|
||||||
|
$expr: { $in: ['$tag', tags] }
|
||||||
|
});
|
||||||
|
|
||||||
|
const existingTagContents = existingTags.map((tag) => tag.tag);
|
||||||
|
const newTagContents = tags.filter((tag) => !existingTagContents.includes(tag));
|
||||||
|
|
||||||
|
const newTags = await MongoDatasetCollectionTags.insertMany(
|
||||||
|
newTagContents.map((tagContent) => ({
|
||||||
|
teamId,
|
||||||
|
datasetId,
|
||||||
|
tag: tagContent
|
||||||
|
})),
|
||||||
|
{ session }
|
||||||
|
);
|
||||||
|
|
||||||
|
return [...existingTags.map((tag) => tag._id), ...newTags.map((tag) => tag._id)];
|
||||||
|
};
|
||||||
|
@@ -21,7 +21,7 @@ import { NextAPI } from '@/service/middleware/entry';
|
|||||||
import { CreateCollectionResponse } from '@/global/core/dataset/api';
|
import { CreateCollectionResponse } from '@/global/core/dataset/api';
|
||||||
|
|
||||||
async function handler(req: NextApiRequest): CreateCollectionResponse {
|
async function handler(req: NextApiRequest): CreateCollectionResponse {
|
||||||
const { datasetId, parentId, fileId } = req.body as FileIdCreateDatasetCollectionParams;
|
const { datasetId, parentId, fileId, ...body } = req.body as FileIdCreateDatasetCollectionParams;
|
||||||
const trainingType = TrainingModeEnum.chunk;
|
const trainingType = TrainingModeEnum.chunk;
|
||||||
const { teamId, tmbId, dataset } = await authDataset({
|
const { teamId, tmbId, dataset } = await authDataset({
|
||||||
req,
|
req,
|
||||||
@@ -54,6 +54,7 @@ async function handler(req: NextApiRequest): CreateCollectionResponse {
|
|||||||
return mongoSessionRun(async (session) => {
|
return mongoSessionRun(async (session) => {
|
||||||
// 4. create collection
|
// 4. create collection
|
||||||
const { _id: collectionId } = await createOneCollection({
|
const { _id: collectionId } = await createOneCollection({
|
||||||
|
...body,
|
||||||
teamId,
|
teamId,
|
||||||
tmbId,
|
tmbId,
|
||||||
name: filename,
|
name: filename,
|
||||||
|
@@ -205,7 +205,6 @@ const HeaderTagPopOver = () => {
|
|||||||
variant={'unstyled'}
|
variant={'unstyled'}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
onOpenTagManageModal();
|
onOpenTagManageModal();
|
||||||
setCheckedTags([]);
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{t('dataset:tag.manage')}
|
{t('dataset:tag.manage')}
|
||||||
|
@@ -21,7 +21,6 @@ import MyInput from '@/components/MyInput';
|
|||||||
import { DatasetTagType } from '@fastgpt/global/core/dataset/type';
|
import { DatasetTagType } from '@fastgpt/global/core/dataset/type';
|
||||||
import { useScrollPagination } from '@fastgpt/web/hooks/useScrollPagination';
|
import { useScrollPagination } from '@fastgpt/web/hooks/useScrollPagination';
|
||||||
import EmptyTip from '@fastgpt/web/components/common/EmptyTip';
|
import EmptyTip from '@fastgpt/web/components/common/EmptyTip';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
|
||||||
import PopoverConfirm from '@fastgpt/web/components/common/MyPopover/PopoverConfirm';
|
import PopoverConfirm from '@fastgpt/web/components/common/MyPopover/PopoverConfirm';
|
||||||
import MyBox from '@fastgpt/web/components/common/MyBox';
|
import MyBox from '@fastgpt/web/components/common/MyBox';
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ const TagManageModal = ({ onClose }: { onClose: () => void }) => {
|
|||||||
const datasetDetail = useContextSelector(DatasetPageContext, (v) => v.datasetDetail);
|
const datasetDetail = useContextSelector(DatasetPageContext, (v) => v.datasetDetail);
|
||||||
const loadDatasetTags = useContextSelector(DatasetPageContext, (v) => v.loadDatasetTags);
|
const loadDatasetTags = useContextSelector(DatasetPageContext, (v) => v.loadDatasetTags);
|
||||||
const loadAllDatasetTags = useContextSelector(DatasetPageContext, (v) => v.loadAllDatasetTags);
|
const loadAllDatasetTags = useContextSelector(DatasetPageContext, (v) => v.loadAllDatasetTags);
|
||||||
const { getData } = useContextSelector(CollectionPageContext, (v) => v);
|
const { getData, collections } = useContextSelector(CollectionPageContext, (v) => v);
|
||||||
|
|
||||||
const tagInputRef = useRef<HTMLInputElement>(null);
|
const tagInputRef = useRef<HTMLInputElement>(null);
|
||||||
const editInputRef = useRef<HTMLInputElement>(null);
|
const editInputRef = useRef<HTMLInputElement>(null);
|
||||||
@@ -156,7 +155,8 @@ const TagManageModal = ({ onClose }: { onClose: () => void }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const { data: tagUsages } = useRequest2(() => getTagUsage(datasetDetail._id), {
|
const { data: tagUsages } = useRequest2(() => getTagUsage(datasetDetail._id), {
|
||||||
manual: false
|
manual: false,
|
||||||
|
refreshDeps: [collections]
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Reference in New Issue
Block a user