Skip to content

Commit

Permalink
perf: variable store
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Aug 27, 2024
1 parent 8db44a5 commit b515968
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
19 changes: 0 additions & 19 deletions packages/service/core/chat/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ChatItemValueTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/c
import { delFileByFileIdList, getGFSCollection } from '../../common/file/gridfs/controller';
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
import { MongoChat } from './chatSchema';
import { ChatSchema as ChatType } from '@fastgpt/global/core/chat/type.d';

export async function getChatItems({
appId,
Expand Down Expand Up @@ -37,24 +36,6 @@ export async function getChatItems({
return { histories };
}

export async function getChat({
appId,
chatId,
field
}: {
appId: string;
chatId?: string;
field: string;
}): Promise<{ chat: ChatType | null }> {
if (!chatId) {
return { chat: null };
}

const chat = await MongoChat.findOne({ appId, chatId }, field).lean();

return { chat };
}

/* Temporary adaptation for old conversation records */
export const adaptStringValue = (value: any): ChatItemValueItemType[] => {
if (typeof value === 'string') {
Expand Down
22 changes: 8 additions & 14 deletions projects/app/src/pages/api/v1/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
textAdaptGptResponse
} from '@fastgpt/global/core/workflow/runtime/utils';
import { GPTMessages2Chats, chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
import { getChat, getChatItems } from '@fastgpt/service/core/chat/controller';
import { getChatItems } from '@fastgpt/service/core/chat/controller';
import { saveChat } from '@fastgpt/service/core/chat/saveChat';
import { responseWrite } from '@fastgpt/service/common/response';
import { pushChatUsage } from '@/service/support/wallet/usage/push';
Expand Down Expand Up @@ -216,44 +216,38 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}
return latestHumanChat;
})();
const { text, files } = chatValue2RuntimePrompt(userQuestion.value);

// Get and concat history;
const limit = getMaxHistoryLimitFromNodes(app.modules);
const [{ histories }, { nodes, edges, chatConfig }, { chat }] = await Promise.all([
const [{ histories }, { nodes, edges, chatConfig }, chatDetail] = await Promise.all([
getChatItems({
appId: app._id,
chatId,
limit,
field: `dataId obj value nodeOutputs`
}),
getAppLatestVersion(app._id, app),
getChat({
appId: app._id,
chatId,
field: 'source variableList variables'
})
MongoChat.findOne({ appId: app._id, chatId }, 'source variableList variables')
]);
// get chat histories

// Get chat histories
const newHistories = concatHistories(histories, chatMessages);

// get global variables
if (chat && chat.variables) {
// Get store variables(Api variable precedence)
if (chatDetail?.variables) {
variables = {
...chat.variables,
...chatDetail.variables,
...variables
};
}

// Get runtimeNodes
let runtimeNodes = storeNodes2RuntimeNodes(nodes, getWorkflowEntryNodeIds(nodes, newHistories));

if (isPlugin) {
// Rewrite plugin run params variables
variables = removePluginInputVariables(variables, runtimeNodes);
runtimeNodes = updatePluginInputByVariables(runtimeNodes, variables);
}

runtimeNodes = rewriteNodeOutputByHistories(newHistories, runtimeNodes);

const workflowResponseWrite = getWorkflowResponseWrite({
Expand Down

0 comments on commit b515968

Please sign in to comment.