gptcli
is a command-line interface tool that leverages the GPT (Generative Pre-trained Transformer) model from OpenAI to generate text based on user-provided input. This tool simplifies text generation using the GPT model directly from the command line.
To use the gptcli
tool, ensure you have Python installed on your system. You can install the tool via pip:
pip install git+https://github.com/ivandustin/gptcli.git
The basic syntax for utilizing the gptcli
tool is as follows:
gpt instructions.txt input.txt > output.txt
You can also use -
for any file parameters to read from stdin. For example:
cat input.txt | gpt instructions.txt - > README.md
Suppose you need to write a technical document.
- Write a draft of your technical document in
draft.txt
. - Create an instructions file named
tech-doc.txt
with the following content:
Create a technical document.
Generate the document with the following command:
gpt tech-doc.txt draft.txt > README.md
Specify the OpenAI model to be used by setting the OPENAI_MODEL
environment variable.
You can also use the --model
argument to specify the OpenAI model directly in the command:
gpt instructions.txt input.txt --model gpt-4o > output.txt
To authenticate with the OpenAI API, you need to specify your API key via the OPENAI_API_KEY
environment variable.
For structured output, provide a JSON Schema in the --schema
option:
gpt instructions.txt input.txt --schema schema.json > output.json
You can use Pydantic to generate a JSON Schema document.
You can import the gpt
function in your project that allows you to generate text based on a model, temperature, instructions, and input.
from gptcli import gpt
model = "gpt-4o"
temperature = 0.7
instructions = "Generate a creative story based on the following input."
input_text = "Once upon a time in a land far, far away..."
output = gpt(model, temperature, instructions, input_text)
print(output)
from gptcli.json import gpt
import json
model = "gpt-4o"
temperature = 0
instructions = "Get the age of Anne."
schema = {
"properties": {
"age": {
"title": "Age",
"type": "integer"
}
},
"required": [
"age"
],
"title": "Schema",
"type": "object"
}
input_text = "The age of Prince is 26. The age of Anne is 21. The age of Anna is 19."
output = gpt(model, temperature, instructions, schema, input_text)
print(json.dumps(output, indent=4))
Output
{
"age": 21
}
If you encounter any character encoding issue, especially in Windows, you may need to set the PYTHONUTF8
environment variable to 1
.