mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-30 02:12:38 +00:00
perf: response key
This commit is contained in:
@@ -14,8 +14,9 @@ export enum SystemInputEnum {
|
|||||||
'history' = 'history',
|
'history' = 'history',
|
||||||
'userChatInput' = 'userChatInput'
|
'userChatInput' = 'userChatInput'
|
||||||
}
|
}
|
||||||
export enum SpecificInputEnum {
|
export enum TaskResponseKeyEnum {
|
||||||
'answerText' = 'answerText' // answer module text key
|
'answerText' = 'answerText', // answer module text key
|
||||||
|
'responseData' = 'responseData'
|
||||||
}
|
}
|
||||||
export enum VariableInputEnum {
|
export enum VariableInputEnum {
|
||||||
input = 'input',
|
input = 'input',
|
||||||
|
@@ -24,6 +24,30 @@ export const ChatRoleMap = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum ChatSourceEnum {
|
||||||
|
'test' = 'test',
|
||||||
|
online = 'online',
|
||||||
|
share = 'share',
|
||||||
|
api = 'api'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ChatSourceMap = {
|
||||||
|
[ChatSourceEnum.test]: {
|
||||||
|
name: '调试测试'
|
||||||
|
},
|
||||||
|
[ChatSourceEnum.online]: {
|
||||||
|
name: '在线使用'
|
||||||
|
},
|
||||||
|
[ChatSourceEnum.share]: {
|
||||||
|
name: '链接分享'
|
||||||
|
},
|
||||||
|
[ChatSourceEnum.api]: {
|
||||||
|
name: 'API调用'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const responseDataKey = 'responseData';
|
||||||
|
|
||||||
export const rawSearchKey = 'rawSearch';
|
export const rawSearchKey = 'rawSearch';
|
||||||
export const quoteLenKey = 'quoteLen';
|
export const quoteLenKey = 'quoteLen';
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { AppModuleItemTypeEnum, SystemInputEnum, SpecificInputEnum } from '../app';
|
import { AppModuleItemTypeEnum, SystemInputEnum, TaskResponseKeyEnum } from '../app';
|
||||||
import { FlowModuleTypeEnum, FlowInputItemTypeEnum, FlowOutputItemTypeEnum } from './index';
|
import { FlowModuleTypeEnum, FlowInputItemTypeEnum, FlowOutputItemTypeEnum } from './index';
|
||||||
import type { AppItemType, AppModuleTemplateItemType } from '@/types/app';
|
import type { AppItemType, AppModuleTemplateItemType } from '@/types/app';
|
||||||
import { chatModelList } from '@/store/static';
|
import { chatModelList } from '@/store/static';
|
||||||
@@ -177,7 +177,7 @@ export const ChatModule: AppModuleTemplateItemType = {
|
|||||||
],
|
],
|
||||||
outputs: [
|
outputs: [
|
||||||
{
|
{
|
||||||
key: SpecificInputEnum.answerText,
|
key: TaskResponseKeyEnum.answerText,
|
||||||
label: '模型回复',
|
label: '模型回复',
|
||||||
description: '直接响应,无需配置',
|
description: '直接响应,无需配置',
|
||||||
type: FlowOutputItemTypeEnum.hidden,
|
type: FlowOutputItemTypeEnum.hidden,
|
||||||
@@ -264,7 +264,7 @@ export const AnswerModule: AppModuleTemplateItemType = {
|
|||||||
inputs: [
|
inputs: [
|
||||||
Input_Template_TFSwitch,
|
Input_Template_TFSwitch,
|
||||||
{
|
{
|
||||||
key: SpecificInputEnum.answerText,
|
key: TaskResponseKeyEnum.answerText,
|
||||||
value: '',
|
value: '',
|
||||||
type: FlowInputItemTypeEnum.textarea,
|
type: FlowInputItemTypeEnum.textarea,
|
||||||
label: '回复的内容'
|
label: '回复的内容'
|
||||||
|
@@ -10,7 +10,7 @@ import type { ChatItemType } from '@/types/chat';
|
|||||||
import { ChatRoleEnum, sseResponseEventEnum } from '@/constants/chat';
|
import { ChatRoleEnum, sseResponseEventEnum } from '@/constants/chat';
|
||||||
import { parseStreamChunk, textAdaptGptResponse } from '@/utils/adapt';
|
import { parseStreamChunk, textAdaptGptResponse } from '@/utils/adapt';
|
||||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||||
import { SpecificInputEnum } from '@/constants/app';
|
import { TaskResponseKeyEnum } from '@/constants/app';
|
||||||
import { getChatModel } from '@/service/utils/data';
|
import { getChatModel } from '@/service/utils/data';
|
||||||
import { countModelPrice, pushTaskBillListItem } from '@/service/events/pushBill';
|
import { countModelPrice, pushTaskBillListItem } from '@/service/events/pushBill';
|
||||||
import { authUser } from '@/service/utils/auth';
|
import { authUser } from '@/service/utils/auth';
|
||||||
@@ -27,7 +27,7 @@ export type Props = {
|
|||||||
limitPrompt?: string;
|
limitPrompt?: string;
|
||||||
billId?: string;
|
billId?: string;
|
||||||
};
|
};
|
||||||
export type Response = { [SpecificInputEnum.answerText]: string; totalTokens: number };
|
export type Response = { [TaskResponseKeyEnum.answerText]: string; totalTokens: number };
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
let { model, stream } = req.body as Props;
|
let { model, stream } = req.body as Props;
|
||||||
|
@@ -3,7 +3,7 @@ import { jsonRes } from '@/service/response';
|
|||||||
import { PgClient } from '@/service/pg';
|
import { PgClient } from '@/service/pg';
|
||||||
import { withNextCors } from '@/service/utils/tools';
|
import { withNextCors } from '@/service/utils/tools';
|
||||||
import type { ChatItemType } from '@/types/chat';
|
import type { ChatItemType } from '@/types/chat';
|
||||||
import { ChatRoleEnum, rawSearchKey } from '@/constants/chat';
|
import { ChatRoleEnum, rawSearchKey, responseDataKey } from '@/constants/chat';
|
||||||
import { modelToolMap } from '@/utils/plugin';
|
import { modelToolMap } from '@/utils/plugin';
|
||||||
import { getVector } from '@/pages/api/openapi/plugin/vector';
|
import { getVector } from '@/pages/api/openapi/plugin/vector';
|
||||||
import { countModelPrice, pushTaskBillListItem } from '@/service/events/pushBill';
|
import { countModelPrice, pushTaskBillListItem } from '@/service/events/pushBill';
|
||||||
@@ -29,7 +29,9 @@ type Props = {
|
|||||||
billId?: string;
|
billId?: string;
|
||||||
};
|
};
|
||||||
type Response = {
|
type Response = {
|
||||||
|
[responseDataKey]: {
|
||||||
[rawSearchKey]: QuoteItemType[];
|
[rawSearchKey]: QuoteItemType[];
|
||||||
|
};
|
||||||
isEmpty?: boolean;
|
isEmpty?: boolean;
|
||||||
quotePrompt?: string;
|
quotePrompt?: string;
|
||||||
};
|
};
|
||||||
@@ -112,7 +114,6 @@ export async function kbSearch({
|
|||||||
// filter part quote by maxToken
|
// filter part quote by maxToken
|
||||||
const sliceResult = modelToolMap
|
const sliceResult = modelToolMap
|
||||||
.tokenSlice({
|
.tokenSlice({
|
||||||
model: 'gpt-3.5-turbo',
|
|
||||||
maxToken,
|
maxToken,
|
||||||
messages: searchRes.map((item, i) => ({
|
messages: searchRes.map((item, i) => ({
|
||||||
obj: ChatRoleEnum.System,
|
obj: ChatRoleEnum.System,
|
||||||
@@ -128,7 +129,9 @@ export async function kbSearch({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
isEmpty: rawSearch.length === 0 ? true : undefined,
|
isEmpty: rawSearch.length === 0 ? true : undefined,
|
||||||
rawSearch,
|
quotePrompt: sliceResult ? `知识库:\n${sliceResult}` : undefined,
|
||||||
quotePrompt: sliceResult ? `知识库:\n${sliceResult}` : undefined
|
responseData: {
|
||||||
|
rawSearch
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -10,7 +10,7 @@ import { getChatHistory } from './getHistory';
|
|||||||
import { saveChat } from '@/pages/api/chat/saveChat';
|
import { saveChat } from '@/pages/api/chat/saveChat';
|
||||||
import { sseResponse } from '@/service/utils/tools';
|
import { sseResponse } from '@/service/utils/tools';
|
||||||
import { type ChatCompletionRequestMessage } from 'openai';
|
import { type ChatCompletionRequestMessage } from 'openai';
|
||||||
import { SpecificInputEnum, AppModuleItemTypeEnum } from '@/constants/app';
|
import { TaskResponseKeyEnum, AppModuleItemTypeEnum } from '@/constants/app';
|
||||||
import { Types } from 'mongoose';
|
import { Types } from 'mongoose';
|
||||||
import { moduleFetch } from '@/service/api/request';
|
import { moduleFetch } from '@/service/api/request';
|
||||||
import { AppModuleItemType, RunningModuleItemType } from '@/types/app';
|
import { AppModuleItemType, RunningModuleItemType } from '@/types/app';
|
||||||
@@ -223,35 +223,24 @@ export async function dispatchModules({
|
|||||||
}) {
|
}) {
|
||||||
const runningModules = loadModules(modules, variables);
|
const runningModules = loadModules(modules, variables);
|
||||||
|
|
||||||
let storeData: Record<string, any> = {}; // after module used
|
// let storeData: Record<string, any> = {}; // after module used
|
||||||
let responseData: Record<string, any> = {}; // response request and save to database
|
let chatResponse: Record<string, any> = {}; // response request and save to database
|
||||||
let answerText = ''; // AI answer
|
let answerText = ''; // AI answer
|
||||||
|
|
||||||
function pushStore({
|
function pushStore({
|
||||||
isResponse = false,
|
|
||||||
answer,
|
answer,
|
||||||
data = {}
|
responseData = {}
|
||||||
}: {
|
}: {
|
||||||
isResponse?: boolean;
|
|
||||||
answer?: string;
|
answer?: string;
|
||||||
data?: Record<string, any>;
|
responseData?: Record<string, any>;
|
||||||
}) {
|
}) {
|
||||||
if (isResponse) {
|
chatResponse = {
|
||||||
responseData = {
|
...chatResponse,
|
||||||
...responseData,
|
...responseData
|
||||||
...data
|
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
if (answer) {
|
|
||||||
answerText += answer;
|
answerText += answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
storeData = {
|
|
||||||
...storeData,
|
|
||||||
...data
|
|
||||||
};
|
|
||||||
}
|
|
||||||
function moduleInput(
|
function moduleInput(
|
||||||
module: RunningModuleItemType,
|
module: RunningModuleItemType,
|
||||||
data: Record<string, any> = {}
|
data: Record<string, any> = {}
|
||||||
@@ -282,20 +271,13 @@ export async function dispatchModules({
|
|||||||
module: RunningModuleItemType,
|
module: RunningModuleItemType,
|
||||||
result: Record<string, any> = {}
|
result: Record<string, any> = {}
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
|
pushStore(result);
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
module.outputs.map((outputItem) => {
|
module.outputs.map((outputItem) => {
|
||||||
if (result[outputItem.key] === undefined) return;
|
if (result[outputItem.key] === undefined) return;
|
||||||
/* update output value */
|
/* update output value */
|
||||||
outputItem.value = result[outputItem.key];
|
outputItem.value = result[outputItem.key];
|
||||||
|
|
||||||
pushStore({
|
|
||||||
isResponse: outputItem.response,
|
|
||||||
answer: outputItem.answer ? outputItem.value : '',
|
|
||||||
data: {
|
|
||||||
[outputItem.key]: outputItem.value
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/* update target */
|
/* update target */
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
outputItem.targets.map((target: any) => {
|
outputItem.targets.map((target: any) => {
|
||||||
@@ -315,7 +297,7 @@ export async function dispatchModules({
|
|||||||
// direct answer
|
// direct answer
|
||||||
if (module.type === AppModuleItemTypeEnum.answer) {
|
if (module.type === AppModuleItemTypeEnum.answer) {
|
||||||
const text =
|
const text =
|
||||||
module.inputs.find((item) => item.key === SpecificInputEnum.answerText)?.value || '';
|
module.inputs.find((item) => item.key === TaskResponseKeyEnum.answerText)?.value || '';
|
||||||
pushStore({
|
pushStore({
|
||||||
answer: text
|
answer: text
|
||||||
});
|
});
|
||||||
@@ -365,7 +347,7 @@ export async function dispatchModules({
|
|||||||
await Promise.all(initModules.map((module) => moduleInput(module, params)));
|
await Promise.all(initModules.map((module) => moduleInput(module, params)));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
responseData,
|
responseData: chatResponse,
|
||||||
answerText
|
answerText
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -402,7 +384,7 @@ function loadModules(
|
|||||||
}),
|
}),
|
||||||
outputs: module.outputs.map((item) => ({
|
outputs: module.outputs.map((item) => ({
|
||||||
key: item.key,
|
key: item.key,
|
||||||
answer: item.key === SpecificInputEnum.answerText,
|
answer: item.key === TaskResponseKeyEnum.answerText,
|
||||||
response: item.response,
|
response: item.response,
|
||||||
value: undefined,
|
value: undefined,
|
||||||
targets: item.targets
|
targets: item.targets
|
||||||
|
@@ -3,7 +3,7 @@ import { getErrText } from '@/utils/tools';
|
|||||||
import { parseStreamChunk } from '@/utils/adapt';
|
import { parseStreamChunk } from '@/utils/adapt';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { sseResponse } from '../utils/tools';
|
import { sseResponse } from '../utils/tools';
|
||||||
import { SpecificInputEnum } from '@/constants/app';
|
import { TaskResponseKeyEnum } from '@/constants/app';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
res: NextApiResponse; // 用于流转发
|
res: NextApiResponse; // 用于流转发
|
||||||
@@ -45,7 +45,7 @@ export const moduleFetch = ({ url, data, res }: Props) =>
|
|||||||
const reader = response.body?.getReader();
|
const reader = response.body?.getReader();
|
||||||
|
|
||||||
let chatResponse: Record<string, any> = {
|
let chatResponse: Record<string, any> = {
|
||||||
[SpecificInputEnum.answerText]: ''
|
[TaskResponseKeyEnum.answerText]: ''
|
||||||
};
|
};
|
||||||
|
|
||||||
const read = async () => {
|
const read = async () => {
|
||||||
@@ -85,8 +85,8 @@ export const moduleFetch = ({ url, data, res }: Props) =>
|
|||||||
if (answer) {
|
if (answer) {
|
||||||
chatResponse = {
|
chatResponse = {
|
||||||
...chatResponse,
|
...chatResponse,
|
||||||
[SpecificInputEnum.answerText]:
|
[TaskResponseKeyEnum.answerText]:
|
||||||
chatResponse[SpecificInputEnum.answerText] + answer
|
chatResponse[TaskResponseKeyEnum.answerText] + answer
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { Schema, model, models, Model } from 'mongoose';
|
import { Schema, model, models, Model } from 'mongoose';
|
||||||
import { ChatSchema as ChatType } from '@/types/mongoSchema';
|
import { ChatSchema as ChatType } from '@/types/mongoSchema';
|
||||||
import { ChatRoleMap } from '@/constants/chat';
|
import { ChatRoleMap } from '@/constants/chat';
|
||||||
|
import { ChatSourceEnum, ChatSourceMap } from '@/constants/chat';
|
||||||
|
|
||||||
const ChatSchema = new Schema({
|
const ChatSchema = new Schema({
|
||||||
userId: {
|
userId: {
|
||||||
@@ -32,6 +33,14 @@ const ChatSchema = new Schema({
|
|||||||
type: Object,
|
type: Object,
|
||||||
default: {}
|
default: {}
|
||||||
},
|
},
|
||||||
|
// source: {
|
||||||
|
// type: String,
|
||||||
|
// enum: Object.keys(ChatSourceMap),
|
||||||
|
// required: true
|
||||||
|
// },
|
||||||
|
// shareId: {
|
||||||
|
// type: String
|
||||||
|
// },
|
||||||
content: {
|
content: {
|
||||||
type: [
|
type: [
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user