-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bookerlyio
committed
Nov 13, 2024
1 parent
14b6a42
commit 032d480
Showing
2 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,57 @@ | ||
import { anthropic } from "@ai-sdk/anthropic"; | ||
import { streamText } from "ai"; | ||
import { Ratelimit } from "@upstash/ratelimit"; | ||
import { Redis } from "@upstash/redis"; | ||
|
||
// Allow streaming responses up to 30 seconds | ||
export const maxDuration = 30; | ||
const redis = Redis.fromEnv(); | ||
|
||
const ratelimit = new Ratelimit({ | ||
redis: redis, | ||
limiter: Ratelimit.slidingWindow(100, "1 d"), // 100 messages / 1 d | ||
}); | ||
|
||
export async function POST(req: Request) { | ||
const { messages } = await req.json(); | ||
const ip = req.headers.get("x-forwarded-for") ?? "127.0.0.1"; | ||
|
||
// Check if the IP is blocked | ||
const isBlocked = await redis.get(ip); | ||
if (isBlocked) { | ||
return new Response( | ||
"You have reached the message limit for today. Install me, use your own API key, and enjoy!", | ||
{ status: 429 } | ||
); | ||
} | ||
|
||
try { | ||
const { success } = await ratelimit.limit(ip); | ||
if (!success) { | ||
await redis.set(ip, "blocked", { ex: 86400 }); // 1 d | ||
|
||
return new Response( | ||
"You have reached the message limit for today. Install me, use your own API key, and enjoy!", | ||
{ status: 429 } | ||
); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
return new Response("An error occurred while processing your request.", { | ||
status: 500, | ||
}); | ||
} | ||
|
||
const { messages, systemPrompt, model } = await req.json(); | ||
|
||
const result = await streamText({ | ||
model: anthropic("claude-3-5-haiku-20241022"), | ||
system: `You are a chatbot AI assistant. You must: | ||
const botResponse = await streamText({ | ||
model: model ? anthropic(model) : anthropic("claude-3-5-haiku-20241022"), | ||
system: | ||
systemPrompt || | ||
`You are a chatbot AI assistant. You must: | ||
- Politely decline to discuss any topics outside of our services. | ||
- Maintain a friendly, professional tone. | ||
- Keep responses concise and focused on solving customer inquiries. | ||
- Keep responses to 20 words or less, but go to up to a maximum of 50 words if you are explaining something or need to in order to answer a query.`, | ||
messages, | ||
}); | ||
|
||
return result.toDataStreamResponse(); | ||
return botResponse.toDataStreamResponse(); | ||
} |