mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 20:37:48 +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)];
|
||||
};
|
||||
|
Reference in New Issue
Block a user