如何使用部署在 Vercel 上的
serverless functions
使下面的示例工作?我不能使用edge functions
,因为一些依赖项(Firebase)需要node.js
,我找不到任何关于如何在eventsource
中使用next.js
的资源,或者它是否可能。来源:https://github.com/Nultlope/twitterbio/blob/main/pages/api/generate.ts
serverless functions
OpenAIStream.ts:
import { OpenAIStream, OpenAIStreamPayload } from "../../utils/OpenAIStream";
if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing env var from OpenAI");
}
export const config = {
runtime: "edge",
};
const handler = async (req: Request): Promise<Response> => {
const { prompt } = (await req.json()) as {
prompt?: string;
};
if (!prompt) {
return new Response("No prompt in the request", { status: 400 });
}
const payload: OpenAIStreamPayload = {
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
temperature: 0.7,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: 200,
stream: true,
n: 1,
};
const stream = await OpenAIStream(payload);
return new Response(stream);
};
export default handler;