修复多轮对话时文件传送角色错配,抹除临时文件路径减少多轮幻觉

This commit is contained in:
Vinlic 2024-04-26 16:19:57 +08:00
parent c70a4a4102
commit 600a42938c

View File

@ -486,10 +486,6 @@ function extractRefFileUrls(messages: any[]) {
* *
* *
* *
* 使\n
* :旧消息1
* :旧消息2
* :新消息
* *
* @param messages gpt系列消息格式 * @param messages gpt系列消息格式
*/ */
@ -520,20 +516,25 @@ function messagesPrepare(messages: any[], refs: any[]) {
const content = ( const content = (
messages.reduce((content, message) => { messages.reduce((content, message) => {
const role = message.role
.replace("system", "<|sytstem|>")
.replace("assistant", "<|assistant|>")
.replace("user", "<|user|>");
if (_.isArray(message.content)) { if (_.isArray(message.content)) {
return ( return (
message.content.reduce((_content, v) => { message.content.reduce((_content, v) => {
if (!_.isObject(v) || v["type"] != "text") return _content; if (!_.isObject(v) || v["type"] != "text") return _content;
return _content + ("<|user|>\n" + v["text"] || "") + "\n"; return _content + (`${role}\n` + v["text"] || "") + "\n";
}, content) }, content)
); );
} }
return (content += `${message.role return (content += `${role}\n${message.content}\n`);
.replace("system", "<|sytstem|>")
.replace("assistant", "<|assistant|>")
.replace("user", "<|user|>")}\n${message.content}\n`);
}, "") + "<|assistant|>\n" }, "") + "<|assistant|>\n"
).replace(/\!\[.+\]\(.+\)/g, ""); )
// 移除MD图像URL避免幻觉
.replace(/\!\[.+\]\(.+\)/g, "")
// 移除临时路径避免在新会话引发幻觉
.replace(/\/mnt\/data\/.+/g, "");
const fileRefs = refs.filter((ref) => !ref.width && !ref.height); const fileRefs = refs.filter((ref) => !ref.width && !ref.height);
const imageRefs = refs const imageRefs = refs
.filter((ref) => ref.width || ref.height) .filter((ref) => ref.width || ref.height)
@ -541,6 +542,7 @@ function messagesPrepare(messages: any[], refs: any[]) {
ref.image_url = ref.file_url; ref.image_url = ref.file_url;
return ref; return ref;
}); });
content
logger.info("\n对话合并\n" + content); logger.info("\n对话合并\n" + content);
return [ return [
{ {
@ -1062,7 +1064,7 @@ async function receiveImages(
let match; let match;
while ((match = urlPattern.exec(text)) !== null) { while ((match = urlPattern.exec(text)) !== null) {
const url = match[1]; const url = match[1];
if(imageUrls.indexOf(url) == -1) if (imageUrls.indexOf(url) == -1)
imageUrls.push(url); imageUrls.push(url);
} }
} }
@ -1141,7 +1143,7 @@ async function getTokenLiveStatus(refreshToken: string) {
const { accessToken } = _result; const { accessToken } = _result;
return !!accessToken; return !!accessToken;
} }
catch(err) { catch (err) {
return false; return false;
} }
} }