Skip to content

Instantly share code, notes, and snippets.

@Brodan
Created September 7, 2020 20:02
Show Gist options
  • Save Brodan/e619d8ec822b168e4e15c9e225b1f4e0 to your computer and use it in GitHub Desktop.
Save Brodan/e619d8ec822b168e4e15c9e225b1f4e0 to your computer and use it in GitHub Desktop.
const got = require('got');
exports.handler = function(context, event, callback) {
let message = "";
let twiml = new Twilio.twiml.MessagingResponse();
const prompt = event.Body !== undefined ? event.Body.trim() : event["prompt"]
got.post('https://api.openai.com/v1/engines/davinci/completions', {
json: {
"prompt": prompt,
"max_tokens": 50,
"temperature": 0.9,
"top_p": 1,
"presence_penalty": 0.4,
"frequency_penalty": 0.75,
"stop": "\n",
},
headers: {
'Authorization': `Bearer ${context.OPENAI_SECRET_KEY}`,
}
})
.json()
.then(response => {
message = response.choices[0].text
})
.catch(error => {
console.log(error);
message = "Oops! Something went wrong.";
})
.finally(() => {
twiml.message(message);
callback(null, twiml);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment