mirror of
https://github.com/LLM-Red-Team/glm-free-api.git
synced 2024-12-22 17:09:22 +08:00
修复多轮对话时文件传送角色错配,抹除临时文件路径减少多轮幻觉
This commit is contained in:
parent
c70a4a4102
commit
600a42938c
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user