-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-meals.ts
51 lines (43 loc) · 1.32 KB
/
generate-meals.ts
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 type { NextApiRequest, NextApiResponse } from "next";
import { LangtailPrompts } from "langtail";
const { LANGTAIL_API_KEY } = process.env;
if (!LANGTAIL_API_KEY) {
throw new Error("LANGTAIL_API_KEY is not defined.");
}
const generateMealIdeas = async (selectedIngredients: string[]) => {
// Initialize the LangtailPrompts instance
const lt = new LangtailPrompts({
apiKey: LANGTAIL_API_KEY,
});
// Requesting the completion
const completion = await lt.invoke({
prompt: "quickstart",
environment: "staging",
variables: {
selectedIngredients: JSON.stringify(selectedIngredients),
},
});
// Handling the response
if (completion && completion.choices && completion.choices.length > 0) {
return completion.choices[0].message; // Assuming the structure matches your needs
} else {
throw new Error("No meal ideas generated.");
}
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
res.status(405).json({ error: "Method Not Allowed" });
return;
}
const { selectedIngredients } = req.body;
try {
const data = await generateMealIdeas(selectedIngredients);
res.status(200).json(data);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
}