feat: source id prefix env (#3103)
This commit is contained in:
@@ -80,69 +80,76 @@ const pushChatLogInternal = async ({
|
|||||||
url: string;
|
url: string;
|
||||||
metadata?: Metadata;
|
metadata?: Metadata;
|
||||||
}) => {
|
}) => {
|
||||||
const [chatItemHuman, chatItemAi] = await Promise.all([
|
try {
|
||||||
MongoChatItem.findById(chatItemIdHuman).lean(),
|
const [chatItemHuman, chatItemAi] = await Promise.all([
|
||||||
MongoChatItem.findById(chatItemIdAi).lean() as Promise<AIChatItemType>
|
MongoChatItem.findById(chatItemIdHuman).lean(),
|
||||||
]);
|
MongoChatItem.findById(chatItemIdAi).lean() as Promise<AIChatItemType>
|
||||||
|
]);
|
||||||
|
|
||||||
if (!chatItemHuman || !chatItemAi) {
|
if (!chatItemHuman || !chatItemAi) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const chat = await MongoChat.findOne({ chatId }).lean();
|
||||||
|
|
||||||
|
// addLog.warn('ChatLogDebug', chat);
|
||||||
|
// addLog.warn('ChatLogDebug', { chatItemHuman, chatItemAi });
|
||||||
|
|
||||||
|
if (!chat) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const metadataString = JSON.stringify(metadata ?? {});
|
||||||
|
|
||||||
|
const uid = chat.outLinkUid || chat.tmbId;
|
||||||
|
// Pop last two items
|
||||||
|
const question = chatItemHuman.value[chatItemHuman.value.length - 1]?.text?.content;
|
||||||
|
const answer = chatItemAi.value[chatItemAi.value.length - 1]?.text?.content;
|
||||||
|
if (!question || !answer) {
|
||||||
|
addLog.error('[ChatLogPush] question or answer is empty', {
|
||||||
|
question: chatItemHuman.value,
|
||||||
|
answer: chatItemAi.value
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const responseData = chatItemAi.responseData;
|
||||||
|
const responseTime =
|
||||||
|
responseData?.reduce((acc, item) => acc + (item?.runningTime ?? 0), 0) || 0;
|
||||||
|
|
||||||
|
const sourceIdPrefix = process.env.SOURCE_ID_PREFIX ?? '';
|
||||||
|
|
||||||
|
const chatLog: ChatLog = {
|
||||||
|
title: chat.title,
|
||||||
|
feedback: (() => {
|
||||||
|
if (chatItemAi.userGoodFeedback) {
|
||||||
|
return 'like';
|
||||||
|
} else if (chatItemAi.userBadFeedback) {
|
||||||
|
return 'dislike';
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
chatItemId: `${chatItemIdHuman},${chatItemIdAi}`,
|
||||||
|
uid,
|
||||||
|
question,
|
||||||
|
answer,
|
||||||
|
chatId,
|
||||||
|
responseTime: responseTime * 1000,
|
||||||
|
metadata: metadataString,
|
||||||
|
sourceName: chat.source ?? '-',
|
||||||
|
// @ts-ignore
|
||||||
|
createdAt: new Date(chatItemAi.time).getTime(),
|
||||||
|
sourceId: `${sourceIdPrefix}${appId}`
|
||||||
|
};
|
||||||
|
await axios
|
||||||
|
.post(`${url}/api/chat/push`, chatLog)
|
||||||
|
.then((res) => {
|
||||||
|
addLog.info('[ChatLogPush] push success', res.data);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
addLog.error('[ChatLogPush] push failed', { e, resData: e.response?.data });
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
addLog.error('[ChatLogPush] error', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
const chat = await MongoChat.findOne({ chatId }).lean();
|
|
||||||
|
|
||||||
// addLog.warn('ChatLogDebug', chat);
|
|
||||||
// addLog.warn('ChatLogDebug', { chatItemHuman, chatItemAi });
|
|
||||||
|
|
||||||
if (!chat) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const metadataString = JSON.stringify(metadata ?? {});
|
|
||||||
|
|
||||||
const uid = chat.outLinkUid || chat.tmbId;
|
|
||||||
// Pop last two items
|
|
||||||
const question = chatItemHuman.value[chatItemHuman.value.length - 1]?.text?.content;
|
|
||||||
const answer = chatItemAi.value[chatItemAi.value.length - 1]?.text?.content;
|
|
||||||
if (!question || !answer) {
|
|
||||||
addLog.error('[ChatLogPush] question or answer is empty', {
|
|
||||||
question: chatItemHuman.value,
|
|
||||||
answer: chatItemAi.value
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const responseData = chatItemAi.responseData;
|
|
||||||
const responseTime = responseData?.reduce((acc, item) => acc + (item?.runningTime ?? 0), 0) || 0;
|
|
||||||
|
|
||||||
const chatLog: ChatLog = {
|
|
||||||
title: chat.title,
|
|
||||||
feedback: (() => {
|
|
||||||
if (chatItemAi.userGoodFeedback) {
|
|
||||||
return 'like';
|
|
||||||
} else if (chatItemAi.userBadFeedback) {
|
|
||||||
return 'dislike';
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})(),
|
|
||||||
chatItemId: `${chatItemIdHuman},${chatItemIdAi}`,
|
|
||||||
uid,
|
|
||||||
question,
|
|
||||||
answer,
|
|
||||||
chatId,
|
|
||||||
responseTime: responseTime * 1000,
|
|
||||||
metadata: metadataString,
|
|
||||||
sourceName: chat.source ?? '-',
|
|
||||||
// @ts-ignore
|
|
||||||
createdAt: new Date(chatItemAi.time).getTime(),
|
|
||||||
sourceId: `crbeer-fastgpt-${appId}`
|
|
||||||
};
|
|
||||||
await axios
|
|
||||||
.post(url + '/api/chat/push', chatLog)
|
|
||||||
.then((res) => {
|
|
||||||
addLog.info('[ChatLogPush] push success', res.data);
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
addLog.error('[ChatLogPush] push failed', { e, resData: e.response?.data });
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -50,5 +50,7 @@ WORKFLOW_MAX_LOOP_TIMES=50
|
|||||||
|
|
||||||
# 对话日志推送服务
|
# 对话日志推送服务
|
||||||
# URL/INTERVAL 为空时不推送
|
# URL/INTERVAL 为空时不推送
|
||||||
|
# 前缀用于区分不同的对话日志来源
|
||||||
# CHAT_LOG_URL=http://localhost:8080
|
# CHAT_LOG_URL=http://localhost:8080
|
||||||
# CHAT_LOG_INTERVAL=10000
|
# CHAT_LOG_INTERVAL=10000
|
||||||
|
# SOURCE_ID_PREFIX=fastgpt-
|
||||||
|
|||||||
Reference in New Issue
Block a user