Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
1295d51
add feature flag
mabaasit Nov 25, 2025
7a44de4
migrate prompts to compass
mabaasit Nov 25, 2025
2e2defe
Merge branch 'main' into COMPASS-10082-add-mms-prompts-and-feature-flag
mabaasit Nov 25, 2025
d524682
co-pilot feedback
mabaasit Nov 25, 2025
f9212fb
Merge branch 'COMPASS-10082-add-mms-prompts-and-feature-flag' of http…
mabaasit Nov 25, 2025
c96161b
clean up
mabaasit Dec 1, 2025
f499026
Merge branch 'main' into COMPASS-10082-add-mms-prompts-and-feature-flag
mabaasit Dec 1, 2025
f4d05a7
use edu api for gen ai
mabaasit Dec 1, 2025
1763531
clean up a bit
mabaasit Dec 1, 2025
d5e2c84
fix url for test and ensure aggregations have content
mabaasit Dec 2, 2025
0781580
tests
mabaasit Dec 3, 2025
058e950
fix error handling
mabaasit Dec 3, 2025
7d54d4d
clean up transport
mabaasit Dec 3, 2025
0aa7ff5
Merge branch 'main' of https://github.com/mongodb-js/compass into COM…
mabaasit Dec 3, 2025
309335a
changes in field name
mabaasit Dec 3, 2025
feacddc
use query parser
mabaasit Dec 3, 2025
c321983
fix check
mabaasit Dec 3, 2025
f5a34df
fix test
mabaasit Dec 3, 2025
91b17d5
clean up
mabaasit Dec 3, 2025
ea4dfdf
copilot feedback
mabaasit Dec 3, 2025
f8a4c43
fix log id
mabaasit Dec 3, 2025
8190219
fix cors issue on e2e tests
mabaasit Dec 4, 2025
c444cb9
more tests
mabaasit Dec 4, 2025
6eec8db
add type
mabaasit Dec 4, 2025
25c8089
wip
mabaasit Dec 8, 2025
9b4c9b7
fix alltext
mabaasit Dec 8, 2025
5e6d537
make expected output xml string
mabaasit Dec 9, 2025
d1b0b51
add fixtures and run gen ai eval tests
mabaasit Dec 10, 2025
1b7343f
ts fixes
mabaasit Dec 10, 2025
a1b38c5
reformat
mabaasit Dec 10, 2025
79e9910
clean up scorer
mabaasit Dec 10, 2025
821917d
Merge branch 'main' of https://github.com/mongodb-js/compass into eva…
mabaasit Dec 11, 2025
3bc7295
bootstrap
mabaasit Dec 11, 2025
eb49493
remove extra files
mabaasit Dec 11, 2025
fa1cf6f
Merge branch 'main' of https://github.com/mongodb-js/compass into eva…
mabaasit Dec 15, 2025
edc5e73
bootstrap
mabaasit Dec 15, 2025
718e8b7
fix prompts and data
mabaasit Dec 15, 2025
5244598
Merge branch 'main' of https://github.com/mongodb-js/compass into eva…
mabaasit Dec 15, 2025
534b0d8
copilot review
mabaasit Dec 15, 2025
d0568ff
reduce num of docs
mabaasit Dec 16, 2025
40ad7c9
reduce fixtures
mabaasit Dec 16, 2025
48b38ce
Merge branch 'main' of https://github.com/mongodb-js/compass into eva…
mabaasit Dec 16, 2025
6153f52
check fix
mabaasit Dec 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
…PASS-10081-switch-to-edu-api
  • Loading branch information
mabaasit committed Dec 3, 2025
commit 0aa7ff5fc1482f035dcbf2b42a19e9399d844c05
74 changes: 54 additions & 20 deletions packages/compass-generative-ai/src/utils/gen-ai-prompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { toJSString } from 'mongodb-query-parser';

// When including sample documents, we want to ensure that we do not
// attach large documents and exceed the limit. OpenAI roughly estimates
// 4 characters = 1 token and we should not exceed context window limits.
// This roughly translates to 128k tokens.
// TODO(COMPASS-10129): Adjust this limit based on the model's context window.
const MAX_TOTAL_PROMPT_LENGTH = 512000;
const MIN_SAMPLE_DOCUMENTS = 1;

