fix: remove references tag in output

This commit is contained in:
Han Xiao 2025-02-14 21:33:00 +08:00
parent db79e40896
commit 8e5367f9a8
2 changed files with 23 additions and 8 deletions

View File

@ -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')}
<action-answer>
- If <question> 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 <action-reflect> instead' : ''}
</action-answer>

View File

@ -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();
}