File tree Expand file tree Collapse file tree 3 files changed +20
-4
lines changed Expand file tree Collapse file tree 3 files changed +20
-4
lines changed Original file line number Diff line number Diff line change 1
1
import { openai } from '@ai-sdk/openai'
2
2
import { ToolInvocation , convertToCoreMessages , streamText } from 'ai'
3
3
import { codeBlock } from 'common-tags'
4
- import { convertToCoreTools , maxMessageContext , tools } from '~/lib/tools'
4
+ import { convertToCoreTools , maxMessageContext , maxRowLimit , tools } from '~/lib/tools'
5
5
6
6
// Allow streaming responses up to 30 seconds
7
7
export const maxDuration = 30
@@ -42,7 +42,8 @@ export async function POST(req: Request) {
42
42
- Make the data realistic, including joined data
43
43
- Check for existing records/conflicts in the table
44
44
45
- When querying data, limit to 5 by default.
45
+ When querying data, limit to 5 by default. The maximum number of rows you're allowed to fetch is ${ maxRowLimit } (to protect AI from token abuse).
46
+ If the user needs to fetch more than ${ maxRowLimit } rows at once, they can export the query as a CSV.
46
47
47
48
When performing FTS, always use 'simple' (languages aren't available).
48
49
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ import { useTablesQuery } from '~/data/tables/tables-query'
20
20
import { embed } from './embed'
21
21
import { loadFile , saveFile } from './files'
22
22
import { SmoothScroller } from './smooth-scroller'
23
- import { OnToolCall } from './tools'
23
+ import { maxRowLimit , OnToolCall } from './tools'
24
24
25
25
export function useDebounce < T > ( value : T , delay : number ) {
26
26
const [ debouncedValue , setDebouncedValue ] = useState ( value )
@@ -265,7 +265,17 @@ export function useOnToolCall(databaseId: string) {
265
265
266
266
const results = await db . exec ( sql )
267
267
268
- // Truncate vector columns due to their large size
268
+ const oversizedResult = results . find ( ( result ) => result . rows . length > maxRowLimit )
269
+
270
+ // We have a max row count in place to mitigate LLM token abuse
271
+ if ( oversizedResult ) {
272
+ return {
273
+ success : false ,
274
+ error : `Query produced ${ oversizedResult . rows . length } rows but the max allowed limit is ${ maxRowLimit } . Rerun the query with a limit of ${ maxRowLimit } .` ,
275
+ }
276
+ }
277
+
278
+ // Truncate vector columns due to their large size (display purposes only)
269
279
const filteredResults = results . map ( ( result ) => {
270
280
const vectorFields = result . fields . filter (
271
281
( field ) => field . dataTypeID === vectorDataTypeId
Original file line number Diff line number Diff line change @@ -16,6 +16,11 @@ function result<T extends z.ZodTypeAny>(schema: T) {
16
16
return z . union ( [ z . intersection ( successResultSchema , schema ) , errorResultSchema ] )
17
17
}
18
18
19
+ /**
20
+ * The maximum SQL result row limit to prevent overloading LLM.
21
+ */
22
+ export const maxRowLimit = 100
23
+
19
24
/**
20
25
* The maximum number of messages from the chat history to send to the LLM.
21
26
*/
You can’t perform that action at this time.
0 commit comments