Giter Club home page Giter Club logo

ai.py's Introduction

ai.py

A single-file Python script that interacts with ChatGPT API in the command-line.

Features:

  • Use shortcuts to access predefined prompts
  • Highlight code in output
  • Support one-shot queries and conversations
  • Use special command like !set to control the behavior when chatting

Install

Just copy the script to a folder in $PATH, like /usr/local/bin. You can also change its name to ai to get ride of the .py extension.

Here's a command that can directly install the script into your system:

curl https://raw.githubusercontent.com/reorx/ai.py/master/ai.py -o /usr/local/bin/ai && chmod +x /usr/local/bin/ai

You can also install it with pip or pipx:

pip install aidotpy

Usage

Paste your OpenAI API key to ~/.config/ai-py/config.json, or set it in AI_PY_API_KEY environment variable.

mkdir -p ~/.config/ai-py
echo '{"api_key":"<Your API key>"}' > ~/.config/ai-py/config.json

For detail usage of the script, please read the description of ./ai.py -h:

usage: ai [-h] [-s SYSTEM] [-c] [--history HISTORY] [-w] [-v] [-t] [-d] [--version] [PROMPT]

A simple CLI for ChatGPT API

positional arguments:
  PROMPT                your prompt, leave it empty to run REPL. you can use @ to load prompt
                        from the prompts file.

options:
  -h, --help            show this help message and exit
  -s SYSTEM, --system SYSTEM
                        system message to use at the beginning of the conversation. if starts
                        with @, the message will be located through the prompts file
  -c, --conversation    enable conversation, which means all the messages will be sent to the
                        API, not just the last one. This is only useful to REPL
  --history HISTORY     load the history from a JSON file.
  -w, --write-history   write new messages to --history file after each chat.
  -v, --verbose         verbose mode, show execution info and role in the message
  -t, --show-tokens     show a breakdown of the tokens used in the prompt and in the response
  -d, --debug           debug mode, enable logging
  --version             show program's version number and exit

One-off query

Pass the prompt as the first argument:

./ai.py 'hello world'

You can also pass the prompt through a pipe (|):

head README.md | ./ai.py 'Proofreading the following text:'

REPL

Run without argument for Read–eval–print loop:

./ai.py

By default only the last message and the system message are sent to the API, if you want it to remember all the context (i.e. send all the messages in each chat), add -c argument to enable conversation:

./ai.py -c

System message

You can pass a system message to define the behavior for the assistant:

./ai.py -s 'You are a proofreader' 'its nice know you'

You can also save your predefined system messages in ~/.config/ai-py/prompts.json and refer them with @ at the beginning, this will be covered in the next section.

Prompt shortcuts

You can predefine prompts in ~/.config/ai-py/prompts.json and refer to them by using @ as a prefix. This works for both system messages and user messages.

Suppose your ~/.config/ai-py/prompts.json looks like this:

{
  "system": {
    "cli": "As a technology assistant with expertise in command line, answer questions in simple and short words for users who have a high-level background. Provide only one example, and explain as less as possible."
  },
  "user": {
    "native": "Paraphrase the following sentences to make it more native:\n",
    "revise": "Revise the following sentences to make them more clear concise and coherent:\n",
    "": ""
  }
}

Then you can use the cli prompt shortcut in system message by:

./ai.py -s @cli

and use the native or revise prompt shortcut in user message by:

./ai.py '@native its nice know you'

It's great to get to know you.

Verbose mode

Add -v to print role name and parameters used in the API call.

Screenshot

Special commands

You can use special commands to control the behavior of the script when running in REPL.

Here's a list of available commands:

  • !set <key> <value>: set a key-value pair in the config, available keys are:
    • verbose: set to True or False, e.g. !set verbose True
    • conversation: set to True or False, e.g. !set conversation True
    • system: set the system message. e.g. !set system you are a poet, !set system @cli
    • params: set the parmeters for the ChatGPT API. e.g. !set params temperature 0.5
    • model: set the model to use. e.g. !set model gpt-4
  • !info: print the execution info
  • !write-history: write current messages to history file. e.g. !write-history history.json

ai.py's People

Contributors

avimitin avatar bili123 avatar intron014 avatar reorx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ai.py's Issues

FeatureRequest: none-interactive conversation mode

"none-interactive conversation mode" I guess you can imagine what i mean, for everyone else reading it:

"-c" enabled conversation mode, where all previous communication is send as history, so chat.ai can understand the context of a rephrased sentence. This only works in interactive mode inside ai.py.

here is the request:

if a "Prompt" and option "-c" are given together, accept a "history"-file ( in the 90s we called it a brainfile ) as a third argument and send it with the prompt to the API. For better parsing I suggest an ordered JSON String-Array [ sortorder: oldest first ].

This would allow external code to get better results via ai.py .

18kb history file => crash in ai.py

Issue:

Mär 27 14:59:06 eve python3[120317]: detected unhandled Python exception in '/usr/local/sbin/ai.py'
Mär 27 14:59:06 eve abrt-server[120321]: Executable '/usr/local/sbin/ai.py' doesn't belong to any package and ProcessUnpackaged is set to 'no'
Mär 27 14:59:06 eve abrt-server[120321]: 'post-create' on '/var/spool/abrt/Python3-2023-03-27-14:59:06-120317' exited with 1
Mär 27 14:59:06 eve.resellerdesktop.de abrt-server[120321]: Deleting problem directory '/var/spool/abrt/Python3-2023-03-27-14:59:06-120317'

as soon as i deleted the history file, it worked again.

I suggest to limit it to the last 10 entries.

18kb history file => crash in ai.py

Issue:

Mär 27 14:59:06 eve python3[120317]: detected unhandled Python exception in '/usr/local/sbin/ai.py'
Mär 27 14:59:06 eve abrt-server[120321]: Executable '/usr/local/sbin/ai.py' doesn't belong to any package and ProcessUnpackaged is set to 'no'
Mär 27 14:59:06 eve abrt-server[120321]: 'post-create' on '/var/spool/abrt/Python3-2023-03-27-14:59:06-120317' exited with 1
Mär 27 14:59:06 eve.resellerdesktop.de abrt-server[120321]: Deleting problem directory '/var/spool/abrt/Python3-2023-03-27-14:59:06-120317'

as soon as i deleted the history file, it worked again.

I suggest to limit it to the last 10 entries.

参数传递问题

大佬,请问我用py ai.py -v可以看到verbose模式,可是如果用ai -v或ai.py -v的话就会直接进入REPL循环,而没有详细信息,换其他参数也一样,似乎参数传递不进去,请问这是为什么呀

FR: move config file to $HOME/.config/ instead of $HOME

having the homedir full of hidden directories and files, everyone should place theire files to the correct directories according to the FreeDesktop doctrine (!=Windows ofcourse) .

In this case, it would be $HOME/.config/

OR accept an ENV variable with the API key and do not require a single-line-configfile ;)

we need a free license to include your work

Pls Add a license or grant an explicit usage right, so your script can get included into AND distributed by this:

https://github.com/Cyborgscode/Personal-Voice-Assistent

BTW: it's working, but the overall usecase is limited due to jibbrish inspired, imagined textrecognition from the STS system ;) But it's FUN all night long :)

STS + an AI Bot is the best use-case of mocking about defective technology in progress .. like in the Big Bang Theory ;)

Thanks.

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.