Skip to content

Commit

Permalink
fix: add route.ts for default theme
Browse files Browse the repository at this point in the history
  • Loading branch information
bookerlyio committed Nov 13, 2024
1 parent 14b6a42 commit 032d480
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion public/r/styles/new-york/chatbot.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"path": "api/chat/route.ts",
"content": "import { anthropic } from \"@ai-sdk/anthropic\";\r\nimport { streamText } from \"ai\";\r\n\r\n// Allow streaming responses up to 30 seconds\r\nexport const maxDuration = 30;\r\n\r\nexport async function POST(req: Request) {\r\n const { messages } = await req.json();\r\n\r\n const result = await streamText({\r\n model: anthropic(\"claude-3-5-haiku-20241022\"),\r\n system: `You are a chatbot AI assistant. You must:\r\n- Politely decline to discuss any topics outside of our services.\r\n- Maintain a friendly, professional tone.\r\n- Keep responses concise and focused on solving customer inquiries.\r\n- 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.`,\r\n messages,\r\n });\r\n\r\n return result.toDataStreamResponse();\r\n}\r\n",
"content": "import { anthropic } from \"@ai-sdk/anthropic\";\r\nimport { streamText } from \"ai\";\r\nimport { Ratelimit } from \"@upstash/ratelimit\";\r\nimport { Redis } from \"@upstash/redis\";\r\n\r\nconst redis = Redis.fromEnv();\r\n\r\nconst ratelimit = new Ratelimit({\r\n redis: redis,\r\n limiter: Ratelimit.slidingWindow(100, \"1 d\"), // 100 messages / 1 d\r\n});\r\n\r\nexport async function POST(req: Request) {\r\n const ip = req.headers.get(\"x-forwarded-for\") ?? \"127.0.0.1\";\r\n\r\n // Check if the IP is blocked\r\n const isBlocked = await redis.get(ip);\r\n if (isBlocked) {\r\n return new Response(\r\n \"You have reached the message limit for today. Install me, use your own API key, and enjoy!\",\r\n { status: 429 }\r\n );\r\n }\r\n\r\n try {\r\n const { success } = await ratelimit.limit(ip);\r\n if (!success) {\r\n await redis.set(ip, \"blocked\", { ex: 86400 }); // 1 d\r\n\r\n return new Response(\r\n \"You have reached the message limit for today. Install me, use your own API key, and enjoy!\",\r\n { status: 429 }\r\n );\r\n }\r\n } catch (error) {\r\n console.error(error);\r\n return new Response(\"An error occurred while processing your request.\", {\r\n status: 500,\r\n });\r\n }\r\n\r\n const { messages, systemPrompt, model } = await req.json();\r\n\r\n const botResponse = await streamText({\r\n model: model ? anthropic(model) : anthropic(\"claude-3-5-haiku-20241022\"),\r\n system:\r\n systemPrompt ||\r\n `You are a chatbot AI assistant. You must:\r\n- Politely decline to discuss any topics outside of our services.\r\n- Maintain a friendly, professional tone.\r\n- Keep responses concise and focused on solving customer inquiries.\r\n- 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.`,\r\n messages,\r\n });\r\n\r\n return botResponse.toDataStreamResponse();\r\n}\r\n",
"type": "registry:page",
"target": "app/api/chat/route.ts"
},
Expand Down
50 changes: 43 additions & 7 deletions registry/new-york/api/chat/route.ts
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();
}

0 comments on commit 032d480

Please sign in to comment.