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
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
const query = new URL(req.url).searchParams;
const prompt = query.get("prompt");
if (!prompt) {
return new Response("Missing prompt query param in the request", { status: 400 });
}
const openai = new OpenAI();
const functionExpression = await openai.chat.completions.create({
messages: [
{
role: "system",
content:
"You are a helpful assistant that converts the user's prompt into a valid crontab expression. You only reply with valid crontab expressions.",
},
{ role: "user", content: "On Juneteenth every hour" },
{ role: "assistant", content: "0 * 19 6 * *" },
{ role: "user", content: "At midnight on christmas" },
{ role: "assistant", content: "0 0 25 12 * *" },
{ role: "user", content: prompt },
],
model: "gpt-4",
max_tokens: 30,
});
const result = functionExpression.choices[0].message.content;
return Response.json({ result });
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.