Skip to content

Commit

Permalink
Merge pull request #716 from tone-row/dev
Browse files Browse the repository at this point in the history
v1.56.0
  • Loading branch information
rob-gordon authored Aug 16, 2024
2 parents b63f34f + c068af2 commit d4b4c21
Show file tree
Hide file tree
Showing 83 changed files with 1,634 additions and 978 deletions.
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

0 comments on commit d4b4c21

Please sign in to comment.