Skip to content

Commit

Permalink
v4.6.2-alpah (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu authored Nov 24, 2023
1 parent 60f7526 commit 9cb4280
Show file tree
Hide file tree
Showing 208 changed files with 5,383 additions and 3,487 deletions.
15 changes: 10 additions & 5 deletions packages/global/common/string/textSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const splitText2Chunks = (props: { text: string; maxLen: number; overlapL

4: /(\n\n)/g,
5: /([\n])/g,
6: /[。]|(?!<[^a-zA-Z])\.\s/g,
6: /([。]|(?!<[^a-zA-Z])\.\s)/g,
7: /([!?]|!\s|\?\s)/g,
8: /([;]|;\s)/g,
9: /([,]|,\s)/g
Expand Down Expand Up @@ -55,10 +55,15 @@ export const splitText2Chunks = (props: { text: string; maxLen: number; overlapL
}

// split text by special char
const splitTexts = text
.replace(reg, isMarkdownSplit ? `${tempMarker}$1` : `$1${tempMarker}`)
.split(`${tempMarker}`)
.filter((part) => part);
const splitTexts = (() => {
if (!reg.test(text)) {
return [text];
}
return text
.replace(reg, isMarkdownSplit ? `${tempMarker}$1` : `$1${tempMarker}`)
.split(`${tempMarker}`)
.filter((part) => part);
})();

let chunks: string[] = [];
for (let i = 0; i < splitTexts.length; i++) {
Expand Down
10 changes: 9 additions & 1 deletion packages/global/core/app/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ChatModelItemType } from '../ai/model.d';
import { AppTypeEnum } from './constants';
import { AppSchema } from './type';
import { AppSchema, AppSimpleEditFormType } from './type';

export type CreateAppParams = {
name?: string;
Expand All @@ -11,8 +12,15 @@ export type CreateAppParams = {
export interface AppUpdateParams {
name?: string;
type?: `${AppTypeEnum}`;
simpleTemplateId?: string;
avatar?: string;
intro?: string;
modules?: AppSchema['modules'];
permission?: AppSchema['permission'];
}

export type FormatForm2ModulesProps = {
formData: AppSimpleEditFormType;
chatModelMaxToken: number;
chatModelList: ChatModelItemType[];
};
10 changes: 9 additions & 1 deletion packages/global/core/app/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export enum AppTypeEnum {
basic = 'basic',
simple = 'simple',
advanced = 'advanced'
}
export const AppTypeMap = {
[AppTypeEnum.simple]: {
label: 'simple'
},
[AppTypeEnum.advanced]: {
label: 'advanced'
}
};
90 changes: 89 additions & 1 deletion packages/global/core/app/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ModuleItemType } from '../module/type';
import type { AppTTSConfigType, ModuleItemType, VariableItemType } from '../module/type.d';
import { AppTypeEnum } from './constants';
import { PermissionTypeEnum } from '../../support/permission/constant';
import type { AIChatModuleProps, DatasetModuleProps } from '../module/node/type.d';
import { VariableInputEnum } from '../module/constants';
import { SelectedDatasetType } from '../module/api';

export interface AppSchema {
_id: string;
Expand All @@ -9,6 +12,7 @@ export interface AppSchema {
tmbId: string;
name: string;
type: `${AppTypeEnum}`;
simpleTemplateId: string;
avatar: string;
intro: string;
updateTime: number;
Expand All @@ -29,3 +33,87 @@ export type AppDetailType = AppSchema & {
isOwner: boolean;
canWrite: boolean;
};

// export type AppSimpleEditFormType = {
// aiSettings: AIChatModuleProps;
// dataset: DatasetModuleProps & {
// searchEmptyText: string;
// };
// userGuide: {
// welcomeText: string;
// variables: VariableItemType[];
// questionGuide: boolean;
// tts: AppTTSConfigType;
// };
// };
// Since useform cannot infer enumeration types, all enumeration keys can only be undone manually
export type AppSimpleEditFormType = {
templateId: string;
aiSettings: {
model: string;
systemPrompt?: string | undefined;
temperature: number;
maxToken: number;
isResponseAnswerText: boolean;
quoteTemplate?: string | undefined;
quotePrompt?: string | undefined;
};
dataset: {
datasets: SelectedDatasetType;
similarity: number;
limit: number;
rerank: boolean;
searchEmptyText: string;
};
userGuide: {
welcomeText: string;
variables: {
id: string;
key: string;
label: string;
type: `${VariableInputEnum}`;
required: boolean;
maxLen: number;
enums: {
value: string;
}[];
}[];
questionGuide: boolean;
tts: {
type: 'none' | 'web' | 'model';
model?: string | undefined;
voice?: string | undefined;
speed?: number | undefined;
};
};
};

/* simple mode template*/
export type AppSimpleEditConfigTemplateType = {
id: string;
name: string;
desc: string;
systemForm: {
aiSettings?: {
model?: boolean;
systemPrompt?: boolean;
temperature?: boolean;
maxToken?: boolean;
quoteTemplate?: boolean;
quotePrompt?: boolean;
};
dataset?: {
datasets?: boolean;
similarity?: boolean;
limit?: boolean;
rerank?: boolean;
searchEmptyText?: boolean;
};
userGuide?: {
welcomeText?: boolean;
variables?: boolean;
questionGuide?: boolean;
tts?: boolean;
};
};
};
123 changes: 123 additions & 0 deletions packages/global/core/app/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import type { AppSimpleEditFormType } from '../app/type';
import { FlowNodeTypeEnum } from '../module/node/constant';
import { ModuleOutputKeyEnum, ModuleInputKeyEnum } from '../module/constants';
import type { FlowNodeInputItemType } from '../module/node/type.d';
import { getGuideModule, splitGuideModule } from '../module/utils';
import { defaultChatModels } from '../ai/model';
import { ModuleItemType } from '../module/type.d';

export const getDefaultAppForm = (templateId = 'fastgpt-universal'): AppSimpleEditFormType => {
const defaultChatModel = defaultChatModels[0];

return {
templateId,
aiSettings: {
model: defaultChatModel?.model,
systemPrompt: '',
temperature: 0,
isResponseAnswerText: true,
quotePrompt: '',
quoteTemplate: '',
maxToken: defaultChatModel ? defaultChatModel.maxResponse / 2 : 4000
},
dataset: {
datasets: [],
similarity: 0.4,
limit: 5,
searchEmptyText: '',
rerank: false
},
userGuide: {
welcomeText: '',
variables: [],
questionGuide: false,
tts: {
type: 'web'
}
}
};
};

/* format app modules to edit form */
export const appModules2Form = ({
templateId,
modules
}: {
modules: ModuleItemType[];
templateId: string;
}) => {
const defaultAppForm = getDefaultAppForm(templateId);

const findInputValueByKey = (inputs: FlowNodeInputItemType[], key: string) => {
return inputs.find((item) => item.key === key)?.value;
};

modules.forEach((module) => {
if (module.flowType === FlowNodeTypeEnum.chatNode) {
defaultAppForm.aiSettings.model = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.aiModel
);
defaultAppForm.aiSettings.systemPrompt = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.aiSystemPrompt
);
defaultAppForm.aiSettings.temperature = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.aiChatTemperature
);
defaultAppForm.aiSettings.maxToken = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.aiChatMaxToken
);
defaultAppForm.aiSettings.quoteTemplate = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.aiChatQuoteTemplate
);
defaultAppForm.aiSettings.quotePrompt = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.aiChatQuoteTemplate
);
} else if (module.flowType === FlowNodeTypeEnum.datasetSearchNode) {
defaultAppForm.dataset.datasets = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.datasetSelectList
);
defaultAppForm.dataset.similarity = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.datasetSimilarity
);
defaultAppForm.dataset.limit = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.datasetLimit
);
defaultAppForm.dataset.rerank = findInputValueByKey(
module.inputs,
ModuleInputKeyEnum.datasetStartReRank
);

// empty text
const emptyOutputs =
module.outputs.find((item) => item.key === ModuleOutputKeyEnum.datasetIsEmpty)?.targets ||
[];
const emptyOutput = emptyOutputs[0];
if (emptyOutput) {
const target = modules.find((item) => item.moduleId === emptyOutput.moduleId);
defaultAppForm.dataset.searchEmptyText =
target?.inputs?.find((item) => item.key === ModuleInputKeyEnum.answerText)?.value || '';
}
} else if (module.flowType === FlowNodeTypeEnum.userGuide) {
const { welcomeText, variableModules, questionGuide, ttsConfig } = splitGuideModule(
getGuideModule(modules)
);
defaultAppForm.userGuide = {
welcomeText: welcomeText,
variables: variableModules,
questionGuide: questionGuide,
tts: ttsConfig
};
}
});

return defaultAppForm;
};
1 change: 0 additions & 1 deletion packages/global/core/chat/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ export type InitChatResponse = {
export type ChatHistoryItemResType = moduleDispatchResType & {
moduleType: `${FlowNodeTypeEnum}`;
moduleName: string;
moduleLogo?: string;
};
6 changes: 0 additions & 6 deletions packages/global/core/chat/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ export enum ChatRoleEnum {
Tool = 'Tool'
}

export enum TaskResponseKeyEnum {
'answerText' = 'answerText', // answer module text key
'responseData' = 'responseData',
'history' = 'history'
}

export const ChatRoleMap = {
[ChatRoleEnum.System]: {
name: '系统提示词'
Expand Down
16 changes: 9 additions & 7 deletions packages/global/core/chat/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ClassifyQuestionAgentItemType } from '../module/type';
import { SearchDataResponseItemType } from '../dataset/type';
import { ChatRoleEnum, ChatSourceEnum, TaskResponseKeyEnum } from './constants';
import { ChatRoleEnum, ChatSourceEnum } from './constants';
import { FlowNodeTypeEnum } from '../module/node/constant';
import { AppSchema } from 'core/app/type';
import { ModuleOutputKeyEnum } from '../module/constants';
import { AppSchema } from '../app/type';

export type ChatSchema = {
_id: string;
Expand Down Expand Up @@ -38,7 +39,7 @@ export type ChatItemSchema = {
value: string;
userFeedback?: string;
adminFeedback?: AdminFbkType;
[TaskResponseKeyEnum.responseData]?: ChatHistoryItemResType[];
[ModuleOutputKeyEnum.responseData]?: ChatHistoryItemResType[];
};

export type AdminFbkType = {
Expand All @@ -55,14 +56,14 @@ export type ChatItemType = {
value: any;
userFeedback?: string;
adminFeedback?: ChatItemSchema['feedback'];
[TaskResponseKeyEnum.responseData]?: ChatItemSchema[TaskResponseKeyEnum.responseData];
[ModuleOutputKeyEnum.responseData]?: ChatHistoryItemResType[];
};

export type ChatSiteItemType = {
export type ChatSiteItemType = ChatItemType & {
status: 'loading' | 'running' | 'finish';
moduleName?: string;
ttsBuffer?: Uint8Array;
} & ChatItemType;
};

export type HistoryItemType = {
chatId: string;
Expand All @@ -77,13 +78,14 @@ export type ChatHistoryItemType = HistoryItemType & {

// response data
export type moduleDispatchResType = {
moduleLogo?: string;
price: number;
runningTime?: number;
tokens?: number;
model?: string;
query?: string;

// chat
question?: string;
temperature?: number;
maxToken?: number;
quoteList?: SearchDataResponseItemType[];
Expand Down
Loading

0 comments on commit 9cb4280

Please sign in to comment.