Skip to content

Commit 918fe7e

Browse files
committed
Merge branch 'main' into next
2 parents 693bd73 + 2480217 commit 918fe7e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

apps/web/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, maxRowLimit, tools } from '~/lib/tools'
4+
import { convertToCoreTools, maxMessageContext, maxRowLimit, 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).
@@ -59,7 +62,7 @@ export async function POST(req: Request) {
5962
Feel free to suggest corrections for suspected typos.
6063
`,
6164
model: openai('gpt-4o-2024-08-06'),
62-
messages: convertToCoreMessages(messages),
65+
messages: convertToCoreMessages(trimmedMessageContext),
6366
tools: convertToCoreTools(tools),
6467
})
6568

apps/web/lib/tools.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ function result<T extends z.ZodTypeAny>(schema: T) {
2121
*/
2222
export const maxRowLimit = 100
2323

24+
/**
25+
* The maximum number of messages from the chat history to send to the LLM.
26+
*/
27+
export const maxMessageContext = 30
28+
2429
/**
2530
* Central location for all LLM tools including their
2631
* description, arg schema, and result schema.

0 commit comments

Comments
 (0)