Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export let convertTodoItemsToGamePlans = async (
req: express.Request,
res: express.Response,
) => {
let task = req.body?.toDoItems;
if (!task) {
res.json({
status: 400,
message: "No to-do items were submitted",
});
}
let PROMPT = `
OK, let's plan my day!
My task remaining is:
"""
${task}
""""
Break the task into a journey like a game so that I can do all the task in an exciting manner and get reward after each small task is done and big reward for a huge task.
The response should be a JSON array
with each JSON object contains
a field called 'questName' that contains the title of the quest (a fun name for the adventure),
a field called 'quest' that contains a JSON array of the steps that I will have to go through the complete the quest. (break the quest down to at least 5 actionable tasks to get done),
and a field called 'reward' that contains the text of the reward (a reward should be something fun and tangible, like "dance 5 minutes" or "get a cup of coffee")
Always return in this format and do not deviate. Do not include any newline characters.`;
let rawBody = JSON.stringify({
"messages": [{ "role": "system", "content": PROMPT }],
"max_tokens": 1500,
"model": "gpt-3.5-turbo",
});
let openApiKey = process.env.OPEN_API_KEY;
var requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${openApiKey}`,
},
body: rawBody,
redirect: "follow",
};
let openApiResponse = await fetch(
"https://api.openai.com/v1/chat/completions",
requestOptions,
);
let jsonResponse = await openApiResponse.json();
let choice = jsonResponse?.choices[0]?.message.content;
if (!choice) {
res.json({
status: 400,
message: "Something went wrong!",
});
}
let finalResponse = choice.replace(/\n/g, "");
const { createClient } = await import(
"https://esm.sh/@supabase/supabase-js@2"
);
const supabase = createClient(
process.env.supabaseURL,
process.env.supabaseKey,
);
const { data, error } = await supabase
.from("daily_quests_usage")
.insert({ prompt: task, response: finalResponse });
if (error) {
throw error;
}
res.status(200).json(finalResponse);
};
thomasatflexos-converttodoitemstogameplans.express.val.run
October 23, 2023