This project enables chatting with ChatGPT in the terminal.
Markdown content in answers is rendered as beautifully formatted rich text.
Supports history retrieval with the up arrow key, optional multi-line questions, and tokens counting.
Slash (/) commands are available in the chat box to toggle multi-line submit mode, undo the last question and answer, modify the system prompt and more.
Supports saving chat messages to a JSON file and loading them from the file.
Uses the gpt-3.5-turbo model, which is the same model used by ChatGPT (Free Edition).
-
Clone the repo and enter the directory
git clone https://github.com/xiaoxx970/chatgpt-in-terminal.git cd ./chatgpt-in-terminal
-
Write the OPENAI_API_KEY variable in the
.env
file in the project root directory, as followsOPENAI_API_KEY=your_API_KEY
OpenAI keys can be generated from the top-right corner of the homepage by clicking
View API keys
. Direct link: https://platform.openai.com/account/api-keysIf you don't configure the
.env
file, you can also input the API KEY directly when running, which is valid for a single session. -
Install dependencies through requirements.txt
pip3 install -r requirements.txt
Run with the following command:
python3 chat.py
Original chat logs will be saved to chat.log
-
/raw
: Display raw text in replies instead of rendered Markdown formatAfter switching, use the
/last
command to reprint the last reply -
/multi
: Enable or disable multi-line mode, allowing users to enter multi-line textIn multi-line mode, use [[Esc]] + [[Enter]] to submit the question
If pasting multi-line text, single-line mode can also paste properly
-
/tokens
: Display the API token count and token length for the current conversationGPT-3.5 has a token limit of 4097; use this command to check if you're approaching the limit
-
/last
: Show the last reply -
/save [filename_or_path]
: Save the chat history to the specified JSON fileIf no filename or path is provided, the default filename
chat_history_YEAR-MONTH-DAY_HOUR,MINUTE,SECOND.json
is prompted on input. -
/system [new_prompt]
: Modify the system prompt -
/timeout [new_timeout]
: Modify API timeout.The default timeout is 20 seconds, it can also be configured by setting
OPENAI_API_TIMEOUT=
in the.env
file. -
/undo
: Delete the previous question and answer -
/help
: Display available commands -
/exit
: Exit the application
options:
-h, --help show this help message and exit
--load FILE Load chat history from file
-m, --multi Enable multi-line mode
-r, --raw Enable raw mode
Multi-line mode and raw mode can be used simultaneously
In the chat, use exit words to end the current session. Exit words include:
['再见', 'bye', 'goodbye', '结束', 'end', '退出', 'exit', 'quit']
Exit words will be sent as a question to ChatGPT, and the application will exit after GPT replies.
You can also use Ctrl-D
or /exit
to exit immediately.
Upon exit, the token count for the chat session will be displayed.
Current price: $0.002 / 1K tokens, Free Edition rate limit: 20 requests / min
- Added slash (/) command functionality
- Added
--load
runtime argument to load previously saved chat history - Modified program structure and interaction methods, changing the original
input()
function to theprompt_toolkit
library's input interface, supporting multi-line input, command-line completion, and other features. - Improved error handling mechanisms, added chat history backup, logging, and other features, enhancing the program's reliability and fault tolerance.
- Refactored code logic and function structure, improving modularity and readability.
Thanks to the following projects for providing strong support for this script:
- rich: For rendering rich text in the terminal
- python-dotenv: For loading environment variables from
.env
file - prompt_toolkit: Command-line input processing library
├── README.md # Documentation
├── chat.py # Project code
├── requirements.txt # Dependency package list
├── chat.log # Chat log generated after chatting
└── .env # API key storage file
This project is licensed under the MIT License.