fix: optimize prompt

This commit is contained in:
Han Xiao 2025-02-15 15:20:23 +08:00
parent f1277dc2f9
commit 8bd5d37d6d
2 changed files with 57 additions and 7 deletions

View File

@ -87,7 +87,6 @@ function getPrompt(
knowledge?: KnowledgeItem[],
allURLs?: Record<string, string>,
beastMode?: boolean,
languageStyle?: string
): string {
const sections: string[] = [];
const actionSections: string[] = [];
@ -226,9 +225,8 @@ ${allKeywords?.length ? `
${allKeywords.join('\n')}
</bad-queries>
`.trim() : ''}
- Propose some unique new queries that might help you find the answer to the question
- Propose some unique new keywords queries that might help you find the answer to the question
- Focus on solving one specific aspect of the original question
- Only use keywords, not full sentences
</action-search>
`);
}
@ -386,7 +384,6 @@ export async function getResponse(question: string,
allKnowledge,
allURLs,
false,
evaluationMetrics[currentQuestion].languageStyle
);
schema = getSchema(allowReflect, allowRead, allowAnswer, allowSearch,
evaluationMetrics[currentQuestion].languageStyle)
@ -551,6 +548,9 @@ But then you realized you have asked them before. You decided to to think out of
// rewrite queries
let {queries: keywordsQueries} = await rewriteQuery(thisStep, context.tokenTracker);
// add the original query before rewrite to the keywordsQueries
keywordsQueries.push(thisStep.searchQuery)
const oldKeywords = keywordsQueries;
// avoid exisitng searched queries
const {unique_queries: dedupedQueries} = await dedupQueries(keywordsQueries, allKeywords, context.tokenTracker);
@ -727,7 +727,6 @@ You decided to think out of the box or cut from a completely different angle.`);
allKnowledge,
allURLs,
true,
evaluationMetrics[question]?.languageStyle || 'same language as the question'
);
schema = getSchema(false, false, true, false,

View File

@ -32,8 +32,59 @@ function buildMdFromAnswer(answer: AnswerAction) {
return answer.answer;
}
// Extract all footnotes from answer
const footnotes: string[] = [];
const footnoteRegex = /\[\^(\d+)]/g;
let match;
while ((match = footnoteRegex.exec(answer.answer)) !== null) {
footnotes.push(match[1]);
}
// Check if correction is needed
const needsCorrection = (() => {
// No footnotes found
if (footnotes.length === 0) return false;
// Case 1 & 3: All footnotes are same number AND matches reference count
if (footnotes.length === answer.references.length &&
footnotes.every(n => n === footnotes[0])) {
return true;
}
// Case 2: All footnotes are same number AND number is too high
return footnotes.every(n => n === footnotes[0]) &&
parseInt(footnotes[0]) > answer.references.length;
})();
if (!needsCorrection) {
const references = answer.references.map((ref, i) => {
const cleanQuote = ref.exactQuote
.replace(/[^a-zA-Z0-9]+/g, ' ')
.trim();
if (ref.url.startsWith('http')) {
return `[^${i + 1}]: [${cleanQuote}](${ref.url})`;
} else {
return `[^${i + 1}]: ${cleanQuote}`;
}
}).join('\n\n');
return `
${answer.answer}
${references}
`.trim();
}
// Apply correction: sequentially number the footnotes
let currentIndex = 0;
const correctedAnswer = answer.answer.replace(footnoteRegex, () =>
`[^${++currentIndex}]`
);
const references = answer.references.map((ref, i) => {
// Clean up the quote text
const cleanQuote = ref.exactQuote
.replace(/[^a-zA-Z0-9]+/g, ' ')
.trim();
@ -46,7 +97,7 @@ function buildMdFromAnswer(answer: AnswerAction) {
}).join('\n\n');
return `
${answer.answer.replace(/\(REF_(\d+)\)/g, (_, num) => `[^${num}]`)}
${correctedAnswer}
${references}
`.trim();