Code Monkey home page Code Monkey logo

chatgpt-in-terminal's Introduction

Chat with GPT in Terminal

English badge 简体中文 badge Platform badge standard-readme compliant

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.

example

Uses the gpt-3.5-turbo model, which is the same model used by ChatGPT (Free Edition).

Installation

  1. Clone the repo and enter the directory

    git clone https://github.com/xiaoxx970/chatgpt-in-terminal.git
    cd ./chatgpt-in-terminal
  2. Write the OPENAI_API_KEY variable in the .env file in the project root directory, as follows

    OPENAI_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-keys

    image-20230303233352970

    If you don't configure the .env file, you can also input the API KEY directly when running, which is valid for a single session.

  3. Install dependencies through requirements.txt

    pip3 install -r requirements.txt

How to Use

Run with the following command:

python3 chat.py

Original chat logs will be saved to chat.log

Available Commands

  • /raw: Display raw text in replies instead of rendered Markdown format

    After switching, use the /last command to reprint the last reply

  • /multi: Enable or disable multi-line mode, allowing users to enter multi-line text

    In 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 conversation

    GPT-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 file

    If 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

Available Arguments

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

Exit Words

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

Changelog

2023-03-23

  • 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 the prompt_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.

Dependencies

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

Project Structure

├── README.md           # Documentation
├── chat.py             # Project code
├── requirements.txt    # Dependency package list
├── chat.log            # Chat log generated after chatting
└── .env                # API key storage file

License

This project is licensed under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.