import { NextApiRequest, NextApiResponse } from "next"; import { Configuration, OpenAIApi } from "openai"; const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(configuration); function generatePrompt(message: string) { return `The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly. Human: Hello, who are you? AI: I'm good, thank you. How are you and how may I help you today? Human: ${message} AI: ` ; } const response = async (req: NextApiRequest, res: NextApiResponse) => { try { const completion = await openai.createCompletion({ model: "text-davinci-002", prompt: generatePrompt(req.body.messageBody), temperature: 0.9, max_tokens: 150, top_p: 1, frequency_penalty: 0.0, presence_penalty: 0.6, stop: [" Human:", " AI:"], }); const result= completion.data.choices[0].text res.status(200).json(result); res.end() } catch (error) { res.json(error); } }; export default response;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter