-
-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathchoose-template.ts
More file actions
51 lines (40 loc) · 1.4 KB
/
choose-template.ts
File metadata and controls
51 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { z } from "zod";
import { templateRateLimit } from "./_shared";
import { generateText, Output } from "ai";
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 generateText({
model: "openai/gpt-oss-120b",
output: Output.object({ schema }),
prompt: getContent(parsed.data.prompt),
system: systemMessage,
providerOptions: {
gateway: {
order: ["cerebras"],
},
},
});
const templateChoice = schema.parse(result.output);
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}'''`;
}