thirdrule/index.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-07-28 15:28:32 +01:00
import index from "./index.html";
export default {
async fetch(request, env, ctx) {
const cacheUrl = new URL(request.url);
// Construct the cache key from the cache URL
const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;
let response = await cache.match(cacheKey);
if (response) {
return response;
}
const url = new URL(request.url);
const { pathname } = url;
switch (pathname) {
case "/":
return new Response(index, {
headers: { "Content-Type": "text/html" }
});
case "/rule.json":
const simple = {
prompt: 'Give me a rule for a Discord server that is surreal, bizzare, and outlandish while still being somewhat realistic on first glance. Keep it short and succinct. Do not add any additional information, greetings, or quotation marks.'
};
const reply = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', simple);
let response = new Response(JSON.stringify(reply), {
headers: { "Content-Type": "application/json",
"Cache-Control": "s-maxage=43200" }
});
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
default:
return new Response("not found", {status: 404});
}
}
};