Last active
April 28, 2023 04:25
-
-
Save kengonakajima/aeea122adaa4e0b0c32cdcbd3c681ed7 to your computer and use it in GitHub Desktop.
Execute ChatGPT text completion in command line, by node.js
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
#! node | |
if(process.argv.length<=2) process.exit(1); | |
const { Configuration, OpenAIApi } = require("openai"); | |
const configuration = new Configuration({apiKey: process.env.OPENAI_API_KEY}); | |
const openai = new OpenAIApi(configuration); | |
const wrap = fn => { | |
return (req, res, next) => { | |
return fn(req, res, next).catch(next); | |
} | |
}; | |
wrap( async ()=>{ | |
try { | |
const completion = await openai.createCompletion({ | |
model: "text-davinci-003", | |
prompt: process.argv.slice(2).join(" "), | |
max_tokens: 300, | |
temperature: 0 | |
}); | |
let t=completion.data.choices[0].text; | |
t=t.replace(/^\s+|\s+$/g, ''); | |
console.log(t); | |
} catch (error) { | |
if (error.response) { | |
console.log(error.response.status); | |
console.log(error.response.data); | |
} else { | |
console.log(error.message); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment