Skip to content

Commit

Permalink
added lambda version
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutlope committed Jan 16, 2023
1 parent 4851fee commit 5df89a3
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pages/api/lambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NextApiRequest, NextApiResponse } from "next";

if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing env var from OpenAI");
}

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const prompt = req.body.prompt;

if (!prompt) {
return new Response("No prompt in the request", { status: 400 });
}

const payload = {
model: "text-davinci-003",
prompt,
temperature: 0.7,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: 1000,
n: 1,
};

const response = await fetch("https://api.openai.com/v1/completions", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY ?? ""}`,
},
method: "POST",
body: JSON.stringify(payload),
});

const data = response.body;
console.log("data", data);
return res.status(200).json(data);
};

export default handler;

0 comments on commit 5df89a3

Please sign in to comment.