diff --git a/README.md b/README.md index 49a8a47..19e2afe 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ -# chat-gpt-shell- +# chat-gpt-cli Access ChatGPT through the Command Line + +## Setup +Obtain a OpenAI API KEY. Create a ```env.js``` file in the root directory and put the following line in it ```module.exports = 'YOUR_OPEN_AI_KEY'``` + +Use ``` npm link ``` to symlink package folder. +```chat-gpt``` command should be available in your shell. + +## Example +``` +$ chat-gpt how many inches in a mile +===================== Chat-GPT ===================== + + +There are 63,360 inches in a mile. +``` diff --git a/chat-gpt.js b/chat-gpt.js new file mode 100755 index 0000000..17f6865 --- /dev/null +++ b/chat-gpt.js @@ -0,0 +1,36 @@ +#!/usr/bin/env node +let prompt = ""; +for(let i = 2; i < process.argv.length; i++){ + prompt += process.argv[i] + ' ' +} +if(process.argv.length <= 2 || prompt.trim().length == 0){ + console.error('No prompt provided to ChatGPT') + process.exit(1) +} + +const { Configuration, OpenAIApi } = require('openai'); +const configuration = new Configuration({ + apiKey: require('./env'), +}); +const openai = new OpenAIApi(configuration); + +async function generateText() { + const response = await openai.createCompletion({ + model: 'text-davinci-002', + prompt: prompt, + temperature: 0, + max_tokens: 1000, + }); + + if(response.data.choices[0].text){ + console.log('===================== Chat-GPT =====================') + console.log(response.data.choices[0].text) + console.log('\n') + } + else{ + console.error('No response generated') + } + +} + +generateText(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..9708ce6 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "dependencies": { + "openai": "^3.2.1" + }, + "name": "chat-gpt-cli", + "description": "Access ChatGPT through the Command Line", + "version": "1.0.0", + "main": "gpt.js", + "devDependencies": {}, + "scripts": { + "test": "node gpt.js How many cups in a gallon" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/tombyrn/chat-gpt-shell-.git" + }, + "author": "Thomas Byrnes", + "license": "GPL-3.0", + "bugs": { + "url": "https://github.com/tombyrn/chat-gpt-shell-/issues" + }, + "homepage": "https://github.com/tombyrn/chat-gpt-shell-#readme", + "bin": { + "chat-gpt": "./chat-gpt.js" + } +}