This is a refactoring project of the original one xiaoxx970/chatgpt-in-terminal in C/C++.
cGPTerm enables chatting with ChatGPT in the terminal.
It supports for rendering markdown contents from ChatGPT's API responses.
Supports history retrieval with the up arrow key, stream output, and tokens counting.
Slash (/) commands are available in the chat box to toggle raw response mode, undo the last question and answer, modify the system prompt and more, see the available commands below for details.
It also supports saving chat messages to a JSON file and loading them from the file.
Uses the gpt-3.5-turbo model as default model, which is the same model used by ChatGPT (Free Edition).
You can also change API Host to configure proxy (See Available Arguments).
An OpenAI API key. You need to register an OpenAI account and obtain an API key.
OpenAI's API key can be generated on the page opened by clicking "View API keys" in the upper right corner of the homepage, direct link: https://platform.openai.com/account/api-keys
You can download all EXE file and DLLs within a zip file in Release, but you can also compile this project by yourself (Please see Section Compilation).
Before or after downloading the release package, you should install all dependencies which are:
Library | Library name with APT | Installation Command with APT |
---|---|---|
curl | libcurl4 | sudo apt install libcurl4 |
argtable | libargtable2-0 | sudo apt install libargtable2-0 |
jansson | libjansson4 | sudo apt install libjansson4 |
readline | readline-common | sudo apt install readline-common |
x11 | x11-common | sudo apt install x11-common |
pcre2 | libpcre2-8-0 | sudo apt install libpcre2-8-0 |
You need to check your Package manager to see how to install these libraries.
Then, unzip the package, run ./cgpterm
in the outcome folder. If all thing goes well, you should see cGPTerm start to work.
You can also add this folder to environment path, in order to run cGPTerm anywhere.
Attention: All my development is done under Debian using gcc 10.2.1. And I've already found out that using another gcc version may result in a segmentation fault after ChatGPT responses. And also when compiling with the release preset of cmake, the program cannot accept SSE Event and I don't know why. Therefore I highly suggest to use the compiled package in releases before this problem is solved. My compile settings are:
set(CMAKE_C_FLAGS "-Wall -g")
set(CMAKE_CXX_FLAGS "-Wall -g -O2")
You may edit the CMakeLists by yourself.
You need GCC and CMake to compile. Make sure that your GCC supports C++20.
-
clone this repo with:
git clone --recursive https://github.com/Ace-Radom/cGPTerm.git
-
install all dependencies' dev packages
Library Installation Command with APT curl sudo apt install libcurl4-openssl-dev
argtable sudo apt install libargtable2-dev
jansson sudo apt install libjansson-dev
readline sudo apt install libreadline-dev
x11 sudo apt install libx11-dev
pcre2 sudo apt install libpcre2-dev
Same, you need to check your own Package manager to see how to install these libraries, when you are not using apt.
-
make build directory with:
mkdir build cd build
-
configure CMake with:
cmake -DCLIP_EXAMPLES=OFF -DCLIP_TESTS=OFF ..
-
build
make
After that, you should see all EXE files, DLLs and static libraries under $/bin
. You can run ./cgpterm
there and it should work.
cGPTerm will automatic create log and config files under ~/.cgpterm
by the first launch.
You should configure your API Key with:
./cgpterm --set-apikey <YOUR_API_KEY>
Same as setting API Key, you don't need to open the config file (which is ~/.cgpterm/config.ini
) and edit it by yourself. cGPTerm provides a couple of commands to set all configs in config file. See Available Arguments to learn more.
Run with the following command:
cgpterm
Use Enter
to submit questions.
Here are some common shortcut keys (also shortcut keys for the shell):
Ctrl+L
: Clear screen, equivalent toclear
command in shellCtrl+C
: Stop the current chat requestCtrl+D
: Exit cGPTerm / Abort settingsTab
: Autocomplete commands or parametersCtrl+U
: Delete all characters to the left of the cursorCtrl+K
: Delete all characters to the right of the cursorCtrl+W
: Delete the word to the left of the cursor
Original chat logs will be saved to
~/.cgpterm/chat.log
This project also provides an updater to fetch the lastest release package from GitHub and install it automatically. You can use it to update your cGPTerm to latest version since v1.1.0.
Run with the following command:
cgpterm-update
This updater will first check if the local version is out of date. If it is, it will then try to download the latest release package from GitHub and install it.
You should put the updater in the same folder as other cGPTerm installed files. Otherwise the updater cannot find cGPTerm and will ask you if you want to install it (yes, it's also an installer).
The updater needs tar
to unpack release package. Use which tar
to see if it is installed.
If you are using a version prior to v1.1.0, you can first remove all local installed cGPTerm files (excluding configuration files) and install the new version using the updater.
cGPTerm provides a couple of arguments to control the software or set configs.
Arguments | Description | Example |
---|---|---|
-h, --help | Show Help Messages and exit | cgpterm --help |
-r, --raw | Enable raw mode | cgpterm --raw |
--load FILE | Load chat history from file | cgpterm --load chat_history_code_check.json |
--set-host HOST | Set API Host to use | cgpterm --set-host https://api.openai.com |
--set-apikey KEY | Set API Key for OpenAI | cgpterm --set-apikey sk-.... |
--set-timeout TIMEOUT | Set maximum waiting time for API requests | cgpterm --set-timeout 40 |
--set-gentitle | Set whether to automatically generate a title for chat | cgpterm --set-gentitle |
--set-saveperfix PERFIX | Set chat history file's save perfix | cgpterm --set-saveperfix chat_history_ |
--set-loglevel LEVEL | Set log level: DEBUG, INFO, ERROR, FATAL | cgpterm --set-loglevel DEBUG |
The configuration file is located at ~/cgpterm/config.ini
and is autogenerated. It can be modified using the program's --set
option or edited manually.
The default configuration is as follows:
[DEFAULT]
# API Host, use for configuring proxy
# It must in format like "https://xxx.xxxxxx.xxx", no slash at the end is needed or allowed
OPENAI_HOST=https://api.openai.com
# API key for OpenAI
OPENAI_API_KEY=
# The maximum waiting time for API requests, the default is 30s
OPENAI_API_TIMEOUT=30
# Whether to automatically generate titles for conversations, enabled by default (generating titles will consume a small amount of tokens)
AUTO_GENERATE_TITLE=True
# Define the default file prefix when the /save command saves the chat history. The default value is "./chat_history_", which means that the chat history will be saved in the file starting with "chat_history_" in the current directory
# At the same time, the prefix can also be specified as a directory + / to allow the program to save the chat history in a folder (note that the corresponding folder needs to be created in advance), for example: CHAT_SAVE_PERFIX=chat_history/
CHAT_SAVE_PERFIX=./chat_history_
# Log level, default is INFO, available value: DEBUG, INFO, ERROR, FATAL
LOG_LEVEL=INFO
-
/raw
: Switch response output style between raw and rendered Markdown formatAfter switching, use the
/last
command to reprint the last reply -
/stream
: Toggle stream output modeIn stream mode, the answer will start outputting as soon as the first response arrives, which can reducing waiting time. Stream mode is on by default.
-
/title
: Toggle whether to enable automatic title generationWhen automatic title generation is on, cGPTerm will generate a new title after the first conversation of the current chat.
-
/tokens
: Display the total tokens spent and the tokens for the current conversationGPT-3.5 has a token limit of 4096; use this command to check if you're approaching the limit
-
/usage
: Show account credits summary -
/timeout [new_timeout]
: Modify the API timeoutThe default timeout is 30 seconds, it can also be configured by setting
OPENAI_API_TIMEOUT=
in the config file, or using--set-timeout
argument. -
/model [model_name]
: Change AI modelgpt-3.5-turbo
,gpt-4
andgpt-4-32k
are supported. -
/system [new_prompt]
: Modify the system prompt -
/rand [randomness]
: Set Model sampling randomness (temperature in OpenAI official docs)Default 1.
Given randomness should be a real between 0 and 2.
-
/save [filename_or_path]
: Save the chat history to the specified JSON fileIf no filename or path is provided, the filename
<SAVE PERFIX>YEAR-MONTH-DAY_HOUR,MINUTE,SECOND.json
will be suggested on input. -
/undo
: Delete the previous question and answer -
/delete
or/delete first
: Delete the first conversation in current chatWhen the token is about to reach the upper limit, the user will be warned.
-
/delete all
: Clear current chat, delete all questions and responses -
/last
: Display last ChatGPT's reply -
/copy
or/copy all
: Copy the full ChatGPT's last reply (raw) to Clipboard -
/copy code
: Copy the code in ChatGPT's last reply to Clipboard -
/version
: Show cGPTerm local and remote version -
/list
: List all settings in use -
/help
: Show Slash Commands' help page -
/exit
: Exit the application
You can press
Ctrl_D
to abort settings.
You can submit Exit Words to exit cGPTerm. Exit words will be sent as a question to ChatGPT, and cGPTerm will exit after GPT replies.
Exit words include:
['再见', 'bye', 'goodbye', '结束', 'end', '退出', 'exit', 'quit']
You can also use /exit
command or Ctrl_D
to exit directly.
Upon exit, the token count for the chat session will be showed.
- dacap/clip: For accessing the clipboard.
- gh-markt/cpp-tiktoken: For tokens count when using stream mode.
- Ace-Radom/cpprich: For rich text output and Markdown render. (Rich Format library subproject under cGPTerm)
- ndevilla/iniparser: For parsering ini config file.
- jonhoo/pthread_pool: For thread-pool support.
You can find their licenses in THIRD_PARTY_LICENSE.
Sincere thanks to the developers of these projects.
If you want, you can always dive in. Feel free to open an issue, submit PRs or report bugs!
This project is licensed under the GPLv3 License after v1.0.0 release.
From v0.1.0 to v0.3.1 is licensed under the Apache-2.0 License.