perf: collection created response (#1947)

* perf: collection created response

* update openapi doc

* remove default collection

* perf: chat ui

* fix: system prompt concat

* perf: published check

* perf: update app
This commit is contained in:
Archer
2024-07-05 10:27:38 +08:00
committed by GitHub
parent 8a7bd689ae
commit 88d10451c9
26 changed files with 160 additions and 165 deletions

View File

@@ -41,7 +41,6 @@ export const useChatBox = () => {
const map: Record<ExportChatType, () => void> = {
md: () => {
console.log(history);
fileDownload({
text: history
.map((item) => {

View File

@@ -1,4 +1,7 @@
import { PushDatasetDataChunkProps } from '@fastgpt/global/core/dataset/api';
import {
PushDatasetDataChunkProps,
PushDatasetDataResponse
} from '@fastgpt/global/core/dataset/api';
import {
DatasetSearchModeEnum,
DatasetSourceReadTypeEnum,
@@ -31,6 +34,10 @@ export type RebuildEmbeddingProps = {
};
/* ================= collection ===================== */
export type CreateCollectionResponse = Promise<{
collectionId: string;
results: PushDatasetDataResponse;
}>;
/* ================= data ===================== */
export type InsertOneDatasetDataProps = PushDatasetDataChunkProps & {

View File

@@ -154,10 +154,8 @@ async function handler(req: ApiRequestProps<AppUpdateParams, { appId: string }>)
return onUpdate(session);
});
}
// Update default permission
if (isDefaultPermissionChanged) {
} else if (isDefaultPermissionChanged) {
// Update default permission
await mongoSessionRun(async (session) => {
if (isFolder) {
// Sync children default permission
@@ -193,6 +191,8 @@ async function handler(req: ApiRequestProps<AppUpdateParams, { appId: string }>)
return onUpdate(session, defaultPermission);
});
} else {
return onUpdate();
}
}

View File

@@ -42,7 +42,7 @@ async function handler(
}
// get app and history
const [{ history }, { nodes }] = await Promise.all([
const [{ histories }, { nodes }] = await Promise.all([
getChatItems({
appId,
chatId,
@@ -60,7 +60,7 @@ async function handler(
title: chat?.title || '新对话',
userAvatar: undefined,
variables: chat?.variables || {},
history,
history: histories,
app: {
chatConfig: getAppChatConfig({
chatConfig: app.chatConfig,

View File

@@ -41,7 +41,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
throw new Error(ChatErrEnum.unAuthChat);
}
const [{ history }, { nodes }] = await Promise.all([
const [{ histories }, { nodes }] = await Promise.all([
getChatItems({
appId: app._id,
chatId,
@@ -56,7 +56,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
]);
// pick share response field
history.forEach((item) => {
histories.forEach((item) => {
if (item.obj === ChatRoleEnum.AI) {
item.responseData = filterPublicNodeResponseData({ flowResponses: item.responseData });
}
@@ -70,7 +70,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
//@ts-ignore
userAvatar: tmb?.userId?.avatar,
variables: chat?.variables || {},
history,
history: histories,
app: {
chatConfig: getAppChatConfig({
chatConfig: app.chatConfig,

View File

@@ -47,7 +47,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
// get app and history
const [{ history }, { nodes }] = await Promise.all([
const [{ histories }, { nodes }] = await Promise.all([
getChatItems({
appId,
chatId,
@@ -58,7 +58,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
]);
// pick share response field
history.forEach((item) => {
histories.forEach((item) => {
if (item.obj === ChatRoleEnum.AI) {
item.responseData = filterPublicNodeResponseData({ flowResponses: item.responseData });
}
@@ -71,7 +71,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
title: chat?.title || '新对话',
userAvatar: team?.avatar,
variables: chat?.variables || {},
history,
history: histories,
app: {
chatConfig: getAppChatConfig({
chatConfig: app.chatConfig,

View File

@@ -18,8 +18,9 @@ import { getLLMModel, getVectorModel } from '@fastgpt/service/core/ai/model';
import { rawText2Chunks } from '@fastgpt/service/core/dataset/read';
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
import { NextAPI } from '@/service/middleware/entry';
import { CreateCollectionResponse } from '@/global/core/dataset/api';
async function handler(req: NextApiRequest) {
async function handler(req: NextApiRequest): CreateCollectionResponse {
const { datasetId, parentId, fileId } = req.body as FileIdCreateDatasetCollectionParams;
const trainingType = TrainingModeEnum.chunk;
const { teamId, tmbId, dataset } = await authDataset({
@@ -50,7 +51,7 @@ async function handler(req: NextApiRequest) {
insertLen: predictDataLimitLength(trainingType, chunks)
});
await mongoSessionRun(async (session) => {
return mongoSessionRun(async (session) => {
// 4. create collection
const { _id: collectionId } = await createOneCollection({
teamId,
@@ -80,7 +81,7 @@ async function handler(req: NextApiRequest) {
});
// 6. insert to training queue
await pushDataListToTrainingQueue({
const insertResult = await pushDataListToTrainingQueue({
teamId,
tmbId,
datasetId: dataset._id,
@@ -97,7 +98,7 @@ async function handler(req: NextApiRequest) {
session
});
return collectionId;
return { collectionId, results: insertResult };
});
}
export default NextAPI(handler);

View File

@@ -15,8 +15,9 @@ import { reloadCollectionChunks } from '@fastgpt/service/core/dataset/collection
import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun';
import { NextAPI } from '@/service/middleware/entry';
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
import { CreateCollectionResponse } from '@/global/core/dataset/api';
async function handler(req: NextApiRequest) {
async function handler(req: NextApiRequest): CreateCollectionResponse {
const {
link,
trainingType = TrainingModeEnum.chunk,
@@ -40,7 +41,7 @@ async function handler(req: NextApiRequest) {
insertLen: predictDataLimitLength(trainingType, new Array(10))
});
await mongoSessionRun(async (session) => {
return mongoSessionRun(async (session) => {
// 2. create collection
const collection = await createOneCollection({
...body,
@@ -70,7 +71,7 @@ async function handler(req: NextApiRequest) {
});
// load
await reloadCollectionChunks({
const result = await reloadCollectionChunks({
collection: {
...collection.toObject(),
datasetId: dataset
@@ -80,7 +81,12 @@ async function handler(req: NextApiRequest) {
session
});
return collection;
return {
collectionId: collection._id,
results: {
insertLen: result.insertLen
}
};
});
}

View File

@@ -23,8 +23,9 @@ import { MongoImage } from '@fastgpt/service/common/file/image/schema';
import { readRawTextByLocalFile } from '@fastgpt/service/common/file/read/utils';
import { NextAPI } from '@/service/middleware/entry';
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
import { CreateCollectionResponse } from '@/global/core/dataset/api';
async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
async function handler(req: NextApiRequest, res: NextApiResponse<any>): CreateCollectionResponse {
/**
* Creates the multer uploader
*/

View File

@@ -17,8 +17,9 @@ import { getLLMModel, getVectorModel } from '@fastgpt/service/core/ai/model';
import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun';
import { NextAPI } from '@/service/middleware/entry';
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
import { CreateCollectionResponse } from '@/global/core/dataset/api';
async function handler(req: NextApiRequest) {
async function handler(req: NextApiRequest): CreateCollectionResponse {
const {
name,
text,

View File

@@ -1,7 +1,6 @@
import type { NextApiRequest } from 'next';
import { MongoDataset } from '@fastgpt/service/core/dataset/schema';
import type { CreateDatasetParams } from '@/global/core/dataset/api.d';
import { createDefaultCollection } from '@fastgpt/service/core/dataset/collection/controller';
import { authUserPer } from '@fastgpt/service/support/permission/user/auth';
import { DatasetTypeEnum } from '@fastgpt/global/core/dataset/constants';
import { getLLMModel, getVectorModel, getDatasetModel } from '@fastgpt/service/core/ai/model';
@@ -50,14 +49,6 @@ async function handler(req: NextApiRequest) {
defaultPermission
});
if (type === DatasetTypeEnum.dataset) {
await createDefaultCollection({
datasetId: _id,
teamId,
tmbId
});
}
return _id;
}

View File

@@ -26,6 +26,7 @@ import requestIp from 'request-ip';
import { getUsageSourceByAuthType } from '@fastgpt/global/support/wallet/usage/tools';
import { authTeamSpaceToken } from '@/service/support/permission/auth/team';
import {
concatHistories,
filterPublicNodeResponseData,
removeEmptyUserInput
} from '@fastgpt/global/core/chat/utils';
@@ -119,6 +120,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
let startTime = Date.now();
// Web chat params: [Human, AI]
const chatMessages = GPTMessages2Chats(messages);
if (chatMessages[chatMessages.length - 1].obj !== ChatRoleEnum.Human) {
chatMessages.pop();
@@ -170,7 +172,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
// 1. get and concat history; 2. get app workflow
const limit = getMaxHistoryLimitFromNodes(app.modules);
const [{ history }, { nodes, edges, chatConfig }] = await Promise.all([
const [{ histories }, { nodes, edges, chatConfig }] = await Promise.all([
getChatItems({
appId: app._id,
chatId,
@@ -179,7 +181,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}),
getAppLatestVersion(app._id, app)
]);
const concatHistories = history.concat(chatMessages);
const newHistories = concatHistories(histories, chatMessages);
const responseChatItemId: string | undefined = messages[messages.length - 1].dataId;
/* start flow controller */
@@ -198,7 +200,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
runtimeEdges: initWorkflowEdgeStatus(edges),
variables,
query: removeEmptyUserInput(question.value),
histories: concatHistories,
histories: newHistories,
stream,
detail,
maxRunTimes: 200
@@ -217,7 +219,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
modules: setEntryEntries(app.modules),
variables,
inputFiles: files,
histories: concatHistories,
histories: newHistories,
startParams: {
userChatInput: text
},

View File

@@ -10,7 +10,6 @@ import {
Checkbox,
ModalFooter
} from '@chakra-ui/react';
import { DragHandleIcon } from '@chakra-ui/icons';
import { useRouter } from 'next/router';
import { AppSchema } from '@fastgpt/global/core/app/type.d';
import { useTranslation } from 'next-i18next';
@@ -28,11 +27,13 @@ import MyModal from '@fastgpt/web/components/common/MyModal';
import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
import { postTransition2Workflow } from '@/web/core/app/api/app';
import { AppTypeEnum } from '@fastgpt/global/core/app/constants';
import { useSystem } from '@fastgpt/web/hooks/useSystem';
const AppCard = () => {
const router = useRouter();
const { t } = useTranslation();
const { appT } = useI18n();
const { isPc } = useSystem();
const { appDetail, setAppDetail, onOpenInfoEdit, onDelApp } = useContextSelector(
AppContext,
@@ -68,7 +69,7 @@ const AppCard = () => {
return (
<>
{/* basic info */}
<Box px={6} py={4} position={'relative'}>
<Box px={[4, 6]} py={4} position={'relative'}>
<Flex alignItems={'center'}>
<Avatar src={appDetail.avatar} borderRadius={'md'} w={'28px'} />
<Box ml={3} fontWeight={'bold'} fontSize={'md'} flex={'1 0 0'} color={'myGray.900'}>
@@ -149,13 +150,15 @@ const AppCard = () => {
/>
)}
<Box flex={1} />
<MyTag
type="borderFill"
colorSchema="gray"
onClick={() => (appDetail.permission.hasManagePer ? onOpenInfoEdit() : undefined)}
>
<PermissionIconText defaultPermission={appDetail.defaultPermission} />
</MyTag>
{isPc && (
<MyTag
type="borderFill"
colorSchema="gray"
onClick={() => (appDetail.permission.hasManagePer ? onOpenInfoEdit() : undefined)}
>
<PermissionIconText defaultPermission={appDetail.defaultPermission} />
</MyTag>
)}
</HStack>
</Box>
{TeamTagsSet && <TagsEditModal onClose={() => setTeamTagsSet(undefined)} />}

View File

@@ -29,7 +29,6 @@ import type { SettingAIDataType } from '@fastgpt/global/core/app/type.d';
import DeleteIcon, { hoverDeleteStyles } from '@fastgpt/web/components/common/Icon/delete';
import { TTSTypeEnum } from '@/web/core/app/constants';
import { getSystemVariables } from '@/web/core/app/utils';
import { useUpdate } from 'ahooks';
import { useI18n } from '@/web/context/I18n';
import { useContextSelector } from 'use-context-selector';
import { AppContext } from '@/pages/app/detail/components/context';
@@ -49,7 +48,7 @@ const ScheduledTriggerConfig = dynamic(
const WelcomeTextConfig = dynamic(() => import('@/components/core/app/WelcomeTextConfig'));
const BoxStyles: BoxProps = {
px: 5,
px: [4, 6],
py: '16px',
borderBottomWidth: '1px',
borderBottomColor: 'borderColor.low'

View File

@@ -140,7 +140,7 @@ const ChatHistorySlider = ({
fontSize={'sm'}
onClick={() =>
canRouteToDetail &&
router.replace({
router.push({
pathname: '/app/detail',
query: { appId }
})
@@ -156,27 +156,26 @@ const ChatHistorySlider = ({
{/* menu */}
<Flex w={'100%'} px={[2, 5]} h={'36px'} my={5} alignItems={'center'}>
<Box flex={'1 0 0'}>
{!isPc && appId && (
<LightRowTabs<TabEnum>
mr={1}
inlineStyles={{
px: 1
}}
list={[
{ label: t('core.chat.Recent use'), value: TabEnum.recently },
...(!isTeamChat ? [{ label: t('App'), value: TabEnum.app }] : []),
{ label: t('core.chat.History'), value: TabEnum.history }
]}
value={currentTab}
onChange={setCurrentTab}
/>
)}
</Box>
{!isPc && appId && (
<LightRowTabs<TabEnum>
flex={'1 0 0'}
mr={1}
inlineStyles={{
px: 1
}}
list={[
{ label: t('core.chat.Recent use'), value: TabEnum.recently },
...(!isTeamChat ? [{ label: t('App'), value: TabEnum.app }] : []),
{ label: t('core.chat.History'), value: TabEnum.history }
]}
value={currentTab}
onChange={setCurrentTab}
/>
)}
<Button
variant={'whitePrimary'}
flex={['auto', 1]}
flex={[appId ? '0 0 auto' : 1, 1]}
h={'100%'}
color={'primary.600'}
borderRadius={'xl'}

View File

@@ -11,7 +11,7 @@ const ToolMenu = ({ history }: { history: ChatItemType[] }) => {
const { t } = useTranslation();
const { onExportChat } = useChatBox();
const router = useRouter();
console.log(history);
return history.length > 0 ? (
<MyMenu
Button={

View File

@@ -34,7 +34,6 @@ export async function insertData2Dataset({
session?: ClientSession;
}) {
if (!q || !datasetId || !collectionId || !model) {
console.log(q, a, datasetId, collectionId, model);
return Promise.reject('q, datasetId, collectionId, model is required');
}
if (String(teamId) === String(tmbId)) {
@@ -140,7 +139,7 @@ export async function updateData2Dataset({
formatIndexes.unshift(defaultIndex ? defaultIndex : getDefaultIndex({ q, a }));
}
formatIndexes = formatIndexes.slice(0, 6);
console.log(formatIndexes);
// patch indexes, create, update, delete
const patchResult: PatchIndexesProps[] = [];

View File

@@ -441,41 +441,46 @@ export const compareWorkflow = (workflow1: WorkflowType, workflow2: WorkflowType
return false;
}
const node1 = clone1.nodes.filter(Boolean).map((node) => ({
flowNodeType: node.flowNodeType,
inputs: node.inputs.map((input) => ({
...input,
value: input.value ?? undefined
})),
outputs: node.outputs.map((input) => ({
...input,
value: input.value ?? undefined
})),
name: node.name,
intro: node.intro,
avatar: node.avatar,
version: node.version,
position: node.position
}));
const node2 = clone2.nodes.filter(Boolean).map((node) => ({
flowNodeType: node.flowNodeType,
inputs: node.inputs.map((input) => ({
...input,
value: input.value ?? undefined
})),
outputs: node.outputs.map((input) => ({
...input,
value: input.value ?? undefined
})),
name: node.name,
intro: node.intro,
avatar: node.avatar,
version: node.version,
position: node.position
}));
const formatNodes = (nodes: StoreNodeItemType[]) => {
return nodes
.filter((node) => {
if (!node) return;
if ([FlowNodeTypeEnum.systemConfig].includes(node.flowNodeType)) return;
return true;
})
.map((node) => ({
flowNodeType: node.flowNodeType,
inputs: node.inputs.map((input) => ({
key: input.key,
selectedTypeIndex: input.selectedTypeIndex ?? 0,
renderTypeLis: input.renderTypeList,
valueType: input.valueType,
value: input.value ?? undefined
})),
outputs: node.outputs.map((item) => ({
key: item.key,
type: item.type,
value: item.value ?? undefined
})),
name: node.name,
intro: node.intro,
avatar: node.avatar,
version: node.version,
position: node.position
}));
};
const node1 = formatNodes(clone1.nodes);
const node2 = formatNodes(clone2.nodes);
// console.log(node1);
// console.log(node2);
node1.forEach((node, i) => {
if (!isEqual(node, node2[i])) {
console.log('node not equal');
}
});
return isEqual(node1, node2);
};