Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit a00268b

Browse files
authored
(EAI-1187) Instruct chatbot on handling fetch_page fallback (#897)
* fetch_page fallback disclaimer instruction * PR review - importantNote shouldn't be in the systemPromptContent * 1 prompt adherence eval case for fallback instruction * Tool instructions in tool descriptions * Clarify fetch_page vs. search_content usage
1 parent d1f8fc5 commit a00268b

File tree

6 files changed

+97
-28
lines changed

6 files changed

+97
-28
lines changed

packages/chatbot-server-mongodb-public/evalCases/system_prompt_conversations.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
- name: default_system_prompt_fetch_fallback
2+
messages:
3+
- role: user
4+
content: use this https://www.geeksforgeeks.org/mongodb/search-text-in-mongodb/ how do I do a vector search query
5+
expectation: The response should let the user know if the fallback search was used.
6+
expectedPromptAdherence:
7+
- The response provides a disclaimer that the provided URL did not contain relevant information and that the knowledge base was searched instead.
8+
tags:
9+
- fetch_page
10+
- systemPromptAdherence
11+
- name: default_system_prompt_no_fetch_fallback
12+
messages:
13+
- role: user
14+
content: What is the Atlas Architecture Center?
15+
expectation: The response should not say a search was performed.
16+
expectedPromptAdherence:
17+
- The response does not say that a search was performed or mention that they used a "knowledge base". It is OK if the response is "I don't know" or similar.
18+
tags:
19+
- fetch_page
20+
- systemPromptAdherence
21+
- name: default_system_prompt_no_fetch_fallback_2
22+
messages:
23+
- role: user
24+
content: How does voyage3.5 compare to voyage-3?
25+
expectation: The response should not say a search was performed.
26+
expectedPromptAdherence:
27+
- The response does not say that a search was performed or mention that they used a "knowledge base". It is OK if the response is "I don't know" or similar.
28+
tags:
29+
- fetch_page
30+
- systemPromptAdherence
31+
- name: default_system_prompt_no_fetch_fallback_3
32+
messages:
33+
- role: user
34+
content: What are the core values of MongoDB?
35+
expectation: The response should not say a search was performed.
36+
expectedPromptAdherence:
37+
- The response does not say that a search was performed or mention that they used a "knowledge base". It is OK if the response is "I don't know" or similar.
38+
tags:
39+
- fetch_page
40+
- systemPromptAdherence
141
- name: custom_personality_override
242
messages:
343
- role: user

packages/chatbot-server-mongodb-public/src/systemPrompt.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import {
33
mongoDbProducts,
44
mongoDbProgrammingLanguages,
55
} from "mongodb-rag-core/mongoDbMetadata";
6+
import {
7+
makeMarkdownUnorderedList,
8+
makeMarkdownNumberedList,
9+
} from "mongodb-rag-core/dataSources";
610
import { SEARCH_TOOL_NAME } from "./tools/search";
711
import {
812
FETCH_PAGE_TOOL_NAME,
@@ -60,20 +64,14 @@ const searchRequiresRephraseNotes = [
6064
"You should also transform the user query into a fully formed question, if relevant.",
6165
];
6266

63-
const searchContentToolNotes = [
64-
"Search all of the available MongoDB reference documents for a given user input.",
65-
"You must generate an appropriate search query for a given user input.",
66-
"You are doing this for MongoDB, and all queries relate to MongoDB products.",
67-
`Only generate ONE ${SEARCH_TOOL_NAME} tool call per user message unless there are clearly multiple distinct queries needed to answer the user query.`,
67+
const coordinateToolNotes = [
68+
`When deciding whether to call ${FETCH_PAGE_TOOL_NAME}, DO NOT ASSUME that the user wants to use the ${FETCH_PAGE_TOOL_NAME} based on the pageUrl in the Front Matter.`,
69+
`The ${FETCH_PAGE_TOOL_NAME} tool should only be used if the user asks you to reference the page they are on (e.g. "Use the page I'm on..."), or if the user explicitly provides a URL in their question.`,
70+
`If the ${FETCH_PAGE_TOOL_NAME} tool returns the string "${SEARCH_ALL_FALLBACK_TEXT}", you MUST immediately call the ${SEARCH_TOOL_NAME} tool.`,
6871
];
6972

70-
const fetchPageToolNotes = [
71-
"Fetch the entire page content for a given URL.",
72-
`Do not assume that the user wants to use the ${FETCH_PAGE_TOOL_NAME} based on the URL in the Front Matter. The ${FETCH_PAGE_TOOL_NAME} should ONLY be used if the user implies you should look on the page or if the user explicitly provides a URL in their question.`,
73-
`If the user provides URLs in their query, ONLY call the ${FETCH_PAGE_TOOL_NAME} for those URLs, and do NOT call the ${FETCH_PAGE_TOOL_NAME} for the URL in the Front Matter.`,
74-
"Sometimes, when a page is very long, a search will be performed over the page. Therefore, you must also provide a search query to the tool.",
75-
"Do not include URLs in the search query.",
76-
`If the ${FETCH_PAGE_TOOL_NAME} tool returns the string "${SEARCH_ALL_FALLBACK_TEXT}", you MUST immediately call the ${SEARCH_TOOL_NAME} tool.`,
73+
const toolUseDisclaimers = [
74+
`If you called the ${FETCH_PAGE_TOOL_NAME} tool and it returned the string "${SEARCH_ALL_FALLBACK_TEXT}", you must tell the user in your final answer: "I couldn't use that page to answer your question, so I searched my knowledge base to find an answer."`,
7775
];
7876

7977
const importantNote = `<important>
@@ -138,28 +136,27 @@ ${makeMarkdownNumberedList(searchRequiresRephraseNotes)}
138136
139137
<tools>
140138
141-
<tool name="${SEARCH_TOOL_NAME}">
139+
You have access to the ${SEARCH_TOOL_NAME} and ${FETCH_PAGE_TOOL_NAME} tools.
140+
141+
<coordinate_tools>
142142
143-
You have access to the ${SEARCH_TOOL_NAME} tool. Use the ${SEARCH_TOOL_NAME} tool as follows:
144-
${makeMarkdownNumberedList(searchContentToolNotes)}
143+
Follow these guidelines when using the tools:
145144
146-
When you search, include metadata about the relevant MongoDB programming language and product.
147-
</tool>
145+
${makeMarkdownUnorderedList(coordinateToolNotes)}
148146
149-
<tool name=${FETCH_PAGE_TOOL_NAME}>
147+
</coordinate_tools>
150148
151-
You have access to the ${FETCH_PAGE_TOOL_NAME} tool. Use the ${FETCH_PAGE_TOOL_NAME} tool as follows:
152-
${makeMarkdownNumberedList(fetchPageToolNotes)}
149+
<tool_disclaimers>
153150
154-
</tool>
151+
When writing your final answer, provide any necessary disclaimers:
152+
153+
${makeMarkdownUnorderedList(toolUseDisclaimers)}
154+
155+
</tool_disclaimers>
155156
156157
</tools>`,
157158
} satisfies SystemMessage;
158159

159-
function makeMarkdownNumberedList(items: string[]) {
160-
return items.map((item, i) => `${i + 1}. ${item}`).join("\n");
161-
}
162-
163160
export const makeMongoDbAssistantSystemPrompt: MakeSystemPrompt = (
164161
customSystemPrompt,
165162
customToolDefinitions

packages/chatbot-server-mongodb-public/src/tools/fetchPage.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
Reference,
77
} from "mongodb-rag-core";
88
import { MakeReferenceLinksFunc } from "mongodb-chatbot-server";
9-
import { normalizeUrl } from "mongodb-rag-core/dataSources";
9+
import {
10+
normalizeUrl,
11+
makeMarkdownNumberedList,
12+
} from "mongodb-rag-core/dataSources";
1013
import { wrapTraced } from "mongodb-rag-core/braintrust";
1114
import { Tool, tool } from "mongodb-rag-core/aiSdk";
1215

@@ -38,6 +41,13 @@ export interface MakeFetchPageToolParams {
3841
pageLengthCutoff?: number;
3942
}
4043

44+
const fetchPageToolNotes = [
45+
`Remember - do not assume that the user wants to use the ${FETCH_PAGE_TOOL_NAME} based on the pageUrl in the Front Matter. Only call this tool when specifically instructed to use a certain page.`,
46+
`If the user provides multiple URLs in their query, call the ${FETCH_PAGE_TOOL_NAME} once for each URL, and do not call the ${FETCH_PAGE_TOOL_NAME} for the URL in the Front Matter.`,
47+
"Sometimes, when a page is very long, a search will be performed over the page. Therefore, you must also provide a search query to the tool.",
48+
"Do not include URLs in the search query.",
49+
];
50+
4151
export function makeFetchPageTool({
4252
loadPage,
4353
findContent,
@@ -47,7 +57,9 @@ export function makeFetchPageTool({
4757
}: MakeFetchPageToolParams): FetchPageTool {
4858
return tool({
4959
inputSchema: MongoDbFetchPageToolArgsSchema,
50-
description: "Fetch all content for a specific URL",
60+
description: `Fetches the entire page contents for a specific URL. Use this tool as follows:
61+
62+
${makeMarkdownNumberedList(fetchPageToolNotes)}`,
5163
toModelOutput(output) {
5264
return {
5365
type: "content",

packages/chatbot-server-mongodb-public/src/tools/search.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
mongoDbProducts,
1212
mongoDbProgrammingLanguageIds,
1313
} from "mongodb-rag-core/mongoDbMetadata";
14+
import { makeMarkdownNumberedList } from "mongodb-rag-core/dataSources";
1415
import { wrapTraced } from "mongodb-rag-core/braintrust";
1516
import { MakeReferenceLinksFunc } from "mongodb-chatbot-server";
1617

@@ -56,13 +57,24 @@ export interface MakeSearchToolParams {
5657
makeReferences: MakeReferenceLinksFunc;
5758
}
5859

60+
const searchContentToolNotes = [
61+
"Search all of the available MongoDB reference documents for a given user input.",
62+
"You must generate an appropriate search query for a given user input.",
63+
"You are doing this for MongoDB, and all queries relate to MongoDB products.",
64+
`Only generate ONE ${SEARCH_TOOL_NAME} tool call per user message unless there are clearly multiple distinct queries needed to answer the user query.`,
65+
];
66+
5967
export function makeSearchTool({
6068
findContent,
6169
makeReferences,
6270
}: MakeSearchToolParams): SearchTool {
6371
const searchTool: SearchTool = tool({
6472
inputSchema: MongoDbSearchToolArgsSchema,
65-
description: "Search MongoDB content",
73+
description: `Search MongoDB content. Use the ${SEARCH_TOOL_NAME} tool as follows:
74+
75+
${makeMarkdownNumberedList(searchContentToolNotes)}
76+
77+
When you search, include metadata about the relevant MongoDB programming language and product.`,
6678

6779
toModelOutput(result) {
6880
return {

packages/mongodb-rag-core/src/dataSources/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export * from "./MarkdownUrlDataSource";
1111
export * from "./LangchainDocumentLoaderDataSource";
1212
export * from "./CodeOnGithubTextDataSource";
1313
export * from "./normalizeUrl";
14+
export * from "./makeMarkdownLists";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function makeMarkdownNumberedList(items: string[]) {
2+
return items.map((item, i) => `${i + 1}. ${item}`).join("\n");
3+
}
4+
5+
export function makeMarkdownUnorderedList(items: string[]) {
6+
return items.map((item) => `- ${item}`).join("\n");
7+
}

0 commit comments

Comments
 (0)