Created
December 10, 2022 06:59
-
-
Save jordiup/25dbe210419df818f875f86a87eaf93b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ChatGPTAPI } from 'chatgpt'; | |
import { NextApiRequest, NextApiResponse } from 'next'; | |
import { notifyAdmin } from '../../../utils/twillio'; | |
export type InputAI = {; | |
variableA: string; | |
variableB: string; | |
variableC: string; | |
}; | |
export default async (req: NextApiRequest, res: NextApiResponse) => { | |
let body = req.body as InputAI; | |
if (req.method === 'POST') { | |
try { | |
const api = new ChatGPTAPI({ | |
sessionToken: process.env.CHATGPT_SESSION_TOKEN, | |
}); | |
// ensure the API is properly authenticated | |
await api.ensureAuth(); | |
let gptInput = ` | |
This is where you insert your ChatGPT prompt: | |
You can add in any conditional logic, or variables by using | |
This style of notation: | |
${body.variableA}, ${body.variableB}, ${body.variableC}`; | |
// send a message and wait for the response | |
const response = await api.sendMessage(gptInput); | |
res.send(response); | |
} catch (err) { | |
notifyAdmin(['AI Error', err.message]); | |
res.status(500).send(err); | |
} | |
} else { | |
res.status(400).send('Bad Request'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment