From 8e5367f9a829a320721f693210bb6187e9d29144 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Fri, 14 Feb 2025 21:33:00 +0800 Subject: [PATCH] fix: remove references tag in output --- src/agent.ts | 3 +-- src/app.ts | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/agent.ts b/src/agent.ts index dc66d75..b31a05c 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -40,7 +40,7 @@ function getSchema(allowReflect: boolean, allowRead: boolean, allowAnswer: boole if (allowAnswer) { actions.push("answer"); properties.answer = z.string() - .describe(`Required when action='answer'. Must in ${languageStyle}`).optional(); + .describe(`Required when action='answer'. Must in ${languageStyle}. Use markdown footnote syntax like [^1], [^2] to refer the corresponding reference item`).optional(); properties.references = z.array( z.object({ exactQuote: z.string().describe("Exact relevant quote from the document"), @@ -219,7 +219,6 @@ ${allKeywords.join('\n')} - If is a simple greeting, chit-chat, or general knowledge, provide the answer directly; - Must provide "references" and each must specify "exactQuote" and "url"; -- In the answer, use markdown footnote syntax like [^1], [^2] to refer to the references; - Responses must be definitive (no ambiguity, uncertainty, or disclaimers) and in the style of ${languageStyle}; - Provide final response only when 100% certain;${allowReflect ? '\n- If doubts remain, use instead' : ''} diff --git a/src/app.ts b/src/app.ts index 28e6e2b..ee365cb 100644 --- a/src/app.ts +++ b/src/app.ts @@ -33,20 +33,36 @@ function buildMdFromAnswer(answer: AnswerAction) { } const references = answer.references.map((ref, i) => { - const escapedQuote = ref.exactQuote - .replace(/([[\]_*`])/g, '\\$1') - .replace(/\n/g, ' ') + // Clean up the quote text + const cleanQuote = ref.exactQuote + // Remove HTML artifacts + .replace(/<[^>]+>/g, '') + // Remove multiple spaces + .replace(/\s+/g, ' ') + // Remove special characters and markdown conflicts + .replace(/[[\]_*`]/g, '') + // Clean up any remaining artifacts + .replace(/=+/g, '') + .replace(/_[^_]+_/g, '$1') + // Remove navigation artifacts + .replace(/arrow_drop_down/g, '') + .replace(/language/g, '') + // Trim whitespace .trim(); - return `[^${i + 1}]: [${escapedQuote}](${ref.url})`; + // If the quote is too long, truncate it + const maxLength = 20; + const truncatedQuote = cleanQuote.length > maxLength + ? cleanQuote.substring(0, maxLength) + '...' + : cleanQuote; + + return `[^${i + 1}]: [${truncatedQuote}](${ref.url})`; }).join('\n\n'); return ` ${answer.answer.replace(/\(REF_(\d+)\)/g, (_, num) => `[^${num}]`)} - ${references} - `.trim(); }