XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Improve merge_messages in GUI

ef642f5b
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +34 -18
Modified g4f/gui/client/static/js/chat.v1.js +34 -18
@@ -994,30 +994,48 @@ const new_conversation = async () => {
994 994
995 995 function merge_messages(message1, message2) {
996 996 let newContent = message2;
997 // Remove start tokens
997 998 if (newContent.startsWith("```")) {
998 const index = str.indexOf("\n");
999 newContent = newContent.substring(index);
999 const index = newContent.indexOf("\n");
1000 if (index != -1) {
1001 newContent = newContent.substring(index);
1002 }
1000 1003 } else if (newContent.startsWith("...")) {
1001 1004 newContent = " " + newContent.substring(3);
1002 }
1003 // Remove duplicate words
1004 if (newContent.indexOf(" ") > 0) {
1005 let words = message1.trim().split(" ");
1006 let lastWord = words[words.length - 1];
1007 if (newContent.startsWith(lastWord)) {
1008 newContent = newContent.substring(lastWord.length);
1005 } else {
1006 // Remove duplicate lines
1007 let lines = message1.trim().split("\n");
1008 let lastLine = lines[lines.length - 1];
1009 let foundLastLine = newContent.indexOf(lastLine + "\n");
1010 if (foundLastLine != -1) {
1011 foundLastLine += 1;
1012 } else {
1013 foundLastLine = newContent.indexOf(lastLine);
1014 }
1015 if (foundLastLine != -1) {
1016 newContent = newContent.substring(foundLastLine + lastLine.length);
1017 }
1018 // Remove duplicate words
1019 if (foundLastLine == -1 && newContent.indexOf(" ") > 0) {
1020 let words = message1.trim().split(" ");
1021 let lastWord = words[words.length - 1];
1022 if (newContent.startsWith(lastWord)) {
1023 newContent = newContent.substring(lastWord.length);
1024 }
1009 1025 }
1010 }
1011 // Remove duplicate lines
1012 let lines = message1.trim().split("\n");
1013 let lastLine = lines[lines.length - 1];
1014 while (newContent && lastLine && newContent.startsWith(lastLine)) {
1015 newContent = newContent.substring(lastLine.length);
1016 lastLine = lines[lines.length - 1];
1017 1026 }
1018 1027 return message1 + newContent;
1019 1028 }
1020 1029
1030 // console.log(merge_messages("Hello", "Hello,\nhow are you?"));
1031 // console.log(merge_messages("Hello", "Hello, how are you?"));
1032 // console.log(merge_messages("Hello", "Hello,\nhow are you?"));
1033 // console.log(merge_messages("Hello,\n", "Hello,\nhow are you?"));
1034 // console.log(merge_messages("Hello,\n", "how are you?"));
1035 // console.log(merge_messages("1 != 2", "1 != 2;"));
1036 // console.log(merge_messages("1 != 2", "```python\n1 != 2;"));
1037 // console.log(merge_messages("1 != 2;\n1 != 3;\n", "1 != 2;\n1 != 3;\n"));
1038
1021 1039 const load_conversation = async (conversation_id, scroll=true) => {
1022 1040 let conversation = await get_conversation(conversation_id);
1023 1041 let messages = conversation?.items || [];
@@ -1039,7 +1057,6 @@ const load_conversation = async (conversation_id, scroll=true) => {
1039 1057 let last_model = null;
1040 1058 let providers = [];
1041 1059 let buffer = "";
1042 let last_usage = null;
1043 1060 let completion_tokens = 0;
1044 1061
1045 1062 messages.forEach((item, i) => {
@@ -1051,7 +1068,6 @@ const load_conversation = async (conversation_id, scroll=true) => {
1051 1068 buffer = buffer.replace(/ \[aborted\]$/g, "").replace(/ \[error\]$/g, "");
1052 1069 buffer += merge_messages(buffer, item.content);
1053 1070 last_model = item.provider?.model;
1054 last_usage = item.usage;
1055 1071 providers.push(item.provider?.name);
1056 1072 let next_i = parseInt(i) + 1;
1057 1073 let next_provider = item.provider ? item.provider : (messages.length > next_i ? messages[next_i].provider : null);