mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-01 11:58:38 +00:00
feat: user openai account
This commit is contained in:
@@ -42,13 +42,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
await connectToDatabase();
|
||||
|
||||
/* user auth */
|
||||
const { userId } = await authUser({ req });
|
||||
const { userId, user } = await authUser({ req, authBalance: true });
|
||||
|
||||
/* start process */
|
||||
const { responseData } = await dispatchModules({
|
||||
res,
|
||||
modules: modules,
|
||||
variables,
|
||||
user,
|
||||
params: {
|
||||
history: gptMessage2ChatType(history),
|
||||
userChatInput: prompt
|
||||
|
@@ -24,6 +24,8 @@ import { AppModuleItemType, RunningModuleItemType } from '@/types/app';
|
||||
import { pushTaskBill } from '@/service/events/pushBill';
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
import { ChatHistoryItemResType } from '@/types/chat';
|
||||
import { UserModelSchema } from '@/types/mongoSchema';
|
||||
import { getAIChatApi } from '@/service/ai/openai';
|
||||
|
||||
export type MessageItemType = ChatCompletionRequestMessage & { _id?: string };
|
||||
type FastGptWebChatProps = {
|
||||
@@ -69,6 +71,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
|
||||
/* user auth */
|
||||
const {
|
||||
user,
|
||||
userId,
|
||||
appId: authAppid,
|
||||
authType
|
||||
@@ -76,7 +79,14 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
? authShareChat({
|
||||
shareId
|
||||
})
|
||||
: authUser({ req }));
|
||||
: authUser({ req, authBalance: true }));
|
||||
|
||||
if (!user) {
|
||||
throw new Error('Account is error');
|
||||
}
|
||||
if (authType !== 'token') {
|
||||
user.openaiAccount = undefined;
|
||||
}
|
||||
|
||||
appId = appId ? appId : authAppid;
|
||||
if (!appId) {
|
||||
@@ -108,6 +118,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
const { responseData, answerText } = await dispatchModules({
|
||||
res,
|
||||
modules: app.modules,
|
||||
user,
|
||||
variables,
|
||||
params: {
|
||||
history: prompts,
|
||||
@@ -182,7 +193,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
responseData,
|
||||
id: chatId || '',
|
||||
model: '',
|
||||
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
||||
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 1 },
|
||||
choices: [
|
||||
{
|
||||
message: [{ role: 'assistant', content: answerText }],
|
||||
@@ -217,12 +228,14 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
export async function dispatchModules({
|
||||
res,
|
||||
modules,
|
||||
user,
|
||||
params = {},
|
||||
variables = {},
|
||||
stream = false
|
||||
}: {
|
||||
res: NextApiResponse;
|
||||
modules: AppModuleItemType[];
|
||||
user?: UserModelSchema;
|
||||
params?: Record<string, any>;
|
||||
variables?: Record<string, any>;
|
||||
stream?: boolean;
|
||||
@@ -304,6 +317,7 @@ export async function dispatchModules({
|
||||
const props: Record<string, any> = {
|
||||
res,
|
||||
stream,
|
||||
userOpenaiAccount: user?.openaiAccount,
|
||||
...params
|
||||
};
|
||||
|
||||
|
@@ -5,12 +5,12 @@ import { User } from '@/service/models/user';
|
||||
import { connectToDatabase } from '@/service/mongo';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
import { UserUpdateParams } from '@/types/user';
|
||||
import { getAIChatApi, openaiBaseUrl } from '@/service/ai/openai';
|
||||
import { axiosConfig, getAIChatApi, openaiBaseUrl } from '@/service/ai/openai';
|
||||
|
||||
/* 更新一些基本信息 */
|
||||
/* update user info */
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
let { avatar, openaiAccount } = req.body as UserUpdateParams;
|
||||
const { avatar, openaiAccount } = req.body as UserUpdateParams;
|
||||
|
||||
const { userId } = await authUser({ req, authToken: true });
|
||||
|
||||
@@ -19,17 +19,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
||||
// auth key
|
||||
if (openaiAccount?.key) {
|
||||
console.log('auth user openai key', openaiAccount?.key);
|
||||
const baseUrl = openaiAccount?.baseUrl || openaiBaseUrl;
|
||||
openaiAccount.baseUrl = baseUrl;
|
||||
|
||||
const chatAPI = getAIChatApi({
|
||||
base: openaiAccount?.baseUrl || openaiBaseUrl,
|
||||
apikey: openaiAccount?.key
|
||||
});
|
||||
const chatAPI = getAIChatApi(openaiAccount);
|
||||
|
||||
const response = await chatAPI.createChatCompletion({
|
||||
model: 'gpt-3.5-turbo',
|
||||
max_tokens: 1,
|
||||
messages: [{ role: 'user', content: 'hi' }]
|
||||
});
|
||||
const response = await chatAPI.createChatCompletion(
|
||||
{
|
||||
model: 'gpt-3.5-turbo',
|
||||
max_tokens: 1,
|
||||
messages: [{ role: 'user', content: 'hi' }]
|
||||
},
|
||||
{
|
||||
...axiosConfig(openaiAccount)
|
||||
}
|
||||
);
|
||||
if (!response?.data?.choices?.[0]?.message?.content) {
|
||||
throw new Error(JSON.stringify(response?.data));
|
||||
}
|
||||
@@ -42,7 +46,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
||||
},
|
||||
{
|
||||
...(avatar && { avatar }),
|
||||
...(openaiAccount && { openaiAccount })
|
||||
openaiAccount: openaiAccount?.key ? openaiAccount : null
|
||||
}
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user