feat: model share market

This commit is contained in:
archer
2023-04-27 23:41:42 +08:00
parent 46eb96c72e
commit 3b8e5d2738
34 changed files with 574 additions and 199 deletions

View File

@@ -14,7 +14,7 @@ const ModelSchema = new Schema({
},
avatar: {
type: String,
default: '/imgs/modelAvatar.png'
default: '/icon/logo.png'
},
systemPrompt: {
// 系统提示词
@@ -43,6 +43,26 @@ const ModelSchema = new Schema({
default: ModelVectorSearchModeEnum.hightSimilarity
}
},
share: {
isShare: {
type: Boolean,
default: false
},
isShareDetail: {
// share model detail info. false: just show name and intro
type: Boolean,
default: false
},
intro: {
type: String,
default: '',
maxlength: 150
},
collection: {
type: Number,
default: 0
}
},
service: {
chatModel: {
// 聊天时使用的模型

View File

@@ -16,16 +16,34 @@ export const getOpenAIApi = (apiKey: string) => {
};
// 模型使用权校验
export const authModel = async (modelId: string, userId: string) => {
export const authModel = async ({
modelId,
userId,
authUser = true,
authOwner = true
}: {
modelId: string;
userId: string;
authUser?: boolean;
authOwner?: boolean;
}) => {
// 获取 model 数据
const model = await Model.findById<ModelSchema>(modelId);
if (!model) {
return Promise.reject('模型不存在');
}
// 凭证校验
if (userId !== String(model.userId)) {
return Promise.reject('无权使用该模型');
// 使用权限校验
if ((authOwner || (authUser && !model.share.isShare)) && userId !== String(model.userId)) {
return Promise.reject('无权操作该模型');
}
// detail 内容去除
if (!model.share.isShareDetail) {
model.systemPrompt = '';
model.temperature = 0;
}
return { model };
};
@@ -42,7 +60,7 @@ export const authChat = async ({
const userId = await authToken(authorization);
// 获取 model 数据
const { model } = await authModel(modelId, userId);
const { model } = await authModel({ modelId, userId, authOwner: false });
// 聊天内容
let content: ChatItemType[] = [];