This commit is contained in:
Archer
2023-10-07 18:02:20 +08:00
committed by GitHub
parent c65a36d3ab
commit 98ce5103a0
56 changed files with 868 additions and 282 deletions

View File

@@ -3,71 +3,39 @@ import { jsonRes } from '@/service/response';
import { connectToDatabase } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { CreateQuestionGuideProps } from '@/api/core/ai/agent/type';
import { getAIChatApi } from '@fastgpt/core/aiApi/config';
import { Prompt_QuestionGuide } from '@/prompts/core/agent';
import { pushQuestionGuideBill } from '@/service/common/bill/push';
import { defaultQGModel } from '@/pages/api/system/getInitData';
import { createQuestionGuide } from '@fastgpt/core/ai/functions/createQuestionGuide';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
await connectToDatabase();
const { messages } = req.body as CreateQuestionGuideProps;
const { user } = await authUser({ req, authToken: true, authApiKey: true, authBalance: true });
const { user } = await authUser({
req,
authOutLink: true,
authToken: true,
authApiKey: true,
authBalance: true
});
if (!user) {
throw new Error('user not found');
}
const qgModel = global.qgModel || defaultQGModel;
const chatAPI = getAIChatApi(user.openaiAccount);
const { data } = await chatAPI.createChatCompletion({
model: qgModel.model,
temperature: 0,
max_tokens: 200,
messages: [
...messages,
{
role: 'user',
content: Prompt_QuestionGuide
}
],
stream: false
const { result, tokens } = await createQuestionGuide({
messages,
model: (global.qgModel || defaultQGModel).model
});
const answer = data.choices?.[0].message?.content || '';
const totalTokens = data.usage?.total_tokens || 0;
jsonRes(res, {
data: result
});
const start = answer.indexOf('[');
const end = answer.lastIndexOf(']');
if (start === -1 || end === -1) {
return jsonRes(res, {
data: []
});
}
const jsonStr = answer
.substring(start, end + 1)
.replace(/(\\n|\\)/g, '')
.replace(/ /g, '');
try {
jsonRes(res, {
data: JSON.parse(jsonStr)
});
pushQuestionGuideBill({
tokens: totalTokens,
userId: user._id
});
return;
} catch (error) {
return jsonRes(res, {
data: []
});
}
pushQuestionGuideBill({
tokens: tokens,
userId: user._id
});
} catch (err) {
jsonRes(res, {
code: 500,

View File

@@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { authBalanceByUid, authUser } from '@/service/utils/auth';
import { withNextCors } from '@/service/utils/tools';
import { getAIChatApi, axiosConfig } from '@fastgpt/core/aiApi/config';
import { getAIChatApi, axiosConfig } from '@fastgpt/core/ai/config';
import { pushGenerateVectorBill } from '@/service/common/bill/push';
type Props = {

View File

@@ -5,7 +5,7 @@ import { User } from '@/service/models/user';
import { connectToDatabase } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { UserUpdateParams } from '@/types/user';
import { axiosConfig, getAIChatApi, openaiBaseUrl } from '@fastgpt/core/aiApi/config';
import { axiosConfig, getAIChatApi, openaiBaseUrl } from '@fastgpt/core/ai/config';
/* update user info */
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {

View File

@@ -12,9 +12,10 @@ import {
dispatchAnswer,
dispatchClassifyQuestion,
dispatchContentExtract,
dispatchHttpRequest
dispatchHttpRequest,
dispatchAppRequest
} from '@/service/moduleDispatch';
import type { CreateChatCompletionRequest } from '@fastgpt/core/aiApi/type';
import type { CreateChatCompletionRequest } from '@fastgpt/core/ai/type';
import type { MessageItemType } from '@/types/core/chat/type';
import { gptMessage2ChatType, textAdaptGptResponse } from '@/utils/adapt';
import { getChatHistory } from './getHistory';
@@ -325,14 +326,19 @@ export async function dispatchModules({
responseData
}: {
answerText?: string;
responseData?: ChatHistoryItemResType;
responseData?: ChatHistoryItemResType | ChatHistoryItemResType[];
}) {
const time = Date.now();
responseData &&
chatResponse.push({
...responseData,
runningTime: +((time - runningTime) / 1000).toFixed(2)
});
if (responseData) {
if (Array.isArray(responseData)) {
chatResponse = chatResponse.concat(responseData);
} else {
chatResponse.push({
...responseData,
runningTime: +((time - runningTime) / 1000).toFixed(2)
});
}
}
runningTime = time;
chatAnswerText += answerText;
}
@@ -411,7 +417,7 @@ export async function dispatchModules({
variables,
moduleName: module.name,
outputs: module.outputs,
userOpenaiAccount: user?.openaiAccount,
user,
inputs: params
};
@@ -424,7 +430,8 @@ export async function dispatchModules({
[FlowModuleTypeEnum.kbSearchNode]: dispatchKBSearch,
[FlowModuleTypeEnum.classifyQuestion]: dispatchClassifyQuestion,
[FlowModuleTypeEnum.contentExtract]: dispatchContentExtract,
[FlowModuleTypeEnum.httpRequest]: dispatchHttpRequest
[FlowModuleTypeEnum.httpRequest]: dispatchHttpRequest,
[FlowModuleTypeEnum.app]: dispatchAppRequest
};
if (callbackMap[module.flowType]) {
return callbackMap[module.flowType](props);