Created
March 7, 2023 17:18
-
-
Save stoerr/cf15974da46b3f2cb51a007e17213ab1 to your computer and use it in GitHub Desktop.
A little script to access ChatGPT from the command line
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
// see also https://platform.openai.com/playground?mode=complete | |
// command line example: | |
// node suggestCommand.js "Write a bash command line to do the following: log the last 3 git commits" | |
// Needs an API key in ~/.openaiapi , see https://beta.openai.com/docs/api-reference/authentication | |
// Idea for a shellscript calling that. You could also add things to the prompt there. | |
// #!/bin/bash | |
// node "$(dirname "$(readlink -f "$0")")/suggestCommand.js" "$*" | |
const axios = require('axios'); | |
const fs = require('fs'); | |
const readline = require('readline'); | |
// Read the API key from the file | |
const apiKey = fs.readFileSync(`${process.env.HOME}/.openaiapi`, 'utf-8').trim(); | |
// Read the prompt from the command line arguments | |
const prompt = process.argv[2]; | |
// Make the API request to generate code completions | |
axios.post('https://api.openai.com/v1/completions', { | |
prompt: prompt, | |
max_tokens: 1000, | |
echo: false, | |
model: 'text-davinci-003', | |
temperature: 0.7, | |
}, { | |
headers: { | |
'Authorization': `Bearer ${apiKey}`, | |
'Content-Type': 'application/json', | |
}, | |
}) | |
.then(response => { | |
const completion = response.data.choices[0].text.trim(); | |
// console.log(response.data.choices); | |
console.log(response.data.choices.map(choice => choice.text.trim()).join('\n')); | |
}) | |
.catch(error => { | |
console.error(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment