mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-24 05:23:57 +00:00
v4.6 -1 (#459)
This commit is contained in:
26
packages/service/core/ai/audio/speech.ts
Normal file
26
packages/service/core/ai/audio/speech.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Text2SpeechProps } from '@fastgpt/global/core/ai/speech/api';
|
||||
import { getAIApi } from '../config';
|
||||
import { defaultAudioSpeechModels } from '../../../../global/core/ai/model';
|
||||
import { Text2SpeechVoiceEnum } from '@fastgpt/global/core/ai/speech/constant';
|
||||
|
||||
export async function text2Speech({
|
||||
model = defaultAudioSpeechModels[0].model,
|
||||
voice = Text2SpeechVoiceEnum.alloy,
|
||||
input,
|
||||
speed = 1
|
||||
}: Text2SpeechProps) {
|
||||
const ai = getAIApi();
|
||||
const mp3 = await ai.audio.speech.create({
|
||||
model,
|
||||
voice,
|
||||
input,
|
||||
response_format: 'mp3',
|
||||
speed
|
||||
});
|
||||
const buffer = Buffer.from(await mp3.arrayBuffer());
|
||||
return {
|
||||
model,
|
||||
voice,
|
||||
tts: buffer
|
||||
};
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
import type { UserModelSchema } from '@fastgpt/global/support/user/type';
|
||||
import OpenAI from 'openai';
|
||||
import OpenAI from '@fastgpt/global/core/ai';
|
||||
|
||||
export const openaiBaseUrl = process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1';
|
||||
export const baseUrl = process.env.ONEAPI_URL || openaiBaseUrl;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import type { ChatCompletionRequestMessage } from '@fastgpt/global/core/ai/type.d';
|
||||
import type { ChatMessageItemType } from '@fastgpt/global/core/ai/type.d';
|
||||
import { getAIApi } from '../config';
|
||||
|
||||
export const Prompt_QuestionGuide = `我不太清楚问你什么问题,请帮我生成 3 个问题,引导我继续提问。问题的长度应小于20个字符,按 JSON 格式返回: ["问题1", "问题2", "问题3"]`;
|
||||
@@ -7,7 +7,7 @@ export async function createQuestionGuide({
|
||||
messages,
|
||||
model
|
||||
}: {
|
||||
messages: ChatCompletionRequestMessage[];
|
||||
messages: ChatMessageItemType[];
|
||||
model: string;
|
||||
}) {
|
||||
const ai = getAIApi(undefined, 48000);
|
||||
|
70
packages/service/core/app/schema.ts
Normal file
70
packages/service/core/app/schema.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { connectionMongo, type Model } from '../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import type { AppSchema as AppType } from '@fastgpt/global/core/app/type.d';
|
||||
import { PermissionTypeEnum, PermissionTypeMap } from '@fastgpt/global/support/permission/constant';
|
||||
import {
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
|
||||
export const appCollectionName = 'apps';
|
||||
|
||||
const AppSchema = new Schema({
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user'
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamMemberCollectionName,
|
||||
required: true
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
default: 'advanced',
|
||||
enum: ['basic', 'advanced']
|
||||
},
|
||||
avatar: {
|
||||
type: String,
|
||||
default: '/icon/logo.svg'
|
||||
},
|
||||
intro: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
updateTime: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
modules: {
|
||||
type: Array,
|
||||
default: []
|
||||
},
|
||||
inited: {
|
||||
type: Boolean
|
||||
},
|
||||
permission: {
|
||||
type: String,
|
||||
enum: Object.keys(PermissionTypeMap),
|
||||
default: PermissionTypeEnum.private
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
AppSchema.index({ updateTime: -1 });
|
||||
AppSchema.index({ 'share.collection': -1 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoApp: Model<AppType> =
|
||||
models[appCollectionName] || model(appCollectionName, AppSchema);
|
88
packages/service/core/chat/chatItemSchema.ts
Normal file
88
packages/service/core/chat/chatItemSchema.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { connectionMongo, type Model } from '../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { ChatItemSchema as ChatItemType } from '@fastgpt/global/core/chat/type';
|
||||
import { ChatRoleMap, TaskResponseKeyEnum } from '@fastgpt/global/core/chat/constants';
|
||||
import { customAlphabet } from 'nanoid';
|
||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 24);
|
||||
import {
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
import { appCollectionName } from '../app/schema';
|
||||
import { userCollectionName } from '../../support/user/schema';
|
||||
|
||||
const ChatItemSchema = new Schema({
|
||||
dataId: {
|
||||
type: String,
|
||||
require: true,
|
||||
default: () => nanoid()
|
||||
},
|
||||
appId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: appCollectionName,
|
||||
required: true
|
||||
},
|
||||
chatId: {
|
||||
type: String,
|
||||
require: true
|
||||
},
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: userCollectionName
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamMemberCollectionName,
|
||||
required: true
|
||||
},
|
||||
time: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
obj: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: Object.keys(ChatRoleMap)
|
||||
},
|
||||
value: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
userFeedback: {
|
||||
type: String
|
||||
},
|
||||
adminFeedback: {
|
||||
type: {
|
||||
datasetId: String,
|
||||
collectionId: String,
|
||||
dataId: String,
|
||||
q: String,
|
||||
a: String
|
||||
}
|
||||
},
|
||||
[TaskResponseKeyEnum.responseData]: {
|
||||
type: Array,
|
||||
default: []
|
||||
},
|
||||
tts: {
|
||||
type: Buffer
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
ChatItemSchema.index({ time: -1 });
|
||||
ChatItemSchema.index({ userId: 1 });
|
||||
ChatItemSchema.index({ appId: 1 });
|
||||
ChatItemSchema.index({ chatId: 1 });
|
||||
ChatItemSchema.index({ userFeedback: 1 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoChatItem: Model<ChatItemType> =
|
||||
models['chatItem'] || model('chatItem', ChatItemSchema);
|
103
packages/service/core/chat/chatSchema.ts
Normal file
103
packages/service/core/chat/chatSchema.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { connectionMongo, type Model } from '../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { ChatSchema as ChatType } from '@fastgpt/global/core/chat/type.d';
|
||||
import {
|
||||
ChatRoleMap,
|
||||
ChatSourceMap,
|
||||
TaskResponseKeyEnum
|
||||
} from '@fastgpt/global/core/chat/constants';
|
||||
import {
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
import { appCollectionName } from '../app/schema';
|
||||
|
||||
export const chatCollectionName = 'chat';
|
||||
|
||||
const ChatSchema = new Schema({
|
||||
chatId: {
|
||||
type: String,
|
||||
require: true
|
||||
},
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user'
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamMemberCollectionName,
|
||||
required: true
|
||||
},
|
||||
appId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: appCollectionName,
|
||||
required: true
|
||||
},
|
||||
updateTime: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
title: {
|
||||
type: String,
|
||||
default: '历史记录'
|
||||
},
|
||||
customTitle: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
top: {
|
||||
type: Boolean
|
||||
},
|
||||
variables: {
|
||||
type: Object,
|
||||
default: {}
|
||||
},
|
||||
source: {
|
||||
type: String,
|
||||
enum: Object.keys(ChatSourceMap),
|
||||
required: true
|
||||
},
|
||||
shareId: {
|
||||
type: String
|
||||
},
|
||||
isInit: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
content: {
|
||||
type: [
|
||||
{
|
||||
obj: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: Object.keys(ChatRoleMap)
|
||||
},
|
||||
value: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
[TaskResponseKeyEnum.responseData]: {
|
||||
type: Array,
|
||||
default: []
|
||||
}
|
||||
}
|
||||
],
|
||||
default: []
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
ChatSchema.index({ userId: 1 });
|
||||
ChatSchema.index({ updateTime: -1 });
|
||||
ChatSchema.index({ appId: 1 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoChat: Model<ChatType> =
|
||||
models[chatCollectionName] || model(chatCollectionName, ChatSchema);
|
@@ -1,26 +0,0 @@
|
||||
import { ERROR_ENUM } from '@fastgpt/global/common/error/errorCode';
|
||||
import { MongoDatasetCollection } from './collection/schema';
|
||||
import { DatasetSchemaType } from '@fastgpt/global/core/dataset/type';
|
||||
|
||||
export async function authCollection({
|
||||
collectionId,
|
||||
userId
|
||||
}: {
|
||||
collectionId: string;
|
||||
userId: string;
|
||||
}) {
|
||||
const collection = await MongoDatasetCollection.findOne({
|
||||
_id: collectionId,
|
||||
userId
|
||||
})
|
||||
.populate('datasetId')
|
||||
.lean();
|
||||
|
||||
if (collection) {
|
||||
return {
|
||||
...collection,
|
||||
dataset: collection.datasetId as unknown as DatasetSchemaType
|
||||
};
|
||||
}
|
||||
return Promise.reject(ERROR_ENUM.unAuthDataset);
|
||||
}
|
@@ -3,6 +3,10 @@ const { Schema, model, models } = connectionMongo;
|
||||
import { DatasetCollectionSchemaType } from '@fastgpt/global/core/dataset/type.d';
|
||||
import { DatasetCollectionTypeMap } from '@fastgpt/global/core/dataset/constant';
|
||||
import { DatasetCollectionName } from '../schema';
|
||||
import {
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
|
||||
export const DatasetColCollectionName = 'dataset.collections';
|
||||
|
||||
@@ -13,8 +17,18 @@ const DatasetCollectionSchema = new Schema({
|
||||
default: null
|
||||
},
|
||||
userId: {
|
||||
// abandoned
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
ref: 'user'
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamMemberCollectionName,
|
||||
required: true
|
||||
},
|
||||
datasetId: {
|
||||
|
@@ -31,18 +31,16 @@ export async function findCollectionAndChild(id: string, fields = '_id parentId
|
||||
}
|
||||
|
||||
export async function getDatasetCollectionPaths({
|
||||
parentId = '',
|
||||
userId
|
||||
parentId = ''
|
||||
}: {
|
||||
parentId?: string;
|
||||
userId: string;
|
||||
}): Promise<ParentTreePathItemType[]> {
|
||||
async function find(parentId?: string): Promise<ParentTreePathItemType[]> {
|
||||
if (!parentId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const parent = await MongoDatasetCollection.findOne({ _id: parentId, userId }, 'name parentId');
|
||||
const parent = await MongoDatasetCollection.findOne({ _id: parentId }, 'name parentId');
|
||||
|
||||
if (!parent) return [];
|
||||
|
||||
|
12
packages/service/core/dataset/controller.ts
Normal file
12
packages/service/core/dataset/controller.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { CollectionWithDatasetType } from '@fastgpt/global/core/dataset/type';
|
||||
import { MongoDatasetCollection } from './collection/schema';
|
||||
|
||||
export async function getCollectionWithDataset(collectionId: string) {
|
||||
const data = (
|
||||
await MongoDatasetCollection.findById(collectionId).populate('datasetId')
|
||||
)?.toJSON() as CollectionWithDatasetType;
|
||||
if (!data) {
|
||||
return Promise.reject('Collection is not exist');
|
||||
}
|
||||
return data;
|
||||
}
|
9
packages/service/core/dataset/file/controller.ts
Normal file
9
packages/service/core/dataset/file/controller.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
||||
import { getGFSCollection } from '../../../common/file/gridfs/controller';
|
||||
|
||||
export async function delDatasetFiles({ datasetId }: { datasetId: string }) {
|
||||
const db = getGFSCollection(BucketNameEnum.dataset);
|
||||
await db.deleteMany({
|
||||
'metadata.datasetId': String(datasetId)
|
||||
});
|
||||
}
|
@@ -2,6 +2,11 @@ import { connectionMongo, type Model } from '../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { DatasetSchemaType } from '@fastgpt/global/core/dataset/type.d';
|
||||
import { DatasetTypeMap } from '@fastgpt/global/core/dataset/constant';
|
||||
import {
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
import { PermissionTypeEnum, PermissionTypeMap } from '@fastgpt/global/support/permission/constant';
|
||||
|
||||
export const DatasetCollectionName = 'datasets';
|
||||
|
||||
@@ -12,8 +17,18 @@ const DatasetSchema = new Schema({
|
||||
default: null
|
||||
},
|
||||
userId: {
|
||||
//abandon
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
ref: 'user'
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamMemberCollectionName,
|
||||
required: true
|
||||
},
|
||||
updateTime: {
|
||||
@@ -41,7 +56,16 @@ const DatasetSchema = new Schema({
|
||||
},
|
||||
tags: {
|
||||
type: [String],
|
||||
default: []
|
||||
default: [],
|
||||
set(val: string | string[]) {
|
||||
if (Array.isArray(val)) return val;
|
||||
return val.split(' ').filter((item) => item);
|
||||
}
|
||||
},
|
||||
permission: {
|
||||
type: String,
|
||||
enum: Object.keys(PermissionTypeMap),
|
||||
default: PermissionTypeEnum.private
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -5,13 +5,27 @@ import { DatasetTrainingSchemaType } from '@fastgpt/global/core/dataset/type';
|
||||
import { TrainingTypeMap } from '@fastgpt/global/core/dataset/constant';
|
||||
import { DatasetColCollectionName } from '../collection/schema';
|
||||
import { DatasetCollectionName } from '../schema';
|
||||
import {
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
|
||||
export const DatasetTrainingCollectionName = 'dataset.trainings';
|
||||
|
||||
const TrainingDataSchema = new Schema({
|
||||
userId: {
|
||||
// abandon
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
ref: 'user'
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamMemberCollectionName,
|
||||
required: true
|
||||
},
|
||||
datasetId: {
|
||||
|
@@ -1,38 +1,15 @@
|
||||
import { CreateOnePluginParams, UpdatePluginParams } from '@fastgpt/global/core/plugin/controller';
|
||||
import { MongoPlugin } from './schema';
|
||||
import { FlowModuleTemplateType } from '@fastgpt/global/core/module/type';
|
||||
import { FlowNodeTypeEnum } from '@fastgpt/global/core/module/node/constant';
|
||||
import { formatPluginIOModules } from '@fastgpt/global/core/module/utils';
|
||||
|
||||
export async function createOnePlugin(data: CreateOnePluginParams & { userId: string }) {
|
||||
const { _id } = await MongoPlugin.create(data);
|
||||
return _id;
|
||||
}
|
||||
|
||||
export async function updateOnePlugin({
|
||||
id,
|
||||
userId,
|
||||
...data
|
||||
}: UpdatePluginParams & { userId: string }) {
|
||||
await MongoPlugin.findOneAndUpdate({ _id: id, userId }, data);
|
||||
}
|
||||
|
||||
export async function deleteOnePlugin({ id, userId }: { id: string; userId: string }) {
|
||||
await MongoPlugin.findOneAndDelete({ _id: id, userId });
|
||||
}
|
||||
export async function getUserPlugins({ userId }: { userId: string }) {
|
||||
return MongoPlugin.find({ userId }, 'name avatar intro');
|
||||
}
|
||||
export async function getOnePluginDetail({ id, userId }: { userId: string; id: string }) {
|
||||
return MongoPlugin.findOne({ _id: id, userId });
|
||||
}
|
||||
/* plugin templates */
|
||||
export async function getUserPlugins2Templates({
|
||||
userId
|
||||
teamId
|
||||
}: {
|
||||
userId: string;
|
||||
teamId: string;
|
||||
}): Promise<FlowModuleTemplateType[]> {
|
||||
const plugins = await MongoPlugin.find({ userId }).lean();
|
||||
const plugins = await MongoPlugin.find({ teamId }).lean();
|
||||
|
||||
return plugins.map((plugin) => ({
|
||||
id: String(plugin._id),
|
||||
@@ -47,8 +24,8 @@ export async function getUserPlugins2Templates({
|
||||
}));
|
||||
}
|
||||
/* one plugin 2 module detail */
|
||||
export async function getPluginModuleDetail({ id, userId }: { userId: string; id: string }) {
|
||||
const plugin = await getOnePluginDetail({ id, userId });
|
||||
export async function getPluginModuleDetail({ id }: { id: string }) {
|
||||
const plugin = await MongoPlugin.findById(id);
|
||||
if (!plugin) return Promise.reject('plugin not found');
|
||||
return {
|
||||
id: String(plugin._id),
|
||||
|
@@ -1,13 +1,26 @@
|
||||
import { connectionMongo, type Model } from '../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import type { PluginItemSchema } from '@fastgpt/global/core/plugin/type.d';
|
||||
import {
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
|
||||
export const ModuleCollectionName = 'plugins';
|
||||
|
||||
const PluginSchema = new Schema({
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
ref: 'user'
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamMemberCollectionName,
|
||||
required: true
|
||||
},
|
||||
name: {
|
||||
|
Reference in New Issue
Block a user