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 {
|
||||
TrainingModeEnum,
|
||||
DatasetCollectionTypeEnum
|
||||
} from '@fastgpt/global/core/dataset/constants';
|
||||
import { TrainingModeEnum } from '@fastgpt/global/core/dataset/constants';
|
||||
import type { CreateDatasetCollectionParams } from '@fastgpt/global/core/dataset/api.d';
|
||||
import { MongoDatasetCollection } from './schema';
|
||||
import {
|
||||
@@ -15,6 +12,7 @@ import { deleteDatasetDataVector } from '../../../common/vectorStore/controller'
|
||||
import { delFileByFileIdList } from '../../../common/file/gridfs/controller';
|
||||
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
||||
import { ClientSession } from '../../../common/mongo';
|
||||
import { createOrGetCollectionTags } from './utils';
|
||||
|
||||
export async function createOneCollection({
|
||||
teamId,
|
||||
@@ -39,6 +37,7 @@ export async function createOneCollection({
|
||||
rawTextLength,
|
||||
metadata = {},
|
||||
session,
|
||||
tags,
|
||||
...props
|
||||
}: CreateDatasetCollectionParams & {
|
||||
teamId: string;
|
||||
@@ -46,6 +45,7 @@ export async function createOneCollection({
|
||||
[key: string]: any;
|
||||
session?: ClientSession;
|
||||
}) {
|
||||
const collectionTags = await createOrGetCollectionTags({ tags, teamId, datasetId, session });
|
||||
const [collection] = await MongoDatasetCollection.create(
|
||||
[
|
||||
{
|
||||
@@ -69,7 +69,8 @@ export async function createOneCollection({
|
||||
|
||||
rawTextLength,
|
||||
hashRawText,
|
||||
metadata
|
||||
metadata,
|
||||
tags: collectionTags
|
||||
}
|
||||
],
|
||||
{ session }
|
||||
|
@@ -11,6 +11,7 @@ import {
|
||||
import { hashStr } from '@fastgpt/global/common/string/tools';
|
||||
import { ClientSession } from '../../../common/mongo';
|
||||
import { PushDatasetDataResponse } from '@fastgpt/global/core/dataset/api';
|
||||
import { MongoDatasetCollectionTags } from '../tag/schema';
|
||||
|
||||
/**
|
||||
* get all collection by top collectionId
|
||||
@@ -200,3 +201,36 @@ export const reloadCollectionChunks = async ({
|
||||
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';
|
||||
|
||||
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 { teamId, tmbId, dataset } = await authDataset({
|
||||
req,
|
||||
@@ -54,6 +54,7 @@ async function handler(req: NextApiRequest): CreateCollectionResponse {
|
||||
return mongoSessionRun(async (session) => {
|
||||
// 4. create collection
|
||||
const { _id: collectionId } = await createOneCollection({
|
||||
...body,
|
||||
teamId,
|
||||
tmbId,
|
||||
name: filename,
|
||||
|
@@ -205,7 +205,6 @@ const HeaderTagPopOver = () => {
|
||||
variant={'unstyled'}
|
||||
onClick={() => {
|
||||
onOpenTagManageModal();
|
||||
setCheckedTags([]);
|
||||
}}
|
||||
>
|
||||
{t('dataset:tag.manage')}
|
||||
|
@@ -21,7 +21,6 @@ import MyInput from '@/components/MyInput';
|
||||
import { DatasetTagType } from '@fastgpt/global/core/dataset/type';
|
||||
import { useScrollPagination } from '@fastgpt/web/hooks/useScrollPagination';
|
||||
import EmptyTip from '@fastgpt/web/components/common/EmptyTip';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import PopoverConfirm from '@fastgpt/web/components/common/MyPopover/PopoverConfirm';
|
||||
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 loadDatasetTags = useContextSelector(DatasetPageContext, (v) => v.loadDatasetTags);
|
||||
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 editInputRef = useRef<HTMLInputElement>(null);
|
||||
@@ -156,7 +155,8 @@ const TagManageModal = ({ onClose }: { onClose: () => void }) => {
|
||||
});
|
||||
|
||||
const { data: tagUsages } = useRequest2(() => getTagUsage(datasetDetail._id), {
|
||||
manual: false
|
||||
manual: false,
|
||||
refreshDeps: [collections]
|
||||
});
|
||||
|
||||
return (
|
||||
|
Reference in New Issue
Block a user