Skip to content

Commit 8eed30b

Browse files
authored
feat(gen-ai): handle model limits COMPASS-10129 (#7655)
* handle model limits and throw error with code * copilot feedback
1 parent b24f7f2 commit 8eed30b

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

packages/compass-generative-ai/src/chatbot-errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export class AiChatbotInvalidResponseError extends AtlasServiceError {
66
this.name = 'AiChatbotInvalidResponseError';
77
}
88
}
9+
10+
export class AiChatbotPromptTooLargeError extends AtlasServiceError {
11+
constructor(message: string) {
12+
super('NetworkError', 400, message, 'PROMPT_TOO_LARGE');
13+
this.name = 'AiChatbotPromptTooLargeError';
14+
}
15+
}

packages/compass-generative-ai/src/utils/gen-ai-prompt.spec.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from './gen-ai-prompt';
88
import { toJSString } from 'mongodb-query-parser';
99
import { ObjectId } from 'bson';
10+
import { AiChatbotPromptTooLargeError } from '../chatbot-errors';
1011

1112
const OPTIONS: PromptContextOptions = {
1213
userInput: 'Find all users older than 30',
@@ -149,13 +150,13 @@ describe('GenAI Prompts', function () {
149150
try {
150151
buildFindQueryPrompt({
151152
...OPTIONS,
152-
userInput: 'a'.repeat(512001),
153+
userInput: 'a'.repeat(250_001),
153154
});
154155
expect.fail('Expected buildFindQueryPrompt to throw');
155156
} catch (err) {
156-
expect(err).to.have.property(
157-
'message',
158-
'Sorry, your request is too large. Please use a smaller prompt or try using this feature on a collection with smaller documents.'
157+
expect(err).to.be.instanceOf(AiChatbotPromptTooLargeError);
158+
expect((err as AiChatbotPromptTooLargeError).message).to.equal(
159+
'PROMPT_TOO_LARGE: Sorry, your request is too large. Please use a smaller prompt or try using this feature on a collection with smaller documents.'
159160
);
160161
}
161162
});
@@ -178,9 +179,9 @@ describe('GenAI Prompts', function () {
178179
it('sends only one sample doc if all exceed limits', function () {
179180
const sampleDocuments = [
180181
{ a: '1'.repeat(5120) },
181-
{ a: '2'.repeat(5120001) },
182-
{ a: '3'.repeat(5120001) },
183-
{ a: '4'.repeat(5120001) },
182+
{ a: '2'.repeat(250_001) },
183+
{ a: '3'.repeat(250_001) },
184+
{ a: '4'.repeat(250_001) },
184185
];
185186
const prompt = buildFindQueryPrompt({
186187
...OPTIONS,
@@ -190,10 +191,10 @@ describe('GenAI Prompts', function () {
190191
});
191192
it('should not send sample docs if even one exceeds limits', function () {
192193
const sampleDocuments = [
193-
{ a: '1'.repeat(5120001) },
194-
{ a: '2'.repeat(5120001) },
195-
{ a: '3'.repeat(5120001) },
196-
{ a: '4'.repeat(5120001) },
194+
{ a: '1'.repeat(250_001) },
195+
{ a: '2'.repeat(250_001) },
196+
{ a: '3'.repeat(250_001) },
197+
{ a: '4'.repeat(250_001) },
197198
];
198199
const prompt = buildFindQueryPrompt({
199200
...OPTIONS,

packages/compass-generative-ai/src/utils/gen-ai-prompt.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { toJSString } from 'mongodb-query-parser';
22
import { flattenSchemaToObject } from './util';
3+
import { AiChatbotPromptTooLargeError } from '../chatbot-errors';
34

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

129
function getCurrentTimeString() {
@@ -165,7 +162,7 @@ function buildUserPromptForQuery({
165162

166163
// If at this point we have exceeded the limit, throw an error.
167164
if (prompt.length > MAX_TOTAL_PROMPT_LENGTH) {
168-
throw new Error(
165+
throw new AiChatbotPromptTooLargeError(
169166
'Sorry, your request is too large. Please use a smaller prompt or try using this feature on a collection with smaller documents.'
170167
);
171168
}

0 commit comments

Comments
 (0)