import { ai } from "https://esm.town/v/yawnxyz/ai";
export default async (req) => {
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
const textEncoder = new TextEncoder();
const url = new URL(req.url);
const prompt = url.searchParams.get("prompt") || "tell me a joke";
if (!prompt || prompt.trim() === "") {
return new Response("Please provide a prompt in the URL query.", {
headers: {
"Content-Type": "text/plain",
},
status: 400,
});
}
const result = await ai(prompt, {
provider: "groq",
streaming: true,
max_tokens: 1024
})
new Promise(async () => {
for await (const textPart of result.textStream) {
console.log('text:', textPart);
writer.write(textEncoder.encode(textPart))
}
writer.close();
});
return new Response(readable, {
headers: {
"Content-Type": "text/html",
},
});
};