cotr-launch_thrifty_idea_generator.web.val.run
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
export default async function(req: Request): Promise<Response> {
if (req.method === "OPTIONS") {
return new Response("ok");
}
const searchParams = new URL(req.url).searchParams;
console.log(searchParams);
const topic = searchParams.get("topic");
console.log(topic);
try {
const apiKey = Deno.env.get("GEMINI_API_KEY");
const url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent";
const headers = {
"Content-Type": "application/json",
};
const data = {
contents: [
{
parts: [{
text: `Give me a business idea ${
topic ? "related to " + topic : ""
} that I can monetize with as little code and monetary investment as possible. It should be primarily online and require no manual labor. Use this JSON schema: \{\"type\":\"object\",\"properties\":\{\"name\":\{\"type\":\"string\"\},\"descriptio
}],
},
],
generationConfig: {
response_mime_type: "application/json",
},
};
const response = await fetch(`${url}?key=${apiKey}`, {
method: "POST",
headers,
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const responseData = (await response.json())["candidates"][0]["content"][
"parts"
][0]["text"];
return new Response(JSON.stringify(responseData), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error generating AI completion:", error);
return new Response(
JSON.stringify({ error: "Error generating AI completion" }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 14, 2024