Skip to content

Instantly share code, notes, and snippets.

@dankolesnikov
Last active April 5, 2023 02:09
Show Gist options
  • Save dankolesnikov/433e35e0718534720ea618101eed93bc to your computer and use it in GitHub Desktop.
Save dankolesnikov/433e35e0718534720ea618101eed93bc to your computer and use it in GitHub Desktop.
Streaming on Edge Vercel Next js
import type { NextRequest } from "next/server";
import { isEmpty } from "lodash";
import { OPENAI_API_KEY } from "../../utils/variables";
import { OpenAICompletionsPayload, models } from "../../utils/types";
export const config = {
runtime: "edge",
};
export default async function handler(req: NextRequest) {
const improvedPrompt = `I am a refugee experiencing a humanitarian crises. I need your help assisting with the folowing question: ${req.headers.get(
"prompt"
)}`;
const payload: OpenAICompletionsPayload = {
model: models.GPT3,
messages: [{ role: "user", content: improvedPrompt }],
temperature: 0,
stream: true,
};
const res = await fetch("https://api.openai.com/v1/chat/completions", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
method: "POST",
body: JSON.stringify(payload),
});
const stream = res.body.getReader();
const readable = new ReadableStream({
async start(controller) {
while (true) {
const { value, done } = await stream.read();
if (done) {
break;
} else {
controller.enqueue(value);
}
}
controller.close();
},
});
return new Response(readable, {
headers: { "Content-Type": "text/html; charset=utf-8" },
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment