doc gpt V0.2
This commit is contained in:
45
src/service/utils/chat.ts
Normal file
45
src/service/utils/chat.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Configuration, OpenAIApi } from 'openai';
|
||||
import { Chat } from '../mongo';
|
||||
|
||||
export const getOpenAIApi = (apiKey: string) => {
|
||||
const configuration = new Configuration({
|
||||
apiKey
|
||||
});
|
||||
|
||||
return new OpenAIApi(configuration, undefined);
|
||||
};
|
||||
|
||||
export const authChat = async (chatId: string) => {
|
||||
// 获取 chat 数据
|
||||
const chat = await Chat.findById(chatId)
|
||||
.populate({
|
||||
path: 'modelId',
|
||||
options: {
|
||||
strictPopulate: false
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'userId',
|
||||
options: {
|
||||
strictPopulate: false
|
||||
}
|
||||
});
|
||||
|
||||
if (!chat || !chat.modelId || !chat.userId) {
|
||||
return Promise.reject('聊天已过期');
|
||||
}
|
||||
|
||||
// 获取 user 的 apiKey
|
||||
const user = chat.userId;
|
||||
|
||||
const userApiKey = user.accounts?.find((item: any) => item.type === 'openai')?.value;
|
||||
|
||||
if (!userApiKey) {
|
||||
return Promise.reject('该用户缺少ApiKey, 无法请求');
|
||||
}
|
||||
|
||||
return {
|
||||
userApiKey,
|
||||
chat
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user