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

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系列消息格式
*/
@ -520,20 +516,25 @@ function messagesPrepare(messages: any[], refs: any[]) {
const content = (
messages.reduce((content, message) => {
const role = message.role
.replace("system", "<|sytstem|>")
.replace("assistant", "<|assistant|>")
.replace("user", "<|user|>");
if (_.isArray(message.content)) {
return (
message.content.reduce((_content, v) => {
if (!_.isObject(v) || v["type"] != "text") return _content;
return _content + ("<|user|>\n" + v["text"] || "") + "\n";
return _content + (`${role}\n` + v["text"] || "") + "\n";
}, content)
);
}
return (content += `${message.role
.replace("system", "<|sytstem|>")
.replace("assistant", "<|assistant|>")
.replace("user", "<|user|>")}\n${message.content}\n`);
return (content += `${role}\n${message.content}\n`);
}, "") + "<|assistant|>\n"
).replace(/\!\[.+\]\(.+\)/g, "");
)
// 移除MD图像URL避免幻觉
.replace(/\!\[.+\]\(.+\)/g, "")
// 移除临时路径避免在新会话引发幻觉
.replace(/\/mnt\/data\/.+/g, "");
const fileRefs = refs.filter((ref) => !ref.width && !ref.height);
const imageRefs = refs
.filter((ref) => ref.width || ref.height)
@ -541,6 +542,7 @@ function messagesPrepare(messages: any[], refs: any[]) {
ref.image_url = ref.file_url;
return ref;
});
content
logger.info("\n对话合并\n" + content);
return [
{