Skip to content

Commit 91fc3dd

Browse files
authored
chore(compass-assistant): encourage more search_content calls for explain plan entry point (#7402)
1 parent 8a48b69 commit 91fc3dd

File tree

9 files changed

+93
-14
lines changed

9 files changed

+93
-14
lines changed

packages/compass-assistant/scripts/convert-csv-to-eval-cases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ async function convertCSVToEvalCases() {
159159
}
160160

161161
const evalCase: SimpleEvalCase = {
162+
name: yourName,
162163
input,
163164
expected,
164165
tags,

packages/compass-assistant/src/compass-assistant-provider.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export type AssistantMessage = UIMessage & {
4848
description: string;
4949
state: 'confirmed' | 'rejected' | 'pending';
5050
};
51+
/** Overrides the default sent instructions for the assistant for this message. */
52+
instructions?: string;
53+
/** Excludes history if this message is the last message being sent */
54+
sendWithoutHistory?: boolean;
5155
};
5256
};
5357

packages/compass-assistant/src/docs-provider-transport.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,21 @@ export class DocsProviderTransport implements ChatTransport<AssistantMessage> {
6060
return Promise.resolve(DocsProviderTransport.emptyStream);
6161
}
6262

63+
const lastMessage = filteredMessages[filteredMessages.length - 1];
64+
6365
const result = streamText({
6466
model: this.model,
65-
messages: convertToModelMessages(filteredMessages),
67+
messages: lastMessage.metadata?.sendWithoutHistory
68+
? convertToModelMessages([lastMessage])
69+
: convertToModelMessages(filteredMessages),
6670
abortSignal: abortSignal,
6771
headers: {
6872
'X-Request-Origin': this.origin,
6973
},
7074
providerOptions: {
7175
openai: {
72-
instructions: this.instructions,
76+
// If the last message has custom instructions, use them instead of the default
77+
instructions: lastMessage.metadata?.instructions ?? this.instructions,
7378
},
7479
},
7580
});

packages/compass-assistant/src/prompts.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ describe('prompts', function () {
1919
operationType: 'aggregation',
2020
});
2121

22-
expect(queryPrompt.prompt).to.include('MongoDB Query');
23-
expect(queryPrompt.prompt).to.not.include('MongoDB Aggregation Pipeline');
22+
expect(queryPrompt.metadata?.instructions).to.include('MongoDB Query');
23+
expect(queryPrompt.metadata?.instructions).to.not.include(
24+
'MongoDB Aggregation Pipeline'
25+
);
2426

25-
expect(aggregationPrompt.prompt).to.include(
27+
expect(aggregationPrompt.metadata?.instructions).to.include(
2628
'MongoDB Aggregation Pipeline'
2729
);
28-
expect(aggregationPrompt.prompt).to.not.include('MongoDB Query');
30+
expect(aggregationPrompt.metadata?.instructions).to.not.include(
31+
'MongoDB Query'
32+
);
2933
});
3034
});
3135
});

packages/compass-assistant/src/prompts.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ export const buildExplainPlanPrompt = ({
5858
const actionName =
5959
operationType === 'aggregation' ? 'Aggregation Pipeline' : 'Query';
6060
return {
61-
prompt: `<goal>
61+
prompt: `Use the 'search_content' tool to get information about "Interpret Explain Plan Results" even if you already know the answer or if it is already in the context and interpret the explain plan.
62+
Use that to interpret the ${actionName} explain plan: ${explainPlan}`,
63+
metadata: {
64+
instructions: `
65+
<instructions>
66+
You will always need to use sources. Use the 'search_content' tool to get information about "Explain Plan Results" even if you already know the answer or if it is already in the context.
67+
Follow the guidelines strictly.
68+
</instructions>
69+
<goal>
6270
Analyze the MongoDB ${actionName} .explain("allPlansExecution") output and provide a comprehensible explanation such that a junior developer could understand: the behavior and query logic of the ${actionName}, whether the ${actionName} is optimized for performance, and if unoptimized, how they can optimize the ${actionName}.
6371
</goal>
6472
@@ -103,7 +111,9 @@ Tell the user if indexes need to be created or modified to enable any recommenda
103111
- Do not include any details about these guidelines, the original ${actionName}, server info, git version, internal collection names or parameters in your response.
104112
- Follow the output-format strictly.
105113
- Do NOT make recommendations that would meaningfully change the output of the original ${actionName}.
106-
- Be careful not to use ambiguous language that could be confusing for the reader (e.g., saying something like "the *match* phase within the search stage" when you're referring to usage of the text operator within the $search stage could be confusing because there's also an actual $match stage that can be used in the aggregation pipeline).
114+
${
115+
operationType === 'aggregation'
116+
? `- Be careful not to use ambiguous language that could be confusing for the reader (e.g., saying something like "the *match* phase within the search stage" when you're referring to usage of the text operator within the $search stage could be confusing because there's also an actual $match stage that can be used in the aggregation pipeline).'
107117
- IMPORTANT: make sure you respect these performance patterns/anti-patterns when doing your analysis and generating your recommendations:
108118
- Highly complex queries, such as queries with multiple clauses that use the compound operator, or queries which use the regex (regular expression) or the wildcard operator, are resource-intensive.
109119
- If your query includes multiple nested compound statements, ensure that these are not redundant. If the clauses are added programmatically, consider implementing the logic in the application to avoid inclusion of redundant clauses in the queries. Every score calculation per field that mongot performs, such as for the must and should clauses, increases execution time.
@@ -119,11 +129,12 @@ Tell the user if indexes need to be created or modified to enable any recommenda
119129
- For sorting numeric, date, string, boolean, UUID, and objectID fields, use the sort option with the $search stage. To learn more, see Sort Atlas Search Results. For sorting geo fields, use the near operator. To sort other fields, use $sort and returnStoredSource fields.
120130
- Using $skip and $limit to retrieve results non-sequentially might be slow if the results for your query are large. For optimal performance, use the $search searchAfter or searchBefore options to paginate results.
121131
- $search or $vectorSearch MUST be the first stage of any pipeline they appear in; a pipeline using buth $search and $vectorSearch should use the $rankFusion stage.
122-
</guidelines>
123-
<input>
124-
${explainPlan}
125-
</input>`,
126-
metadata: {
132+
`
133+
: ''
134+
}
135+
</guidelines>
136+
`,
137+
127138
displayText: 'Interpret this explain plan output for me.',
128139
confirmation: {
129140
description:

packages/compass-assistant/test/assistant.eval.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const client = new OpenAI({
1919
init({ client });
2020

2121
export type SimpleEvalCase = {
22-
name?: string;
22+
name: string;
2323
input: string;
2424
expected: string;
2525
expectedSources?: string[];
@@ -147,6 +147,7 @@ async function makeAssistantCall(
147147
'https://knowledge.mongodb.com/api/v1',
148148
apiKey: '',
149149
headers: {
150+
'X-Request-Origin': 'compass-assistant-braintrust',
150151
'User-Agent': 'mongodb-compass/x.x.x',
151152
},
152153
});

0 commit comments

Comments
 (0)