post adjustment processing
This commit is contained in:
+44
-23
@@ -1,10 +1,23 @@
|
|||||||
export function formatMessages(messages) {
|
export function formatMessages(messages, proxyModel) {
|
||||||
|
function convertToUpperCase(messages) {
|
||||||
|
return messages.map(message => {
|
||||||
|
let content = message.content;
|
||||||
|
|
||||||
|
content = content.replace(/^(system|assistant|user):/gim, match => match.toUpperCase());
|
||||||
|
content = content.replace(/\n(system|assistant|user|human):/gim, (match, p1) => '\n' + p1.toUpperCase() + ':');
|
||||||
|
|
||||||
|
const role = message.role.toUpperCase();
|
||||||
|
|
||||||
|
return { role, content };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否存在 "<!-- AI Round 0 begins. -->" 标记
|
// 检查是否存在 "<!-- AI Round 0 begins. -->" 标记
|
||||||
const hasAIRound0 = messages.some(message => message.content.includes('<!-- AI Round 0 begins. -->'));
|
const hasAIRound0 = messages.some(message => message.content.includes('<!-- AI Round 0 begins. -->'));
|
||||||
|
|
||||||
// 如果没有找到标记,直接返回原始消息数组
|
// 如果没有找到标记,直接返回原始消息数组
|
||||||
if (!hasAIRound0) {
|
if (!hasAIRound0) {
|
||||||
return messages;
|
return proxyModel === 'gpt_4o' ? convertToUpperCase(messages) : messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
let formattedMessages = [];
|
let formattedMessages = [];
|
||||||
@@ -12,7 +25,6 @@ export function formatMessages(messages) {
|
|||||||
let assistantRoundCounter = 0;
|
let assistantRoundCounter = 0;
|
||||||
let descriptionPointCounter = 0;
|
let descriptionPointCounter = 0;
|
||||||
let isFirstUserFound = false;
|
let isFirstUserFound = false;
|
||||||
let isLatestRound = false;
|
|
||||||
let lastAssistantRound = 0;
|
let lastAssistantRound = 0;
|
||||||
|
|
||||||
// 查找初始回合数
|
// 查找初始回合数
|
||||||
@@ -34,6 +46,21 @@ export function formatMessages(messages) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 找到包含 </context> --- 的消息索引
|
||||||
|
let contextEndIndex = messages.length;
|
||||||
|
for (let i = 0; i < messages.length; i++) {
|
||||||
|
if (messages[i].content.includes('</context> ---')) {
|
||||||
|
contextEndIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 找到最后一个 user 消息的索引(在 </context> --- 之前)
|
||||||
|
let lastUserIndex = contextEndIndex - 1;
|
||||||
|
while (lastUserIndex >= 0 && messages[lastUserIndex].role !== 'user') {
|
||||||
|
lastUserIndex--;
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < messages.length; i++) {
|
for (let i = 0; i < messages.length; i++) {
|
||||||
const message = messages[i];
|
const message = messages[i];
|
||||||
|
|
||||||
@@ -45,7 +72,7 @@ export function formatMessages(messages) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.role === 'user') {
|
if (message.role === 'user' && i < contextEndIndex) {
|
||||||
if (isFirstUserFound) {
|
if (isFirstUserFound) {
|
||||||
userRoundCounter = lastAssistantRound + 1;
|
userRoundCounter = lastAssistantRound + 1;
|
||||||
descriptionPointCounter++;
|
descriptionPointCounter++;
|
||||||
@@ -54,37 +81,31 @@ export function formatMessages(messages) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let roundInfo = '';
|
let roundInfo = '';
|
||||||
if (i + 1 < messages.length && messages[i + 1].role === 'assistant') {
|
if (i === lastUserIndex) {
|
||||||
|
roundInfo = `{{最新user:(${userRoundCounter})回合|assistant:(${userRoundCounter + 1})回合开始,基于上回(${descriptionPointCounter - 1}(user${userRoundCounter - 1}|assistant${userRoundCounter}))中的历史锚点内的\`assistant:\`发言末尾衔接,叙事创作时不要只关注最近故事动向,尽可能的记住多个历史锚点中的"assistant:..."故事内容,保持思路清晰来响应下一个回复:}}\n`;
|
||||||
|
} else {
|
||||||
const nextAssistantRound = userRoundCounter + 1;
|
const nextAssistantRound = userRoundCounter + 1;
|
||||||
roundInfo = `{{历史第 user = 回合${userRoundCounter}|assistant = 回合${nextAssistantRound} 开始,标记锚点:[${descriptionPointCounter}]}}\n`;
|
roundInfo = `{{历史第 user = 回合${userRoundCounter}|assistant = 回合${nextAssistantRound} 开始,标记锚点:[${descriptionPointCounter}]}}\n`;
|
||||||
} else {
|
|
||||||
isLatestRound = true;
|
|
||||||
roundInfo = `{{最新user:(${userRoundCounter})回合|assistant:(${userRoundCounter + 1})回合开始,基于上回(${descriptionPointCounter - 1}(user${userRoundCounter - 1}|assistant${userRoundCounter}))中的历史锚点内的\`assistant:\`发言末尾衔接,叙事创作时不要只关注最近故事动向,尽可能的记住多个历史锚点中的"assistant:..."故事内容,保持思路清晰来响应下一个回复:}}\n`;
|
|
||||||
}
|
}
|
||||||
formattedMessages.push({
|
message.content = roundInfo + message.content;
|
||||||
role: 'system',
|
} else if (message.role === 'assistant' && i < contextEndIndex) {
|
||||||
content: roundInfo
|
|
||||||
});
|
|
||||||
} else if (message.role === 'assistant') {
|
|
||||||
const match = message.content.match(/<!-- AI Round (\d+) begins\. -->/);
|
const match = message.content.match(/<!-- AI Round (\d+) begins\. -->/);
|
||||||
if (match) {
|
if (match) {
|
||||||
assistantRoundCounter = parseInt(match[1]);
|
assistantRoundCounter = parseInt(match[1]);
|
||||||
lastAssistantRound = assistantRoundCounter;
|
lastAssistantRound = assistantRoundCounter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message.content.includes('<CHAR_turn>') && i < lastUserIndex) {
|
||||||
|
message.content += `\n--------------------<历史锚点[${descriptionPointCounter}]结束>--------------------`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
formattedMessages.push(message);
|
formattedMessages.push(message);
|
||||||
|
}
|
||||||
|
|
||||||
if (message.content.includes('<CHAR_turn>') && !isLatestRound) {
|
// 如果 proxyModel 是 gpt_4o,则转换消息前缀为大写
|
||||||
formattedMessages.push({
|
if (proxyModel === 'gpt_4o') {
|
||||||
role: 'system',
|
formattedMessages = convertToUpperCase(formattedMessages);
|
||||||
content: `--------------------<历史锚点[${descriptionPointCounter}]结束>--------------------`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.content.includes('<!-- The following are the writing style rules and guidelines for the turn-based collaborative storytelling: -->')) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return formattedMessages;
|
return formattedMessages;
|
||||||
|
|||||||
Reference in New Issue
Block a user