From e2675d6b0969bb6333f0fc9c23dc5e3327cff2eb Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 9 May 2024 21:40:34 +0800 Subject: [PATCH] refactor: playground webapp supports structure prompt (#496) --- assets/playground.html | 72 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/assets/playground.html b/assets/playground.html index 56f84eb..6160a9f 100644 --- a/assets/playground.html +++ b/assets/playground.html @@ -899,7 +899,7 @@ }, buildBody() { - const messages = []; + let messages = []; for ([userMessage, assistantMessage] of chunkArray(this.messages.filter(v => v.id > this.skipMessageId), 2)) { if (assistantMessage.state == "failed") { continue; @@ -924,10 +924,25 @@ if (messages[0]?.content?.indexOf("__INPUT__") > -1) { messages[0].content = systemPrompt.replace("__INPUT__", messages[0].content); } else { - messages.unshift({ - role: "system", - content: systemPrompt, - }); + const { system, cases } = parseStructurePrompt(systemPrompt); + const promptMessages = []; + if (system) { + promptMessages.push({ + role: "system", + content: system, + }); + } + for (const item of cases) { + promptMessages.push({ + role: "user", + content: item.input, + }); + promptMessages.push({ + role: "assistant", + content: item.output, + }); + } + messages = [...promptMessages, ...messages]; } } const body = { @@ -1035,6 +1050,53 @@ }, duration); } + function parseStructurePrompt(prompt) { + let text = prompt; + let searchInput = true; + let system = null; + let parts = []; + + while (text) { + const search = searchInput ? "### INPUT:" : "### OUTPUT:"; + const index = text.indexOf(search); + + if (index !== -1) { + if (system == null) { + system = text.slice(0, index); + } else { + parts.push(text.slice(0, index)); + } + searchInput = !searchInput; + text = text.slice(index + search.length); + } else { + if (text.trim()) { + if (system == null) { + system = text; + } else { + parts.push(text); + } + } + break; + } + } + + const partsLength = parts.length; + if (partsLength > 0 && partsLength % 2 === 0) { + const cases = parts.reduce((acc, val, idx) => { + if (idx % 2 === 0) { + acc.push({ input: val.trim() }) + } else { + acc[acc.length - 1].output = val.trim(); + } + return acc; + }, []); + system = system ? system.trim() : ""; + return { system, cases } + } + + return { system: prompt, cases: [] } + } + function convertImageToDataURL(imageFile) { return new Promise((resolve, reject) => { if (!imageFile) {