From b773a2751403d06afd6674aa3b9260496e67f7b1 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Tue, 11 Feb 2025 21:18:16 +0800 Subject: [PATCH] feat: improve prompt --- src/agent.ts | 28 +++++++++++++++++----------- src/app.ts | 9 +++++---- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/agent.ts b/src/agent.ts index 155112ff..47388d38 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -174,20 +174,24 @@ ${learnedStrategy} // Build actions section - if (allURLs && Object.keys(allURLs).length > 0 && allowRead) { - const urlList = Object.entries(allURLs) - .map(([url, desc]) => ` + "${url}": "${desc}"`) - .join('\n'); + if (allowRead) { + let urlList = ''; + if (allURLs && Object.keys(allURLs).length > 0) { + urlList = Object.entries(allURLs) + .map(([url, desc]) => ` + "${url}": "${desc}"`) + .join('\n'); + } actionSections.push(` - + +- This allows you to access the full content behind any URLs. +- If the contains a URL, you must visit the URL to gather more information. +${urlList ? ` - Visit any URLs from below to gather external knowledge, choose the most relevant URLs that might contain the answer ${urlList} -- When you have enough search result in the context and want to deep dive into specific URLs -- It allows you to access the full content behind any URLs - +`.trim() : ''} `); } @@ -213,8 +217,10 @@ ${allKeywords.join('\n')} actionSections.push(` - If is a simple greeting, chit-chat, or general knowledge, provide the answer directly. -- Provide final response only when 100% certain -- Responses must be definitive (no ambiguity, uncertainty, or disclaimers)${allowReflect ? '\n- If doubts remain, use instead' : ''} +- 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) +- Provide final response only when 100% certain${allowReflect ? '\n- If doubts remain, use instead' : ''} `); } @@ -323,7 +329,7 @@ export async function getResponse(question: string, tokenBudget: number = 1_000_ } // update all urls with buildURLMap - allowRead = allowRead && (Object.keys(allURLs).length > 0); + // allowRead = allowRead && (Object.keys(allURLs).length > 0); allowSearch = allowSearch && (Object.keys(allURLs).length < 50); // disable search when too many urls already // generate prompt for this step diff --git a/src/app.ts b/src/app.ts index 8af806fe..f9cac977 100644 --- a/src/app.ts +++ b/src/app.ts @@ -45,12 +45,13 @@ function buildMdFromAnswer(answer: AnswerAction) { let refStr = ''; if (answer.references?.length > 0) { refStr = ` - -## References + ${answer.references.map((ref, i) => ` -${i + 1}. [${ref.exactQuote}](${ref.url})`).join('')}`; +${i + 1}. [${ref.exactQuote}](${ref.url})`).join('')} + +`.trim(); } - return `${answer.answer.replace(/\(REF_(\d+)\)/g, (_, num) => `[^${num}]`)}${refStr}`; + return `${answer.answer.replace(/\(REF_(\d+)\)/g, (_, num) => `[^${num}]`)}\n${refStr}`; }