Skip to content

Instantly share code, notes, and snippets.

@samuelkarani
Last active May 1, 2025 12:03
Show Gist options
  • Save samuelkarani/dd16524bce3d73c71e01ba3906487d7e to your computer and use it in GitHub Desktop.
Save samuelkarani/dd16524bce3d73c71e01ba3906487d7e to your computer and use it in GitHub Desktop.
import { openai } from "@ai-sdk/openai";
import { streamText } from "ai";
export function OPTIONS() {
return new Response("OK", {
status: 200,
headers: {
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
"Access-Control-Allow-Headers":
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
},
});
}
export async function GET() {
const response = streamText({
model: openai("gpt-4o-mini"),
messages: [{ role: "user", content: "Write a short poem" }],
});
return response.toTextStreamResponse({
headers: {
"Content-Type": "text/event-stream",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
"Access-Control-Allow-Headers":
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment