Run OpenAI's ChatGPT from the commandline
$ git clone https://github.com/triciopo/gptcli
$ cd gptcli
$ pip install .
$ gptcli --help
usage: gptcli.py [-h] [-m MODEL] [-f FILE] [-n NUM] [-t TEMP] [--code] [--translate] ...
Run OpenAI's ChatGPT from the commandline
positional arguments:
prompt Main prompt (Leave blank to enable chat mode)
options:
-h, --help show this help message and exit
-m MODEL, --model Select model (default: gpt-3.5-turbo)
-f FILE, --file FILE Specify a file to include in the prompt
-n NUM, --num NUM Number of responses generated by one input (default: 1)
-t TEMP, --temp TEMP What sampling temperature to use, between 0 and 2. (default: 1)
$ python gptcli.py "What is the meaning of life?"
This will generate a response to the prompt "What is the meaning of life?" using the default model gpt-3.5-turbo
$ gptcli -n 3 -t 1.3 -m gpt-4 "Tell me a joke"
This will generate three responses to the prompt "Tell me a joke" using the gpt-4
model with 1.3
temperature.
To generate a response to the prompt "What does this code do?" and include a file in the prompt:
$ gptcli -f example.py "What does this code do?"
# Prompt: What does this code do?
num1 = 5
num2 = 15
sum = num1 + num2
print(f'{num1} + {num2} = {sum}')
Output:
This code initializes two variables `num1` and `num2` with integer values 5 and 15 respectively.
It then adds `num1` and `num2` together and stores the result in a variable called `sum`.
Finally, it uses an f-string to print out the message "5 + 15 = 20" where 5, 15, and 20
are replaced with the values of the variables `num1`, `num2`, and `sum`, respectively.
This will generate a response to the prompt "What does this code do? + {file content}"
Config files are generated on $HOME/.config/gptcli
Alternatively, your API key can also be set with the env "OPENAI_API_KEY"
Example config.ini
file:
# https://platform.openai.com/account/api-keys
api_key = <YOUR API KEY GOES HERE>
model = gpt-3.5-turbo
[file_format_prompt]
# Format:
# {.fileExtension}: {prompt}
# EG: .pdf : Summarize this document for me:
.txt = Summarize this for me:
[args_prompts]
# Format:
# {argument}: {prompt}
# EG: summarize: Summarize this text for me:
# Example Usage: ./gptcli.py --code Write a python code to solve the Two Sum problem.
code = Answer only with code. Do not include comments or explanation.
You can define pre-prompts that are based on the file extension on config.ini
file.
Example:
[file_format_prompt]
.pdf = Summarize this for me:
$ gptcli -f doc.pdf
You can define pre-prompts for certain arguments in your config.ini
file. Args pre-prompts override file extension prompts.
Example:
[args_prompts]
code = Print only code. Do not include comments or anything, except code.
$ gptcli --code "Write a python code to multiply matrices."
def multiply_matrix(matrix1, matrix2):
result = [[0 for j in range(len(matrix2[0]))] for i in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result