Package: chatgpt-prompt-wrapper
chatgpt-prompt-wrapperPython CLI implementation forChatGPT.RequirementsPython 3.9, 3.10, 3.11InstallationBy Homebrew:$ brew install rcmdnk/rcmdnkpac/chatgpt-prompt-wrapperBy pip:$ pip3 install chatgpt-prompt-wrapperPreparationTo get an OpenAI API key, go toAccount API Keys - OpenAI API.Set it as the environment variableOPENAI_API_KEY.To load the variable when you start the terminal, write it in.bashrcor.zshrc.export OPENAI_API_KEY="sk-..."Alternatively, pass the key using the '-k' or '--key' argument in the command.UsageCommand-line interface Help$ cg help
usage: cg [-h] [-k KEY] [-c CONF] [-m MODEL] [-t MAX_TOKENS] [-T MIN_MAX_TOKENS] [-l TOKENS_LIMIT] [--show] [--hide] [--multiline]
          [--no_multiline] [--vi] [--emacs] [--show_cost]
          subcommand [message ...]

positional arguments:
  subcommand            Subcommand to run. Use 'commands' subcommand to list up available subcommands.
  message               Message to send to ChatGPT

optional arguments:
  -h, --help            show this help message and exit
  -k KEY, --key KEY     OpenAI API key.
  -c CONF, --conf CONF  Path to the configuration toml file.
  -m MODEL, --model MODEL
                        ChatGPT Model to use.
  -t MAX_TOKENS, --max_tokens MAX_TOKENS
                        The maximum number of tokens to generate in the chat completion. Set 0 to use the max values for the model
                        minus prompt tokens.
  -T MIN_MAX_TOKENS, --min_max_tokens MIN_MAX_TOKENS
                        The minimum of max_tokens for the completion when max_tokens = 0.
  -l TOKENS_LIMIT, --tokens_limit TOKENS_LIMIT
                        The limit of the total tokens of the prompt and the completion. Set 0 to use the max values for the model.
  --show                Show prompt for ask command.
  --hide                Hide prompt for ask command.
  --multiline           Use multiline input for chat command.
  --no_multiline        Use single line input for chat command.
  --vi                  Use vi mode at chat.
  --emacs               Use emacs mode at chat.
  --show_cost           Show cost used.$ cg commands
Available subcommands:
  Reserved commands:
    ask       : Ask w/o predefined prompt.
    chat      : Start chat w/o predefined prompt.
    discuss   : Start a discussion between GPTs. Give a them as a message.
    init      : Initialize config file with an example command.
    cost      : Show estimated cost used until now.
    commands  : List up subcommands (show this).
    version   : Show version.
    help      : Show help.
  User commands:
    test      : Example command to test the OpenAI API.
    ...Ask, Chataskandchatare reserved commands for running simple interaction without a predefined prompt.askcg ask <message>returns the answer from ChatGPT formessage.chatcg chatstarts a chat.:memo: Inchatmode, all messages in the past, including answers from
ChatGPT, will be sent each time you send a new message.The oldest message will be dropped when the total tokens (including the reserved tokens for the completion defined bymax_tokensormin_max_tokens) exceeds the tokens limit (tokens_limitor the number of max tokens (-1) for the used model).It means you will send almost the max length after a long conversation.
Please keep the cost in mind. You may want to settokens_limit.Discussdiscussis another reserved command which start a discussion between two ChatGPTs.Two GPTs are ask to perform as:Please engage in the discussion as a supporter.Please engage in the discussion as a opponent.You input message will be a message and they start a discussion.Please pushEnterto proceed a duscussion andCtrl-Cto quit a discussion.Configuration fileYou can define your command in the configuration files.A command can be in eitheraskmode,chatmode ordiscussmode.askmode: Send a predefined prompt and a message from the command line and receive one answer.chatmode: Start a chat with a predefined prompt if defined:chatmode can be eithermultilineor single (no_multiline), andvimoremacs.discussmode: Start a discussion between two different ChatGPTs.File pathThe default path to the configuration file is$XDG_CONFIG_HOME/cg/config.toml.If$XDG_CONFIG_HOMEis not defined, use~/.config/cg/config.toml.If it does not exist and~/.cg/config.tomlexists,
the existing file is used.You can change the path by-c <file>(--conf <file>) option.How to write the configuration fileThe configuration file is written in theTOML format.You can define a command as the top table name.globalis the special table to define global options,
which are enabled for all commands if the command does not have the corresponding options.The options for each table can be:description: Description of the command.mode: Setask,chatordiscuss. (default isaskmode.)show_cost: Settrueto show the cost at the end of the command.model: The model to use (default: "gpt-3.5-turbo").max_tokens: The maximum number of tokens to generate in the chat completion. Set 0 to use the maximum values for the model. (default: 0)min_max_tokens: The minimum ofmax_tokensfor the completion whenmax_tokens = 0. (default: 200)tokens_limit: The limit of the total tokens of the prompt and the completion. Set 0 to use the maximum values for the model. (default: 0)temperature: Sampling temperature (0 ~ 2). (default: 1)top_p: Probability (0 ~ 1) that the model will consider the top_p tokens. Do not set both temperature and top_p at the same time. (default: 1)presence_penalty: The penalty for the model to return the same token (-2 ~ 2). (default: 0)frequency_penalty: The penalty for the model to return the same token multiple times (-2 ~ 2). (default: 0)Table ofalias: Dictionary of role aliases. The default alias is: 'user' = 'User','system' = 'System','assistant' = 'Assistant'.model_max_tokens: Additional or updated model's max_token definitions.price: Additional or updated model's price definitions.List ofmessages: Dictionary of message, which must haveroleandcontent(message text).Forask,chatmodes,rolemust be one ofsystem,userandassistantFordiscussmode, three roles,theme,gpt1andgpt2are needed.The options for ask mode:show: Settrueto show prompt for non chat command.hide: Settrueto hide prompt for non chat command (default).The options for chat mode:multiline: Settrueto hide prompt for non chat command.no_multiline: Settrueto hide prompt for non chat command (default).vi: Settrueto use vi mode.emacs: Settrueto use emacs mode (default).You can make a example configuration file bycg init.Here is a example configuration:[global]# Global configuration# `global` is special name and not a subcommandmodel='gpt-3.5-turbo'# Following model_max_tokens and prices are pre-defined in# https://github.com/rcmdnk/chatgpt-prompt-wrapper/blob/main/src/chatgpt_prompt_wrapper/chatgpt/chatgpt.py# If you find new model or price change, you can overwrite these variables in config as below.[global.model_max_tokens]"gpt-3.5-turbo-16k"=16384[global.prices]"gpt-3.5-turbo-16k"=[0.003,0.004][test]# Example command to test the OpenAI API, taken from below.# [Chat completion - OpenAI API](https://platform.openai.com/docs/guides/chat/introduction)description="Example command to test the OpenAI API."show=true[[test.messages]]role="system"content="You are a helpful assistant."[[test.messages]]role="user"content="Who won the world series in 2020?"[[test.messages]]role="assistant""content"="The Los Angeles Dodgers won the World Series in 2020."[[test.messages]]role="user"content="Where was it played?"[sh]description="Ask a shell scripting question."[[sh.messages]]role="user"content="You are an expert of the shell scripting. Answer the following questions."[py]description="Ask a python programming question."[[py.messages]]role="user"content="You are an expert python programmer. Answer the following questions."[enjoy_chat]description="Chat example with a predefined prompt."mode='chat'[[chat.messages]]role="user"content="Let's enjoy a chat."[dd]mode='discuss'[dd.names]gpt1="pros"gpt2="cons"[[dd.messages]]role="theme"content="Discuss whether or not to actively adopt ChatGPT in the education field. Please provide one-sentence responses."[[dd.messages]]role="gpt1"content="Please speak from the standpoint that you should actively adopt ChatGPT in the field of education."[[dd.messages]]role="gpt2"content="Please speak from the standpoint that you should not actively adopt ChatGPT in the field of education."These messages will be sent as an prompt before your input message.You can give full questions and usecgw/o input messages like a first exampletestcommand.Command examples:testshpyenjoy_chatExample usage as a part of an external scriptGit commit by ChatGPTSeegit-gpt-commit.DevelopmentPoetryUsePoetryto setup environment.To install poetry, run:$ pip install poetryor usepipx(xis3or anything of your python version).Setup poetry environment:$ poetry installThen enter the environment:$ poetry shellpre-commitTo check codes at the commit, usepre-commit.pre-commitcommand will be installed in the poetry environment.First, run:$ pre-commit installThenpre-commitwill be run at the commit.Sometimes, you may want to skip the check. In that case, run:$ git commit --no-verifyYou can runpre-commiton entire repository manually:$ pre-commit run -apytestTests are written withpytest.Write tests in/testsdirectory.To run tests, run:$ pytestThe default setting runs tests in parallel with-n auto.
If you run tests in serial, run:$ pytest -n 0GitHub ActionsIf you push a repository to GitHub, GitHub Actions will run a test job
byGitHub Actions.The job runs at the Pull Request, too.It checks codes withpre-commitand runs tests withpytest.
It also makes a test coverage report and uploads it tothe coverage branch.You can see the test status as a badge in the README.RenovateIf you want to update dependencies automatically,install Renovate into your repository.