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
import OpenAI from "npm:openai";
const openai = new OpenAI();
export default async (req) => {
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
const textEncoder = new TextEncoder();
// Get the prompt from the URL query
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,
});
}
// Generate the AI response
const stream = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{
role: "system",
content: "You are a helpful assistant that provides detailed and informative responses to user prompts.",
}, {
role: "user",
content: prompt.trim(),
}],
max_tokens: 1024,
stream: true,
});
new Promise(async () => {
// Stream the AI response
for await (const part of stream) {
const text = part.choices[0]?.delta?.content || "";
console.log('text::', text)
writer.write(textEncoder.encode(text)); // Remove 'await' here
}
writer.close(); // This can be awaited if needed
});
// Send the readable stream back to the client
return new Response(readable, {
headers: {
"Content-Type": "text/html",
},
});
};