#chatgpt #ai #markdown #cli #gpt #cache #config-file

bin+lib pgpt

A CLI to ask ChatGPT anything with pretty markdown rendering (Pretty GPT)

3 unstable releases

0.2.0 May 27, 2024
0.1.1 May 24, 2024
0.1.0 May 24, 2024

#2398 in Command line utilities

23 downloads per month

MIT license

2.5MB
829 lines

Pretty GPT

Ask ChatGPT anything directly from the terminal with pretty markdown rendering! With support for previous chat messages!

Inspired by shell-gpt-rs

Demo

GIF showcasing how to use pgpt CLI

Installation

Cargo

cargo install pgpt
pgpt --help

Source

git clone https://github.com/ammar-ahmed22/pgpt.git
cd pgpt
cargo install --path .

Usage

When using for the first time, pgpt will prompt you to enter an OpenAI API key.

The key is saved/encrypted in a local config file to be used later.

Alternatively, you can pass your API key to the environment variable OPENAI_API_KEY in order to not save it.

Configuration (config set)

model

Set the default model to use when asking questions.

pgpt config set model <MODEL>

Currently supported options are gpt-3, gpt-4, and gpt-4o

cache-length

Sets the number (positive integer) of prompt/response pairs to save in cache. Think of this as your chat history. Defaults to 5.

pgpt config set cache-length <LENGTH>

context

Sets the number (positive integer) of previous prompt/response pairs to send with a query. If value is greater than cache-length, all saved values will be sent. Defaults to 0.

pgpt config set context <NUMBER>

api-key

Sets the OpenAI API key.

pgpt config set api-key <API_KEY>

To display the configuration values for any of the above options use:

pgpt config show <OPTION>

or display all of the configuration values with pgpt config show all

Cache Clearing

As the cache (saved chat history) can become very long or irrelevant, you can clear the saved history with:

pgpt config clear cache

Question

Asking a question to ChatGPT using saved configuration parameters

pgpt query what is the meaning of life

This will query ChatGPT using the model set in configuration and any chat history saved up to the context value.

--cost

Passing the --cost flag will display the total cost for the query using the model dependent prices.

pgpt query --cost how to do a for loop in Rust

-m, --model

Override the model set in configuration and use a specific model for the query

pgpt query -m gpt-4o how to reverse a linked list in C++

-c, --context

Override the context value set in the configuration.

For example, if the context in configuration is set to 3 but you only want to send the last message with the query.

pgpt -c 1 Explain what you just said before this

-s, --show-context

Display the previous messages that are being sent with the query

pgpt -s elaborate further

Examples

We'll use the default values set by CLI to start:

Showing the configuration

pgpt config show all
Creating configuration file with default values at "/Users/ammar/Library/Application Support/com.pgpt.pgpt/./config.json"
Model: gpt-3
API Key (encrypted): ����p"��Ý�<H*���g�4ق
                                         �"O��?��
                                                 ��/���s��G�C�EFQ~����Y�t�K���=���x���T�d|���G�;Hud�
Cache Length: 5
Context: 0
To display cache, run `pgpt config show cache`

Set the context to the max value

pgpt config set context 5
Setting context to 5
Saved config successfully!

Ask a question

pgpt query what is a for loop
Saved cache successfully!
Cache capacity 1/5

Response from gpt-3.5-turbo-0125
A for loop is a control flow statement that allows you to iterate over a sequence of elements (such as a list, tuple, 
dictionary, etc.) and execute a block of code for each element in the sequence. It consists of three parts: 
initialization, condition, and increment/decrement. The loop continues to execute as long as the condition is true, 
and the increment/decrement part is used to update the loop variable.

Show the current cache

pgpt config show cache
Cache:
Cached 1/1
You said: what is a for loop
GPT said:
A for loop is a control flow statement that allows you to iterate over a sequence of elements (such as a list, tuple, dictionary, etc.) and execute a block of code for each element in the sequence. It consists of three parts: initialization, condition, and increment/decrement. The loop continues to execute as long as the condition is true, and the increment/decrement part is used to update the loop variable.

Ask a follow-up question (with the total cost and previous messages)

pgpt query --cost --show-context how do I do that in Rust
Saved cache successfully!
Cache capacity 2/5
You said:
what is a for loop
GPT said:
A for loop is a control flow statement that allows you to iterate over a sequence of elements (such as a list, tuple, 
dictionary, etc.) and execute a block of code for each element in the sequence. It consists of three parts: 
initialization, condition, and increment/decrement. The loop continues to execute as long as the condition is true, 
and the increment/decrement part is used to update the loop variable.

You said:
how do I do that in Rust

Response from gpt-3.5-turbo-0125
In Rust, you can use the for loop to iterate over a collection of items. Here is an example of a basic for loop in 
Rust:

fn main() {                       
    let numbers = [1, 2, 3, 4, 5];
                                  
    for number in numbers.iter() {
        println!("{}", number);   
    }                             
}                                 

In this example, the for loop iterates over each element in the numbers array and prints it out. The iter() method is 
used to create an iterator over the array.

You can also use ranges in for loops in Rust. Here's an example:

fn main() {               
    for i in 0..5 {       
        println!("{}", i);
    }                     
}                         

In this example, the for loop iterates over a range from 0 to 5 (excluding 5) and prints each number.

Cost: $0.000345

[!NOTE] The above outputs are shown without the formatting that would be displayed in the terminal. Most terminals support ANSI colors which would display the responses from ChatGPT in terminal markdown.

License

MIT

Dependencies

~17–31MB
~521K SLoC