Expand Down Expand Up @@ -55,16 +58,25 @@ function buildInstructionsForAggregateQuery() {
}

export type UserPromptForQueryOptions = {
userInput: string;
userPrompt: string;
databaseName?: string;
collectionName?: string;
schema?: unknown;
sampleDocuments?: unknown[];
};

function withCodeFence(code: string): string {
return [
'', // Line break
'```',
code,
'```',
].join('\n');
}

function buildUserPromptForQuery({
type,
userInput,
userPrompt,
databaseName,
collectionName,
schema,
Expand All @@ -75,7 +87,7 @@ function buildUserPromptForQuery({
const queryPrompt = [
type === 'find' ? 'Write a query' : 'Generate an aggregation',
'that does the following:',
`"${userInput}"`,
`"${userPrompt}"`,
].join(' ');

if (databaseName) {
Expand All @@ -86,35 +98,41 @@ function buildUserPromptForQuery({
}
if (schema) {
messages.push(
'Schema from a sample of documents from the collection: ```' +
JSON.stringify(schema) +
'```'
`Schema from a sample of documents from the collection:${withCodeFence(
toJSString(schema)!
)}`
);
}
if (sampleDocuments) {
// When attaching the sample documents, we want to ensure that we do not
// exceed the token limit. So we try following:
// 1. If attaching all the sample documents exceeds then limit, we attach only 1 document.
// 2. If attaching 1 document still exceeds the limit, we do not attach any sample documents.
const sampleDocumentsStr = JSON.stringify(sampleDocuments);
const singleDocumentStr = JSON.stringify(
const sampleDocumentsStr = toJSString(sampleDocuments);
const singleDocumentStr = toJSString(
sampleDocuments.slice(0, MIN_SAMPLE_DOCUMENTS)
);
const promptLengthWithoutSampleDocs =
messages.join('\n').length + queryPrompt.length;
if (
sampleDocumentsStr &&
sampleDocumentsStr.length + promptLengthWithoutSampleDocs <=
MAX_TOTAL_PROMPT_LENGTH
MAX_TOTAL_PROMPT_LENGTH
) {
messages.push(
'Sample documents from the collection: ```' + sampleDocumentsStr + '```'
`Sample documents from the collection:${withCodeFence(
sampleDocumentsStr
)}`
);
} else if (
singleDocumentStr &&
singleDocumentStr.length + promptLengthWithoutSampleDocs <=
MAX_TOTAL_PROMPT_LENGTH
MAX_TOTAL_PROMPT_LENGTH
) {
messages.push(
'Sample document from the collection: ```' + singleDocumentStr + '```'
`Sample document from the collection:${withCodeFence(
singleDocumentStr
)}`
);
}
}
Expand All @@ -129,12 +147,20 @@ export type AiQueryPrompt = {
};
};

export function buildFindQueryPrompt(
options: UserPromptForQueryOptions
): AiQueryPrompt {
export function buildFindQueryPrompt({
userPrompt,
databaseName,
collectionName,
schema,
sampleDocuments,
}: UserPromptForQueryOptions): AiQueryPrompt {
const prompt = buildUserPromptForQuery({
type: 'find',
...options,
userPrompt,
databaseName,
collectionName,
schema,
sampleDocuments,
});
const instructions = buildInstructionsForFindQuery();
return {
Expand All @@ -145,12 +171,20 @@ export function buildFindQueryPrompt(
};
}

export function buildAggregateQueryPrompt(
options: UserPromptForQueryOptions
): AiQueryPrompt {
export function buildAggregateQueryPrompt({
userPrompt,
databaseName,
collectionName,
schema,
sampleDocuments,
}: UserPromptForQueryOptions): AiQueryPrompt {
const prompt = buildUserPromptForQuery({
type: 'aggregate',
...options,
userPrompt,
databaseName,
collectionName,
schema,
sampleDocuments,
});
const instructions = buildInstructionsForAggregateQuery();
return {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.