Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.56.0 #716

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ jobs:
version: 8
- name: Install Dependencies
run: pnpm install
- name: Build shared
run: pnpm -F shared build
- name: Test
run: pnpm -F app test:coverage
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.12.1
v18.17.1
4 changes: 2 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"author": "",
"license": "ISC",
"dependencies": {
"@ai-sdk/openai": "^0.0.37",
"@ai-sdk/openai": "^0.0.45",
"@notionhq/client": "^0.4.13",
"@octokit/core": "^4.2.0",
"@sendgrid/mail": "^7.4.6",
"@supabase/supabase-js": "^2.31.0",
"@upstash/ratelimit": "^1.1.3",
"@vercel/kv": "^1.0.1",
"ai": "^3.2.32",
"ai": "^3.3.6",
"ajv": "^8.12.0",
"axios": "^0.27.2",
"csv-parse": "^5.3.6",
Expand Down
29 changes: 29 additions & 0 deletions api/prompt/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ export async function handleRateLimit(req: Request) {
return null;
}

/**
* Rate limit for template requests
*/
export async function templateRateLimit(req: Request) {
const ip = getIp(req);

const ratelimit = new Ratelimit({
redis: kv,
limiter: Ratelimit.slidingWindow(3, "1m"),
});
const rateLimitKey = `template_${ip}`;
const { success, limit, reset, remaining } = await ratelimit.limit(
rateLimitKey
);

if (!success) {
return new Response("You have reached your request limit for templates.", {
status: 429,
headers: {
"X-RateLimit-Limit": limit.toString(),
"X-RateLimit-Remaining": remaining.toString(),
"X-RateLimit-Reset": reset.toString(),
},
});
}

return null;
}

export async function processRequest(
req: Request,
systemMessage: string,
Expand Down
48 changes: 48 additions & 0 deletions api/prompt/choose-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { z } from "zod";
import { templateRateLimit } from "./_shared";
import { generateObject } from "ai";
import { openai } from "@ai-sdk/openai";
import { templates } from "shared";

const schema = z.object({
template: z.enum(templates),
});

const reqSchema = z.object({
prompt: z.string(),
});

export const config = {
runtime: "edge",
};

const systemMessage = `You're the Flowchart Fun Template Picker. From the list of templates, find the most interesting one to use for the user's prompt. Avoid using default.`;

export default async function handler(req: Request) {
const rateLimitResponse = await templateRateLimit(req);
if (rateLimitResponse) return rateLimitResponse;

const parsed = reqSchema.safeParse(await req.json());

if (!parsed.success) {
return new Response(JSON.stringify(parsed.error), { status: 400 });
}

const result = await generateObject({
model: openai("gpt-3.5-turbo"),
schema,
prompt: getContent(parsed.data.prompt),
system: systemMessage,
});

// Parse the result to ensure it matches one of the template options
const templateChoice = schema.parse(result.object);

return new Response(JSON.stringify({ template: templateChoice.template }), {
headers: { "Content-Type": "application/json" },
});
}

function getContent(prompt: string): string {
return `Which template would you like to use for the following prompt?\n\n'''${prompt}'''`;
}
Loading
Loading