Skip to content

Commit e148551

Browse files
committed
feat: limit message context sent to llm
1 parent 3350cda commit e148551

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

apps/postgres-new/app/api/chat/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { openai } from '@ai-sdk/openai'
22
import { ToolInvocation, convertToCoreMessages, streamText } from 'ai'
33
import { codeBlock } from 'common-tags'
4-
import { convertToCoreTools, tools } from '~/lib/tools'
4+
import { convertToCoreTools, maxMessageContext, tools } from '~/lib/tools'
55

66
// Allow streaming responses up to 30 seconds
77
export const maxDuration = 30
@@ -15,6 +15,9 @@ type Message = {
1515
export async function POST(req: Request) {
1616
const { messages }: { messages: Message[] } = await req.json()
1717

18+
// Trim the message context sent to the LLM to mitigate token abuse
19+
const trimmedMessageContext = messages.slice(-maxMessageContext)
20+
1821
const result = await streamText({
1922
system: codeBlock`
2023
You are a helpful database assistant. Under the hood you have access to an in-browser Postgres database called PGlite (https://github.com/electric-sql/pglite).
@@ -58,7 +61,7 @@ export async function POST(req: Request) {
5861
Feel free to suggest corrections for suspected typos.
5962
`,
6063
model: openai('gpt-4o-2024-08-06'),
61-
messages: convertToCoreMessages(messages),
64+
messages: convertToCoreMessages(trimmedMessageContext),
6265
tools: convertToCoreTools(tools),
6366
})
6467

apps/postgres-new/lib/tools.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ function result<T extends z.ZodTypeAny>(schema: T) {
1616
return z.union([z.intersection(successResultSchema, schema), errorResultSchema])
1717
}
1818

19+
/**
20+
* The maximum number of messages from the chat history to send to the LLM.
21+
*/
22+
export const maxMessageContext = 30
23+
1924
/**
2025
* Central location for all LLM tools including their
2126
* description, arg schema, and result schema.

0 commit comments

Comments
 (0)