This CLI tool allows you to send a prompt to OpenAI's GPT-4 model and receive a response directly from the command line. It uses axios
for HTTP requests, yargs
for command line argument parsing, and dotenv
for environment variable management.
- Node.js
- npm (Node Package Manager)
- OpenAI API key
1.) Clone the repository:
git clone https://github.com/yourusername/chatgpt-cli-tool.git
cd chatgpt-cli-tool
2.) Install the required dependencies:
npm install
3.) Create a .env file in the root directory and add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here
To use the CLI tool, run the following command in this format:
node index.js -p "Enter prompt here"
node index.js -p "Explain the concept of recursion in computer science."
-p, --prompt: The prompt to send to ChatGPT. This option is required. -h, --help: Show help information.
Response from ChatGPT:
Choice 1: Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. A recursive function calls itself with modified parameters, allowing the problem to be solved in a step-by-step manner until a base case is reached.
If the OpenAI API key is not found in the environment variables, the tool will display the following error message and exit:
OpenAI API key not found. Set the OPENAI_API_KEY environment variable.
If there's an error querying the OpenAI API, the tool will display the error message and exit:
Error querying OpenAI: [Detailed error message]
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please fork the repository and submit a pull request. Would like to ideally have a frontend GUI at some point.